You are on page 1of 3

/*P12. Implement stack and queue using linked list.

*/

#include<iostream.h>
struct node
{
int data;
node *next;
}*front,*rear,*p,*np;

void push(int x)
{
np = new node;
np->data = x;
np->next = NULL;
if(front == NULL)
{
front = rear = np;
rear->next = NULL;
}
else
{
rear->next = np;
rear = np;
rear->next = NULL;
}
}

void push1(int x)
{
np = new node;
np->data = x;
np->next = NULL;
if(front == NULL)
{
front = np;
front->next = NULL;
}
else
{
np->next = front;
front = np;
}
}

int remove()
{
int x;
if(front == NULL)
{
cout<<"underflow \n";
}
else
{
p = front;
x = p->data;
front = front->next;
delete(p);
return(x);
}
}

void display()
{
cout<<"\n\n List is\n___________________________\n";
node *t;
t=front;
if(t==NULL)
{
cout<<"EMPTY\n__________________________\n";
return;
}
while(t->next!=NULL)
{
cout<<t->data<<" ";
t=t->next;
}
cout<<" "<<t->data<<"\n___________________________";
cout<<endl;

int main()
{
int ch,t,d;
char k='y';
cout<<"******************by parth trehan**********************\n";
cout<<"******************roll no.=03116412814**********************\n";
cout<<"****************************************\n";
cout<<" 1.QUEUE\n";
cout<<" 2.STACK\n";
cout<<"*****************************************\n";
cout<<" Enter your choice....";
cin>>ch;
switch(ch)
{
case 1:
{
cout<<" for QUEUE \n";
cout<<" Enter the information of front node ";
front=new node;
cin>>front->data;
front->next=NULL;
rear=front;
while(k=='y')
{
cout<<"1.Enter in the QUEUE\n2.Delete in the QUEUE\n3.display the
QUEUE\n ";
cout<<" Enter your choice... ";
cin>>t;
switch(t)
{
case 1:
cout<<"Enter the information ... ";
cin>>d;
push(d);
display();
break;
case 2:
if(front == NULL)
{
cout<<"underflow \n";
}
else
cout<<"\n Element Removed is.. "<<remove()<<endl;
case 3:
display();
break;
default :
cout<<"!!!!!!Enter correct option !!!!!!!";
}
cout<<"CONTINUE y/n ";
cin>>k;
}
break;
}
case 2:
{
cout<<" for STACK \n";
cout<<" Enter the information of top node ";
front=new node;
cin>>front->data;
front->next=NULL;
rear=front;
while(k=='y')
{
cout<<"1.PUSH in stack\n2.POP form stackk\n3.DISPLAY the stack\ ";
cout<<" Enter your choice... ";
cin>>t;
switch(t)
{
case 1:
cout<<"Enter the information ... ";
cin>>d;
push1(d);
display();
break;
case 2:
if(front == NULL)
{
cout<<" --\-(Underflow)-/-- \n";
}
else
cout<<"\n Element POPED is.. "<<remove()<<endl;
case 3:
display();
break;
default :
cout<<"!!!!!!Enter correct option !!!!!!!";
}
cout<<"CONTINUE y/n ";
cin>>k;
}
break;
}
}
}

You might also like