Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.23 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. const int size=100;
  4. class stack{
  5.     int items[size];
  6.     int top;
  7.     public:
  8.         stack()
  9.         {
  10.             top=-1;
  11.         }
  12.         void push();
  13.         void pop();
  14.         void travers();
  15. };
  16.  
  17. void stack::push()
  18. {
  19.     int item;
  20.     if (top==size-1)
  21.         cout<<"\n the stack is full";
  22.     else
  23.     {
  24.         cout<<"\n enter the element =";
  25.         cin>>item;
  26.         items[++top]=item;
  27.     }
  28. }
  29.  
  30. void stack::pop()
  31. {
  32.     int item;
  33.     if (top==-1)
  34.         cout<<"\n the steack is empty";
  35.     else
  36.     {
  37.         item=items[top--];
  38.         cout<<"\n the deleted elemnt is = "<<item;
  39.     }
  40. }
  41. void stack::travers()
  42. {
  43.     int i;
  44.     if (top==-1)
  45.         cout<<"\n the stack is empty ";
  46.     else
  47.     {
  48.         cout<<"\n\n the elemets in the stack are >>";
  49.         for(i=top;i>=0;i--)
  50.         {
  51.             cout<<endl<<items[i];
  52.         }
  53.     }
  54. }
  55.  
  56. int main()
  57. {
  58.     int choice ;
  59.     char ch;
  60.     stack ps;
  61.     do{
  62.         cout<<"\n1.push ";
  63.         cout<<"\n2.pop";
  64.         cout<<"\n3.traverse ";
  65.         cout<<"\n enter your choice = ";
  66.         cin>>choice;
  67.         switch(choice){
  68.             case 1:
  69.                 ps.push();
  70.                 break;
  71.             case 2:
  72.                 ps.pop();
  73.                 break;
  74.             case 3:
  75.                 ps.travers();
  76.                 break;
  77.             default :
  78.             cout<<"\n you entered wrong choice ";
  79.                
  80.         }
  81.        
  82.         cout<<"\n\npress(Y|y) to continue = ";
  83.         cin>>ch;
  84.        
  85.     }while (ch=='Y'||ch=='y');
  86.     return 0;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement