#include<iostream.h>
#include<conio.h>
#include<process.h>
struct node
{
int data;
node *next;
};
class stack
{
node *top;
public:
stack()
{
top=NULL;
}
void push(int d)
{
node *p=new node;
p->data=d;
p->next=top;
top=p;
}
void pop()
{
node *temp=top;
if(top==NULL)
cout<<"Stack Underflow";
else
{
cout<<"The element to be deleted is"<<top->data;
top=top->next;
delete temp;
}
}
void display()
{
node *temp=top;
if(top==NULL)
cout<<"Stack Underflow";
else
{
while(temp)
{
cout<<temp->data<<" ";
temp=temp->next;
}
}
}
};
void main()
{
clrscr();
stack s;
int d,option;
while(1)
{
cout<<"\n1.Push \n 2.Pop \n 3.Display\n 4.Exit\n Enter your Option";
cin>>option;
switch(option)
{
case 1:
cout<<"\nEnter the element to be pushed";
cin>>d;
s.push(d);
break;
case 2:
s.pop();
break;
case 3:
s.display();
break;
case 4:
exit(0);
}
}
}
#include<conio.h>
#include<process.h>
struct node
{
int data;
node *next;
};
class stack
{
node *top;
public:
stack()
{
top=NULL;
}
void push(int d)
{
node *p=new node;
p->data=d;
p->next=top;
top=p;
}
void pop()
{
node *temp=top;
if(top==NULL)
cout<<"Stack Underflow";
else
{
cout<<"The element to be deleted is"<<top->data;
top=top->next;
delete temp;
}
}
void display()
{
node *temp=top;
if(top==NULL)
cout<<"Stack Underflow";
else
{
while(temp)
{
cout<<temp->data<<" ";
temp=temp->next;
}
}
}
};
void main()
{
clrscr();
stack s;
int d,option;
while(1)
{
cout<<"\n1.Push \n 2.Pop \n 3.Display\n 4.Exit\n Enter your Option";
cin>>option;
switch(option)
{
case 1:
cout<<"\nEnter the element to be pushed";
cin>>d;
s.push(d);
break;
case 2:
s.pop();
break;
case 3:
s.display();
break;
case 4:
exit(0);
}
}
}