#include<iostream>
#include<cstdlib>
using namespace std;
template<class T>
class stacks
{
public:
struct node
{
node *link;
T num;
};
node *top;
stacks()
{
top=NULL;
}
void push(T);
bool pop();
void display();
};
template<class T>
void stacks<T>::push(T ele)
{
node *t=new node;
t->num=ele;
if(top==NULL)
{
top=t;
t->link=NULL;
}
else
{
t->link=top;
top=t;
}
}
template<class T>
bool stacks<T>::pop()
{
node *t;
node *p;
if(top==NULL)
return 0;
else
{
t=new node;
t=top;
top=top->link;
delete t;
return 1;
}
return 0;
}
template<class T>
void stacks<T>::display()
{
node *t=new node;
t=top;
while(t!=NULL)
{
cout<<t->num<<endl;
t=t->link;
}
}
int main()
{
stacks<int>ob;
int op,ele;
while(1)
{
cout<<"\nMenu is ";
cout<<"\n1.Push \n2.Pop \n3.Display \n4.Exit";
cout<<"\nYour choice is ";
cin>>op;
switch(op)
{
case 1:
{
cout<<"\nEnter element to be pushed";
cin>>ele;
ob.push(ele);
}
break;
case 2:
{
int t;
t=ob.pop();
if(t==true)
cout<<"\nPop Successful";
else
cout<<"\nPop unsuccessful";
}
break;
case 3:
ob.display();
break;
case 4:
exit(1);
}
}
}
No comments:
Post a Comment