1.链栈
人生.又仿佛是 天小小的找重现 童年父母每天抱你不断地迸出家门,壮
年你每天奔波于家与事业之间,老年你每天独自豌跚于养老院的门里屋前
public class ChainStack {
private Node top;
private int count;
public ChainStack() {
}
public void push(Object element) {
Node node = new Node(element);
node.next = top;
top = node;
count++;
}
public Object pop() {
if (top == null) {
throw new RuntimeException("空栈");
}
Node temp = top;
Object element = top.data;
top = top.next;
temp.next = null;
count--;
return element;
}
class Node {
Node next;
Object data;
public Node(Object data) {
this.data = data;
}
}
public static void main(String[] args) {
ChainStack chainStack = new ChainStack();
chainStack.push(1);
chainStack.push(2);
System.out.println(chainStack.pop());
System.out.println(chainStack.pop());
System.out.println(chainStack.pop());
}
}
网友评论