2
#include<iostream>
#include<cstdlib>
using namespace std;

 struct node
 {
   int data;   //data
   node *next; //link
 };

 class stack // stack using linked list
 {
  public:
  node *top; // top element of stack

  public:
   stack()
   {
     top= NULL;
   }

   void push(int value)
   {
     node *temp  = new node; // create a new node
     temp-> data = value;
     temp-> next = NULL;
   if(top==NULL)          //  stack is empty
    {
      top=temp;
      temp=NULL;
    }
    else
    {
      temp-> next = top;
      top=temp;
      temp=NULL;
    }
  }
  //template <class X>
  void pop()
  {
    if(top==NULL)
    {
      cout<<"\nStackOverflow "<<endl;
      cout<<"Program Terminated "<<endl;
      exit (0);
    }
    else
    {
      top=top->next;
    }

  }

  void display()
  {

    node *temp=new node;
    temp=top;

    while(temp!=NULL)
    {
      cout<<temp->data<<" ";
      temp = temp-> next;
    }
    while(top==NULL)

     { 
        cout<<"\nStack is Empty "<<endl;  
        exit (0);

     }



  }
};

int main()
{
   stack a;

  a.push(5);
  a.display();
  a.push(10);
  a.display();
  a.pop();
  a.pop();
  a.push(20);
  a.display();
  a.pop();
  a.display();
  return 0;
}

The Output of this code is 5 10 5 20 Stack is Empty.

Which is wrong output and the correct output is 5 10 20 Stack is Empty..

Anyone tell me why this errors occured.

The Refrence of the code :[Implementation of stack using Templates and And Linked List in c++

5
  • Unrelated to your problem, but your display function have a memory leak. Commented Sep 28, 2018 at 7:39
  • As for your problem, you push two values, then pop both values (making the stack empty), then push one value and pop that value again making the stack empty. What else did you expect? Please edit your question to include the expected output. Commented Sep 28, 2018 at 7:41
  • dispaly() leaks memory somewhere iknow but how can i overcome with that Commented Sep 28, 2018 at 7:41
  • Why do you create a new node in display()? Commented Sep 28, 2018 at 7:47
  • @AkkiêThakur Just change node *temp=new node; to node *temp;. There's no need to allocate a node there because the very next line says temp=top; Your version allocates a node and then throws it away, a.k.a a memory leak. Commented Sep 28, 2018 at 7:48

2 Answers 2

6

No, the output is correct.

a.push(5);
a.display();

This displays the first 5.

a.push(10);
a.display();

The 5 is still on the stack, so now this displays the 10 and then the 5.

a.pop();
a.pop();
a.push(20);
a.display();

Now everything is removed, the 20 is added and the displayed, so this should just display 20.

And then the empty stack is printed with

a.pop();
a.display();

So put together, it should display 5 10 5 20 Stack is Empty.

Sign up to request clarification or add additional context in comments.

4 Comments

sir i think the correct output will be 5 10 20 Stack is Empty
Nah, the 5 is correctly printed twice. You put the 5 on the stack and call display() twice before popping the 5 off the stack, so it is printed both times.
a.push(5); a.display(); a.push(10); a.display(); you call display twice with 5 on the stack. So it will be printed twice. The output of this code is 5 10 5, as you see.
@AkkiêThakur I'm guessing that you think display should show the last item pushed onto the stack, but it doesn't it shows the whole stack.
4
a.push(5);     // Stack:  5
a.display();   // Output: new: 5
a.push(10);    // Stack:  10 5
a.display();   // Output: old: 5 new: 10 5
a.pop();       // Stack:  5
a.pop();       // Stack:  empty
a.push(20);    // Stack:  20
a.display();   // Output: old: 5 10 5 new: 20
a.pop();       // Stack:  empty
a.display();   // Output: old: 5 10 5 20 new: 

stack()
{
    top= NULL;  // use initializ list instead of assignment
                // in constructors body
}

-->

stack() : top{ nullptr } {}

void pop()
{
    if (top == NULL)
    {
        cout << "\nStackOverflow " << endl;
        cout << "Program Terminated " << endl;
        exit(0);  // don't use exit() in C++ if there are other ways!
    }
    else
    {
        top = top->next; // the memory top pointed to
                         // before the assignment leaks!
    }
}

-->

void pop()
{
    if (!top) {
        cout << "Pop on empty stack!\n";
        return;
    }

    node *old_top = top;
    top = top->next;
    delete old_top;
}

void display()
{
    node *temp = new node;  // no need to allocate a node in a
                            // function that should only make output
    temp = top;             // the memory temp points to before the
                            // assignment leaks

    while (temp != NULL)
    {
        cout << temp->data << " ";
        temp = temp->next;
    }
    while (top == NULL)  // why a loop?
    {
        cout << "\nStack is Empty " << endl;
        exit (0);   // again ...
    }
}

-->

void display() const
{
    if (!top) {
        std::cout << "Stack is empty!\n";
        return;
    }

    for(node *current = top; current; current = current->next)
        cout << current->data << ' ';
}

6 Comments

sir when i implemented your code it shows error in pop().
display () is well implemented sir. Overall thank you sir for your help
@AkkiêThakur "it shows error in pop()" yes, there was a } missing.
but sir it shows the output 5 10 5 20 Stack is empty! is this true sir
@sir why this print 5 before 20 in the output
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.