使用单链表 模拟栈

无人久伴 提交于 2020-01-10 06:32:08
//使用单链表模拟栈
class MM{
    public static void main(String[] args){
        singleList list=new singleList(5);
        list.push(1);
        list.push(2);
        list.push(3);
        list.push(4);
        list.showlist();
        list.pop();
        System.out.println("..............");
        list.showlist();
    }
}
class singleList{
    Node head=new Node(0);
    int maxSize;
    public singleList(int maxSize) {
        this.maxSize = maxSize;
    }
    void push(int n){
        Node temp=head;
        int i=0;
        while (true){
            if(temp.getNext()==null)break;
            temp=temp.getNext();
            i++;
        }
        if(i<maxSize){
        Node newnode=new Node(n);
        temp.setNext(newnode);
        }else{
            System.out.println("栈满!无法添加");
        }
    }
    void pop(){
        Node temp=head;
        if(temp.getNext()==null){
            System.out.println("栈空,无法抛出");
            return;
        }
        while (true){
            if(temp.getNext().getNext()==null)break;
            temp=temp.getNext();
        }
        temp.setNext(null);
    }
    void showlist(){
        Node temp=head.getNext();
        if(temp==null){
            System.out.println("栈空没有数据");
            return;
        }
        while (true){
            System.out.println(temp);
            temp=temp.getNext();
            if(temp==null)break;
        }

    }
}
class Node{
    private int no;
    private Node next;

    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 +
                '}';
    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!