#include
#include
struct node
{
int data;
node *next;
};
class stack
{
node *start,*last,*head;
public:
void push();
void pop();
void display();
stack()
{
start=last=NULL;
}
};
void stack::push()
{
head=new node;
cout<<"Enter the data to be inserted :";
cin>>head->data;
if(start==NULL)
{
head->next=NULL;
start=last=head;
}
else
{
head->next=start;
start=head;
}
}
void stack::pop()
{
node *temp;
if(start==NULL)
{
cout<<"\nStack is empty";
}
else
{
temp=start;
cout<<"\nDeleted item is : "<
if(start==last)
{
start=last=NULL;
}
else
{
start=start->next;
delete temp;
}
}
}
void stack::display()
{
node *temp;
temp=start;
if(temp==NULL)
{
cout<<"\nStack is empty :";
}
else
{
cout<<"\nThe elements are : ";
while(temp!=NULL)
{
cout<<" "<
temp=temp->next;
}
}
}
void main()
{
stack s;
int opt;
char ch;
clrscr();
do
{
cout<<"\nMENU";
cout<<"\n1.PUSH()\n2.POP()\n3.DISPLAY()\nEnter your choice:";
cin>>opt;
switch(opt)
{
case 1:s.push();break;
case 2:s.pop();break;
case 3:s.display();break;
default:cout<<"Invalid choice\n";
}
cout<<"\nDo you want to continue?(Y/N) :";
cin>>ch;
}while(ch=='y'||ch=='Y');
getch();
}
No comments:
Post a Comment