Program to implement Dynamic Stack :-
#include<iostream>
#include<iostream>
#include<conio.h>
#include<stdlib.h>
struct node
{
int a;
node *next;
};
class queue
{
node *top,*end;
public:
queue()
{
top= end=NULL;
}
void push();
void pop();
void show();
};
void queue::push()
{
node *p;
if(!(malloc(sizeof(p))))
{
cout<<"MEMORY EXHAUSTED\n";
return;
}
p=new node;
cout<<"Enter an element :\n";
cin>>p->a;
p->next=NULL;
if(top!=NULL)
{
p->next=top;
}
top=p;
cout<<"Element inserted\n";
}
void queue::pop()
{
node *p;
if(top==NULL)
{
cout<<"UNDERFLOW\n";
return;
}
p=top;
top=top->next;
cout<<"Removed element is "<< p->a <<endl;
delete p;
}
void queue::show()
{
node *p;
if(top==NULL)
{
cout<<"Queue is empty\n";
return;
}
p=top;
while(p!=NULL)
{
cout<<" <- "<<p->a;
p=p->next;
}
}
void main()
{
queue q;
int c;
while(1)
{
cout<<"\n\nPress 1 - to insert element\nPress 2 - to delete element\nPress 4 - to exit:\n";
cin>>c;
switch(c)
{
case 1:
q.push();
q.show();
break;
case 2:
q.pop();
q.show();
break;
case 4:
exit(0);
default:
cout<<"INVALID OPTION\nChoose valid option from menu.....\n";
break;
}
}
}
No comments:
Post a Comment