Advertisement
Guest User

Stack

a guest
Nov 28th, 2014
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 KB | None | 0 0
  1. # include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. struct stack {
  6.     int top=0;
  7.     int arr[100];
  8.  
  9.     void push( int num ) {
  10.         if( top<100 ) arr[top++] = num;
  11.         else cout << "Stack Overflow" << endl;
  12.     }
  13.  
  14.     int pop() {
  15.         if( top ) return arr[--top];
  16.         else {
  17.             cout << "Stack Underflow" << endl;
  18.             return -1;
  19.         }
  20.     }
  21. };
  22.  
  23. int main() {
  24.     struct stack int_stack;
  25.     int cmd, num;
  26.  
  27.     cout << "1 - push\n2 - pop\n0 - Quit" << endl;
  28.     while( true ) {
  29.         cout << "cmd: ";
  30.         cin>>cmd;
  31.         if( !cmd ) break;
  32.         switch(cmd) {
  33.         case 1:
  34.             cout << "Push: ";
  35.             cin>> num;
  36.             int_stack.push(num);
  37.             break;
  38.         case 2:
  39.             cout << "Pop: ";
  40.             num = int_stack.pop();
  41.             if( num!=-1 ) cout << num << endl;
  42.             break;
  43.         }
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement