//**************** stack implimentation using linked lists************//
#include<iostream>
using namespace std;
class stack
{
public:
int data;
stack* link;
stack(int d)
{
data=d;
link=NULL;
}
};
void display(stack* &top)
{
stack* temp=top;
temp=top;
while(temp->data!=NULL)
{
cout<<temp->data<<"<--";
temp=temp->link;
}
cout<<endl;
}
void pop(stack* &top)
{
stack*temp=top;
temp=top;
if(top==NULL)
{
cout<<"Cant b poped"<<endl;
}
else
{
temp=top;
top=top->link;
cout<<temp->data<<"poped";
delete temp;
}
cout<<"poped"<<endl;
display(top);
}
void push(stack* &top)
{
int data;
cout<<"Enter the data into stack"<<endl;
cin>>data;
stack* n=new stack(data);
if(top==NULL)
{
top=n;
}
else
{
n->link=top;
top=n;
cout<<data<<" pushed to stack"<<endl;
}
}
void peek(stack* & top)
{
if(top==NULL)
{
cout<<" top is empty"<<endl;
}
else
{
cout<<top->data<<":is peak element "<<endl;
}
display(top);
}
int main()
{
stack*top=NULL;
int ch;
do
{
cout<<"Enter your choicce"<<endl;
cout<<"\n 1=push \n 2= pop \n 3= peek \n 4= display"<<endl;
cin>>ch;
switch(ch)
{
case 1:
push(top);
break;
case 2:
pop(top);
break;
case 3:
peek(top);
break;
case 4:
display(top);
break;
}
cout<<"MAIn: press 1 to cntinue"<<endl;
cin>>ch;
}while(ch==1);
return 0;
}
0 comments:
Post a Comment