#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class NODE
{
public:
int data;
NODE *next;
void insert();
void display();
void delet();
int isemty();
};
NODE
*nw,*top=NULL,*t1,*temp;
int NODE:: isemty()
{
if(top==NULL)
{
return(1);
}
else
{
return(0);
}
}
void NODE ::insert()
{
nw=new(NODE);
cout<<"\nenter a new node";
cin>>nw->data;
nw->next=top;
top=nw;
}
void NODE::display()
{
if(isemty())
{
cout<<"\nStack is NULL";
}
else
{
cout<<"\nElements in stack
are:";
temp=top;
while(temp!=NULL)
{
cout<<"\n"<<temp->data;
temp=temp->next;
}
}
}
void NODE::delet()
{
if(isemty())
{
cout<<"\nStack is NULL";
}
else
{
t1=top;
top=top->next;
cout<<"Deleted element
is:"<<t1->data;
free(t1);
}
}
void main()
{
NODE d1;
int ch;
do
{
cout<<"\n1.Insert
Element\n2.Delete Element\n3.Display elements\n4.exit";
cout<<"\n Enter your choice";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\n***Insert
Element***";
d1.insert();
break;
case 2:
cout<<"\n***Delete
Element***";
d1.delet();
break;
case 3:
cout<<"\n***Display
Elements***";
d1.display();
break;
case 4:
exit(0);
break;
default :
cout<<"invalid choice";
break;
}
}while(ch!=5);
getch();
}
/*OUTPUT*/
1.Insert Element
2.Delete Element
3.Display elements
4.exit
Enter your choice1
***Insert Element***
enter a new node12
1.Insert Element
2.Delete Element
3.Display elements
4.exit
Enter your choice1
***Insert Element***
enter a new node23
1.Insert Element
2.Delete Element
3.Display elements
4.exit
Enter your choice1
***Insert Element***
enter a new node58
1.Insert Element
2.Delete Element
3.Display elements
4.exit
Enter your choice1
***Insert Element***
enter a new node25
1.Insert Element
2.Delete Element
3.Display elements
4.exit
Enter your choice3
***Display Elements***
Elements in stack are:
25
58
23
12
1.Insert Element
2.Delete Element
3.Display elements
4.exit
Enter your choice2
***Delete
Element***Deleted element is:25
1.Insert Element
2.Delete Element
3.Display elements
4.exit
Enter your choice