public class SequenceStack { |
private String[] urls; // 数组,存储用户请求的url |
private int top; // 栈顶指针 |
private final static int maxSize = 20 ; // 数组长度 |
public SequenceStack() { |
urls = new String[maxSize]; |
top = - 1 ; |
} |
// url入栈 |
public void push(String url) { |
if (top == maxSize - 1 ) { // 栈满处理 |
if (url.equals(urls[top])) { // 防止刷新,如果栈顶url与传入的url相等,即为刷新操作 |
return ; |
} |
// 更新数组保存的url |
String[] otherUrls = urls.clone(); |
for ( int i = 0 ; i <= maxSize - 1 ; i++) { |
if (i == top) { |
break ; |
} |
otherUrls[i] = urls[i + 1 ]; // 前移 |
} |
otherUrls[top] = url; |
urls = otherUrls; |
} else { // 栈未满 |
top++; |
urls[top] = url; |
} |
} |
// 栈顶元素即为前页的url |
public String pop() { |
if (top == - 1 || top == 0 ) { |
return null ; |
} |
return urls[top]; |
} |
public String[] getUrls() { |
return urls; |
} |
public void setUrls(String[] urls) { |
this .urls = urls; |
} |
public int getTop() { |
return top; |
} |
public void setTop( int top) { |
this .top = top; |
} |
public static int getMaxsize() { |
return maxSize; |
} |
} |