Thursday, 4 June 2020

stack using c++

#include<iostream>
using namespace std;
struct node
{
int data ;
node *next;
};
class stack
{      // struct node*top;
    node *top;
public:

stack()
{
   top=NULL;
}
void push();
void pop();
void show();
~stack();
};
stack::~stack()
{
node * temp;
temp=top;
while(top!=NULL)
{
top=top->next;
delete temp;
}

}
void stack::push()
{
node *temp;
temp=new node;
cout<<" enter the data to insert\n";
cin>>temp->data;
temp->next=top;
top=temp;
}
void stack::pop()
{
     if(top==NULL)
{
  cout<<" stack is empty\n";
}
     else
{
node *temp;
temp=top;
top=top->next;
cout<<" deleted item is \n"<<temp->data;
delete temp;
}
}

 void stack::show()
 {
node *temp2;
temp2=top;
while(temp2!=NULL)
{
cout<<temp2->data<<"-->";
temp2=temp2->next;
}
cout<<"null\n";
 }



int main()
{
stack s;
int choice;
while(1)
{
cout<<"\t\t\t******MENU********";
cout<<"\n 1 PUSH\n 2 POP \n 3 DISPLAY\n 4 EXIT \n";
cin>>choice;
switch(choice)
{
case 1: s.push();
break;
case 2: s.pop();
break;
case 3: s.show();
break;
case 4: exit(0)  ;
break;
default: cout<<" wrong  choice\n:";
break;
}
}

return 0;
}

No comments:

Post a Comment