package com.acwer.queue;
public class MyStack {
public static void main(String[] args) {
MyStack stack=new MyStack();
stack.push(new Person("宋江"));
stack.push(new Person("卢俊义"));
stack.push(new Person("吴用"));
stack.push(new Person("公孙胜"));
for (int i = 0; i < 10; i++) {
stack.push(new Person("公孙胜"+String.valueOf(i)));
}
for (;;){
if(stack.empty()) break;
System.out.println(stack.pop());
}
}
private StackNode head=new StackNode<>();
private StackNode tail=head;
public void push(T data){
StackNode node=new StackNode<>(data,tail,null);
//尾连前进
tail.next=node;
tail=tail.next;
}
public boolean empty(){
return head==tail;
}
public T pop(){
if(empty()){
throw new RuntimeException("空");
};
StackNode node =tail;
//尾链后退
tail=tail.pre;
//help GC
node.next=null;
node.pre=null;
return node.data;
}
private static class StackNode{
private T data;
public void data(T data){
this.data=data;
}
public T data(){
return this.data;
}
public StackNode pre;
public StackNode next;
public StackNode(){
this(null);
}
public StackNode(T data){
this(data,null,null);
}
public StackNode(T data,StackNode pre,StackNode next){
this.data=data;
this.pre=pre;
this.next=next;
}
}
}