程序设计 java

来源:百度知道 编辑:UC知道 时间:2024/06/23 08:20:50
There is a data structure called a drop-out stack that behaves like a stack in every respect except that if the stack size is n, when the n+1 element is pushed, the first element is lost. Implement a drop-out stack using links.

上面是程序要求,正确运行即可得分
请给出明确的程序,是stack用links. 请一些必要的说明, 谢谢了 , 刚提高了悬赏, 希望能有合适的答案, 不希望浪费掉这些分数.

import java.util.ArrayList;
import java.util.List;

public class DropOutStack {
private int size;
private List<Object> list;

public DropOutStack(int size) throws Exception {
if(size <= 0) {
throw new Exception("循环栈的大小需大于0");
}
this.size = size;
list = new ArrayList<Object>();
}

public void push(Object obj) throws Exception {
int lsize = list.size();
if(lsize >= size) {
list.remove(0);
}
list.add(obj);
}

public Object pop() throws Exception {
if(list.size() == 0) {
throw new Exception("栈中元素为空");
}
return list.remove(list.size()-1);
}

public String toString() {
return list.toString();
}
public static void main(String[] args) throws Exception {
DropOutStack dos = new DropOutStack(5);
dos.push("A");
System