Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

https://www.scribd.

com/doc/40156935/project-onnickel
LINKED STACK
#include<iostream.h>
#include<conio.h>
struct node
{int data;
node *next;};
node *push(node *top,int val)
{node *x=new node;
x->data=val;
x->next=top;
top=x;
return(top);
}
node *pop(node *top,int &val)
{node *x;
if(top==NULL)
{cout<<"Stack empty";
val=-9999;}
else
{x=top;
top=top->next;
val=x->data;
delete x;}
return(top);
}
void showstack(node *top)
{node *ptr=top;
if(ptr==NULL)
cout<<"Stack empty";
else
while(ptr!=NULL)
{cout<<ptr->data<<" ";
ptr=ptr->next;}
}

void main()
{clrscr();
node *top=NULL;
int val,choice;
char ch;
cout<<"MENU";
cout<<"\n1.push";
cout<<"\n2.pop";
cout<<"\n3.display";
do
{cout<<"\nEnter your choice";
cin>>choice;
switch(choice)
{case 1:cout<<"Enter the value to be pushed ";
cin>>val;
top=push(top,val);
break;
case 2:top=pop(top,val);
if(val!=-9999)
cout<<"Popped value is "<<val;
break;
case 3:showstack(top);
}
cout<<"\nDo you wish to continue(y/n)";
cin>>ch;
}while(ch=='y'||ch=='Y');
}

LINKED QUEUE
#include<iostream.h>
#include<conio.h>
struct node
{int data;
node *next;};
node *add(node *rear,int val)
{node *x=new node;
x->data=val;
x->next=NULL;
if(rear!=NULL)
rear->next=x;
rear=x;
return(rear);
}
node *delet(node *front,int &val)
{node *x;
if(front==NULL)
{cout<<"Q empty";
val=-9999;}
else
{x=front;
front=front->next;
val=x->data;
delete x;}
return(front);
}
void showQ(node *front)
{node *ptr=front;
if(ptr==NULL)
cout<<"Q empty";
else
while(ptr!=NULL)
{cout<<ptr->data<<" ";
ptr=ptr->next;}

}
void main()
{clrscr();
node *front=NULL,*rear=NULL;
int val,choice;
char ch;
cout<<"MENU";
cout<<"\n1.insert";
cout<<"\n2.delete";
cout<<"\n3.display";
do
{cout<<"\nEnter your choice";
cin>>choice;
switch(choice)
{case 1:cout<<"Enter the value to be inserted ";
cin>>val;
rear=add(rear,val);
if(front==NULL)
front=rear;
break;
case 2:front=delet(front,val);
if(val!=-9999)
cout<<"Deleted value is "<<val;
if(front==NULL)
rear=front;
break;
case 3:showQ(front);
}
cout<<"\nDo you wish to continue(y/n)";
cin>>ch;
}while(ch=='y'||ch=='Y');
}

You might also like