链表实现栈的模拟
 
 
package com.iflytek.stack;
class Node{
    public int num;
    public Node next;
    public Node(int num) {
        this.num = num;
    }
    @Override
    public String toString() {
        return "Node{" +
                "num=" + num +
                '}';
    }
}
class LinkStack{
    private Node head=new Node(-1);
    
    public boolean isEmpty(){
        return head.next==null;
    }
    
    public void push(Node node){
        if (head.next==null){
            head.next=node;
            return;
        }
        node.next=head.next;
        head.next=node;
    }
    
    public void pop(){
        if (head.next==null){
            System.out.println("栈为空!不能出栈!");
        }
        System.out.println(head.next+"出栈");
        head=head.next;
    }
    
    public void showLinkList(){
        if (isEmpty()){
            System.out.println("栈为空");
        }
        Node tmp=head.next;
        while (tmp!=null){
            System.out.println("节点为 "+tmp);
            tmp=tmp.next;
        }
    }
}
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);
        LinkStack linkStack=new LinkStack();
        linkStack.push(node1);
        linkStack.push(node2);
        linkStack.push(node3);
        linkStack.push(node4);
        linkStack.showLinkList();
        linkStack.pop();
        System.out.println("出栈后");
        linkStack.showLinkList();
    }
}