java 中用双向链表模拟栈

来源:百度知道 编辑:UC知道 时间:2024/05/25 05:23:50
最近学数据结构 想问 java 中用双向链表模拟栈??

这里只是实现了Stack的部分功能

public class Stack{

private Node top;

public Stack(){
this.top = null;
}

public void push(Node node){
if(node == null)
return;
if(this.top == null){
this.top = node;
node.setNext(null);
node.setPre(null);
}
else{
this.top.setNext(node);
node.setPre(this.top);
node.setNext(null);
this.top = node;
}
}

public Node pop(){
if(this.top == null)
return null;
Node curr = this.top;
Node pre = curr.getPre();
pre.setNext(null);
this.top = pre;
return curr;
}

public Node top(){
return this.top;
}

public boolean isEmpty(){
return this.top == null ? true : false;
}

public void empty(){
this.top = null;
}

public static void main(String[] args){
Stack