简体   繁体   中英

Iterating through nodes in a push() method for a linked list stack

I'm creating a push() method for adding nodes to a linked list stack. I'm unable to iterate and loop from one node to the next using the already-defined variables.

I've tried various loop arguments to iterate through the .csv file, however at this point only one record of the five in the file is being retained. The pop method is being utilized to take off the top node in the stack, so my end result is to have 4 nodes printed.

public class Stack {

    Node first;

    public Stack() {
        //constructor initalizes the empty stack
        first = null;
    }

    public void push(Node newNode) //add the node to the top of the stack
    {
        //if the stack is empty, make first point to new Node.
        if (first == null) {
            first = newNode;
            return;

        } //if the stack is not empty, loop until we get
        //to the end of the list
        else if (first != null) {

            Node tempNode = first;


            //loop to advance from one node to the next until
            //last node is found
        while (tempNode.next == null)
        {
            //then make the last Node point to the newNode
            tempNode.next = newNode;

            }

        }
    }


    public Node pop() {


        //remove the last node from the top of the stack

    {
        //if the stack is empty, return null

        if(first == null)
            {
                return null;
            }

        //handle the case where there is only one node in the stack

        else if(first.next == null)
        {
                System.out.println("Only one node in list");
        }


        //remove first 
        return first = first.next;

}

}

    public void print()

            //print the contents of the entire stack

    {
        //display the entire stack

        //start at the beginning of linkedList
        Node tempDisplay = first; 

        while (tempDisplay != null) //executes until we don't find end of list.
        {
            tempDisplay.displayNode();
            tempDisplay = tempDisplay.next; // move to next Node
        }

        System.out.println();
    }



}

The code inside your while loop attaches the newNode at the end of the linked list (as a child of tempNode ). This is correct, but that should not be done inside a loop. To fix this, move the assignment tempNode.next = newNode out of the loop.

Now you need to make sure that, when this assignment happens, tempNode actually is the last node. For that, use the following loop:

while (tempNode.next != null) {
    tempNode = tempNode.next;
}

In total, this could be the result:

public void push(Node newNode) {
    if (first == null) {
        first = newNode;
        return;
    }
    Node tempNode = first;
    while (tempNode.next != null) {
        tempNode = tempNode.next;
    }
    tempNode.next = newNode;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM