简体   繁体   中英

java- how to implement a pop operation in stack which is implemented using linked list?

I am currently using Java to complete a test module which is concerned about implementing a stack using Linked List.

I have implemented the push() operation but for the pop() operation I did not get the correct result. I Tried as following :

public class LLStack implements LLStackQInterface{
SintNode head = new SintNode(0);
@Override
public void push(int num) {

head.nextNode = new SintNode(num);
head = head.nextNode;
}

@Override
public int pop() {

    SintNode t = head;
    while(t.nextNode!=null)
    {
        t=t.nextNode;

    }
    t = null;
    return 0;

}

I have to push out the latest element so I navigate to last element in the list using t pointer but I did not get the result!

I do get this as the result :

Failed for the input: PUSH 1 PUSH 3 POP PUSH 4 PUSH 6
POP PUSH 8 PUSH 9 POP
Expected output is: 8->4->1->NULL

Actual output generated by your code: 9->8->6->4->3->1->NULL

I managed to reverse the list but clearly my pop() does not work. What shall I do?

Both your pop and push methods are bugged.

Your management of next pointers in push is incorrect. You first set the next pointer of the current head to the new node, and then assign the new node to head. Therefore, head.next == null . The correct way to do it is:

public void push(int num) {
        SintNode n = new SintNode(num);
        n.next = head;
        head = n;
}

This way, you insert the new node at the front of the list and maintain a pointer to the next element, ie the previous head of the list.

In your pop method, you traverse to the end of the list. This is incorrect, since you add elements to the front of the list. A stack is a LIFO (Last-In First-Out) data structure, meaning you should remove the last element that was inserted. In this case, it is the head of the list.

public int pop() {
        if (head == null)
           return 0;

        SintNode t = head;
        head = t.next;
        t = null;
        return 0;

}

Here, we first set the new head to be head.next , and then delete the current head .

I ran the corrected version, and the actual output matches the expected output.

@Override public void push(int num) {
    // store a copy of the current head
    SintNode temp = this.head;

    // update the current head to the pushed value
    this.head = new SintNode(num);

    // link the head to the rest of the stack, as each subsequent node will point to another one until the tail
    this.head.nextNode = temp; }

@Override public int pop() {
    // assuming SintNode has a value property
    int value = this.head.value;

    // remove this node by overwriting it as the next
    this.head = this.head.nextNode;

    // the value of removed node
    return value; }

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