堆栈推送方法无限循环 Java

Stack Push Method Infinite Loop Java

我不明白为什么调用 printList() 时会无限循环。我试图在不使用 java 中的内置堆栈方法的情况下编写堆栈链表并打印列表。为什么我的打印方法无限循环,我该如何解决这个问题?

public class LinkedListStack{
    private String item;
    private Node next;
    private Node top = null;

    public LinkedListStack(){
    }

    public void push(String item){
        top = new Node(item, top);
    }

    public void printList(){
        Node currentNode = top;
        for(currentNode = top; currentNode.getItem()!= null; currentNode = currentNode.getNext()){
            System.out.println(currentNode.getItem());
        }
    }

    public class Node{
        public Node(String newItem, Node nextNode){
            item = newItem;
            next = nextNode;
        }

        public Node(String newItem){
            item = newItem;
            next = null;
        }

        //to set the value of the next field
        public void setNext(Node nextNode){
            next = nextNode;
        }

        //read the value of the next field
        public Node getNext(){
            return(next);
        }

        //to set the value of the item field
        public String setItem(String newItem){
            item = newItem;
            return(item);
        }

        //read the value of the item field
        public String getItem(){
            return(item);
        }
    }

    public static void main(String args[]){
        LinkedListStack newList = new LinkedListStack();
        newList.push("hello");
        newList.push("goodbye");
        newList.printList();
    }
}

问题是 itemnextLinkedListStack 的字段并且在所有 Node 实例之间共享。当您创建另一个 Node 并设置项目时,您会更改 all 个节点。要修复它,只需将字段声明移动到 Node 内部 class。 除此之外, printList 方法中的循环条件是错误的:下一个节点为空,而不是它的项目。 这是一个工作示例:

public class LinkedListStack {
    private Node top = null;

    public LinkedListStack() {
    }

    public void push(final String item) {
        top = new Node(item, top);
    }

    public void printList() {
        Node currentNode = top;
        for (currentNode = top; currentNode != null; currentNode = currentNode.getNext()) {
            System.out.println(currentNode.getItem());
        }
    }

    public class Node {
        private String item;
        private Node next;

        public Node(final String newItem, final Node nextNode) {
            item = newItem;
            next = nextNode;
        }

        public Node(final String newItem) {
            item = newItem;
            next = null;
        }

        // to set the value of the next field
        public void setNext(final Node nextNode) {
            next = nextNode;
        }

        // read the value of the next field
        public Node getNext() {
            return next;
        }

        // to set the value of the item field
        public String setItem(final String newItem) {
            item = newItem;
            return item;
        }

        // read the value of the item field
        public String getItem() {
            return item;
        }
    }

    public static void main(final String args[]) {
        final LinkedListStack newList = new LinkedListStack();
        newList.push("hello");
        newList.push("goodbye");
        newList.printList();
    }
}