package LinkStack;
public class LinkStack {
Node top=null;
public boolean isEmpty(){
return top==null;
}
public void push(Node node){
if (top==null){
top=node;
}else {
Node pre=top;
top.setNext(node);
top=node;
top.setPre(pre);
}
}
public Node pop(){
if (isEmpty()){
throw new RuntimeException("栈为空!");
}
Node node=top;
top=top.getPre();
top.setNext(null);
return node;
}
public void list(){
if (isEmpty()){
System.out.println("栈为空!");
return;
}
Node temp=top;
while (temp!=null){
System.out.println(temp);
temp=temp.getPre();
}
}
}
class Node{
private int no;
private Node next;
private Node pre;
public Node(int no){
this.no=no;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
@Override
public String toString() {
return "Node{" +
"no=" + no +
'}';
}
public Node getPre() {
return pre;
}
public void setPre(Node pre) {
this.pre = pre;
}
}
package LinkStack;
public class LinkStackDemo {
public static void main(String[] args) {
Node node1=new Node(1);
Node node2=new Node(2);
Node node3=new Node(3);
Node node4=new Node(4);
Node node5=new Node(5);
LinkStack stack=new LinkStack();
stack.push(node1);
stack.push(node2);
stack.push(node3);
stack.push(node4);
stack.push(node5);
stack.list();
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println("出栈后----------");
stack.list();
}
}