Friday, October 19, 2007

3.implementing stack using linked list

/*implementing stack using linked list*/
#include<iostream.h>
#include<conio.h>
#include<process.h>
template<class t>
class stack
{
struct node1
{t sno;struct node1 *next;};
typedef node1 node;

node *top;
node *create(){ return new node;}
public:stack(){top=NULL;}
void push(t ele)
{
node *temp=create();
temp->sno=ele;
temp->next=top;
top=temp;
cout<<"the element inserted is:"<<temp->sno;}
void pop()
{
node *temp;
temp=top;
if(temp==NULL)
{
cout<<"\nunderflow";
getch();
}
else
{
top=top->next;
cout<<"\nthe deleted element is:"<<temp->sno;
delete temp ;
}
return;
}
void display()
{
node *temp;
temp=top;
if(top==0)
cout<<"\nqueue empty..";
else
{
cout<<"the elements:";
while(temp!=NULL)
{
cout<<"\n"<<endl<<temp->sno;
temp=temp->next;
}
}
}
};
void main()
{
stack<int> s;
int choice;
clrscr();
do
{
cout<<"\n 1.push\n2.pop\n3.display\n4.exit\n";
cout<<"Enter your coice:";
cin>>choice;
switch(choice)
{
case 1:int ele;
cout<<"enter element";
cin>>ele;
s.push(ele);
break;
case 2:s.pop();
break;
case 3:s.display();
break;
case 4:exit(0);
break;
}
getch();
}
while(1);
}

No comments: