Advertisement
saske_7

class_stack.cpp

Nov 3rd, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.51 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std ;
  4.  
  5. #define M 500
  6. class _stack
  7. {
  8.  
  9.     int arr[M], top ;
  10. public:
  11.     _stack()
  12.     {
  13.         top   = -1;
  14.     };
  15.     void push(int a)
  16.     {
  17.         if(top >  M )
  18.         {
  19.             cout <<"overflow\n";
  20.             return ;
  21.         }
  22.  
  23.         top++;
  24.         arr[top ] =  a;
  25.         cout << "Item inserted to stack : " << a << endl ;
  26.  
  27.     }
  28.     void pop(void )
  29.     {
  30.         if(top == -1)
  31.         {
  32.             cout << "Underflow\n";
  33.             return ;
  34.  
  35.         }
  36.         cout<< "Deleting item : " << arr[top ] << endl;
  37.         top-- ;
  38.  
  39.  
  40.     }
  41.     void _top(void )
  42.     {
  43.         if(top ==  -1  )
  44.             cout << "Stack empty" << endl;
  45.         else
  46.             cout << "Stack top : " << arr[top ] << endl ;
  47.     }
  48.  
  49.  
  50. };
  51.  
  52. void input()
  53. {
  54.     _stack stk ;
  55.  
  56.     int i, j,k ;
  57.     while(1)
  58.     {
  59.         int choice, item ;
  60.         cout << "delete(0 ) / insert (1) / show(3) /  : " ;
  61.         cin >> choice ;
  62.         if(choice ==  1)
  63.         {
  64.             cout <<"Enter item : ";
  65.             cin >> item ;
  66.             stk.push(item );
  67.  
  68.  
  69.         }
  70.         else if(choice ==  0) stk.pop();
  71.         else if(choice ==  3) stk._top();
  72.         cout <<"\n" << endl;
  73.     }
  74.  
  75.  
  76. }
  77.  
  78. int main()
  79. {
  80.  
  81.     input();
  82.     /*
  83.         _queue q ;
  84.         stk.push(11);
  85.         stk.push(12);
  86.         stk.push(13);
  87.         stk.front();
  88.         stk.pop();
  89.         stk.pop();
  90.         stk.pop();
  91.         stk.pop();
  92.  
  93.     */
  94.  
  95.     return 0 ;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement