Bijon Guha
Bijon Guha

Reputation: 51

How pop is working in stack using linked list?

Here in the pop function, after top is assigned to temp, its written top=top->link, but how is top=top->link making it point to its back node?

top=top->link is meant to move forward and point to the node to its front and here it should be NULL.

EDIT: cout added in pop. Now to make question more specific, how cout<<top->data will print 32 ? it should be pointing to NULL.

#include<iostream>
using namespace std;

class stack_ll{
    private:
        struct node{
            int data;
            node *link;
        }*top;

    public:
        stack_ll()
        {
            top=NULL;
        }
        void push(int);
        void pop();
        void display();

        ~stack_ll()
        {
            if(top==NULL)
            return;

            node *temp;
            while(top!=NULL)
            {
                temp=top;
                top=top->link;
                delete temp;
            }
        }
};

void stack_ll::push(int num)
{
    node *temp;
    temp=new node;

    if(temp==NULL) {
    cout<<"stack is full"<<endl;
    return;
    }

    temp->data=num;
    temp->link=top;
    top=temp;

}

void stack_ll::pop(){
    if(top==NULL)
    {
        cout<<"stack is empty"<<endl;
        return;
    }

    node * temp;
    int item;

    temp=top;
    item=temp->data;
    top=top->link;
    cout<<top->data<<endl;
    delete temp;

}

int main()
{
    stack_ll l1;
    l1.push(5);
    l1.push(56);
    l1.push(32);
    l1.push(34);
    l1.pop();
    l1.push(23);
    l1.pop();
}

Upvotes: 1

Views: 225

Answers (2)

Arpit Ratan
Arpit Ratan

Reputation: 3026

This implementation is correct. You don't need to change anything.

Working of push

When we try to add an element is this stack, we create a new node and make it head of the link list. Hence the last element added will always be head of the link list.

Working of of Pop

When we try to pop an element we just do top- = top->link. This means we just changed the head node to the previous element which was added in the stack.

Upvotes: 1

CIsForCookies
CIsForCookies

Reputation: 12817

You are right. Pop here will remove the first object and will now have top as NULL even though there are some objects in the stack. What you should do to fix that, is change the link direction - meaning each object will point to the lower object. In that case, when removing the top, you will be able to retrieve the next object, that is lower in the stack

change:

temp->data=num;
temp->link=top;
top=temp;

to:

temp->data=num;
top->link=temp;
top=temp;

and add a special case for an empty stack where top->link=temp; will not be legal like:

if(top==NULL){
    temp->data=num;
    top=temp;
    top->link=NULL;
}

EDIT:

your logic should be like this:

empty stack has only a null_ptr (top)

push: if stack is empty - top is now new element and it's link is null_ptr, else - top's link is now new element, top becomes new element and new element's link is null_ptr

pop: if stack is empty do nothing, else - print top's data, save top as temp, top becomes top's link (the lower element in stack), print temp's data and delete temp

Upvotes: 3

Related Questions