#include<iostream>
using namespace std;
class stack
{private:
struct node
{int element;
node* next;
}*top;
public:
stack();
void push(int element);
int pop();
bool isempty();
void display();
~stack();
};
stack::stack()
{
top=NULL;
}
bool stack::isempty()
{if(top==NULL)
return true;
else return false;}
void stack::push(int element)
{if(isempty()){top=new node;
top->element=element;
top->next=NULL; return;}
node* temp=new node;
temp->element=element;
temp->next=top;
top=temp;
return;}
int stack::pop()
{if(isempty())
{cout<<"\n stack is empty ";
return -10000;
}
node*t=top;
top=top->next;
int data=t->element;
delete(t);
return data;
}
void stack::display()
{
if(isempty()){cout<<"\n stack is empty ";
return;}
node* curr=top;
cout<<endl;
while(curr!=NULL)
{cout<<"\n\t"<<curr->element<<"\n\t^\n\t|";
curr=curr->next;}}
stack::~stack()
{while(top!=NULL){
node* t=top;
top=top->next;
delete(t);
}}
int main()
{stack s;
char c;
int a;
cout<<"\t\t***************welcome to stack operation********\n";
do
{
cout<<"\t\t\tpress 1 to push element\n\t\t\tpress 2 to pop element\n\t\t\tpress 3 to display element\n";
cout<<"\n select operation you want to do\t";
cin>>a;
switch(a)
{case 1:cout<<"\n enter the element you want to push is stack\t";
int b;
cin>>b;
s.push(b);break;
case 2:int d;
d=s.pop();
if(d!=-10000)
cout<<"\n the element poped is\t"<<d;
break;
case 3:
cout<<"\n--------------------------------------------------------------\n";
s.display();
cout<<"\n-------------------------------------------------------------\n"; break;
defoult: cout<<"\n select correct operation\n";}
cout<<"\n do you want to continue is yes press y\n";
cin>>c;
}while(c=='y');
return 0;}
No comments:
Post a Comment