Code:
#include <iostream.h>
#include<stdlib.h>
#include<conio.h>
struct node
{
int data;
struct node *link;
};
struct node *top=NULL,*temp;
void main()
{ int choice,data;
while(1)//infinite loop is used to insert/delete infinite number of nodes
{ cout << "\n1.Push\n2.Pop\n3.Display\n4.Exit\n";
cout << "\nEnter ur choice:";
cin >> choice;
switch(choice)
{ case 1:
temp=(struct node *)malloc(sizeof(struct node));
cout << "Enter a node data :";
cin >> data;
temp->data=data;
temp->link=top;
top=temp;
break;
case 2:
if(top!=NULL)
{
cout << "The poped element is " << data,top->data;
top=top->link;
}
else
{ cout << "\nStack Underflow"; }
break;
case 3:
temp=top;
if(temp==NULL)
{ cout << "\nStack is empty\n"; }
while(temp!=NULL)
{
cout << "->" <<data << "->",temp->data;
temp=temp->link; }
break;
case 4:
exit(0); }
}
}
n i choose option2 is pop
the element of popped is 11
and again option 2 pop
the element of popped is 11
n if i put display option3
the output is 11->11->11->11->
do you know what's wrong with this?
thanks

