Advertisement
Centipede18

stack

Apr 14th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.64 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. main()
  5. {
  6.     stack <int> S;
  7.     while(1)
  8.     {
  9.         string s;
  10.         cin>>s;
  11.         if(s == "end") break;
  12.         if(s == "pop")
  13.         {
  14. //          if(S.empty() == 0) cout<<S.top();
  15. //          cout<<endl;
  16.             if(!S.empty())S.pop();
  17.         }
  18.         if(s == "init")
  19.         {
  20.             for(int i = S.size()-1; i >= 0; i--)
  21.             {
  22.                 S.pop();
  23.             }
  24.         }
  25.         if(s == "empty")
  26.         {
  27.             if(!S.empty()) cout<<"0";
  28.             else cout<<"1";
  29.             cout<<endl;
  30.         }
  31.         if(s == "push")
  32.         {
  33.             int x;
  34.             cin>>x;
  35.             S.push(x);
  36.         }
  37.         if(s == "top")
  38.         {
  39.             if(!S.empty()) cout<<S.top();
  40.             else cout<<"-1";
  41.             cout<<endl;
  42.         }
  43.         if(s == "size") cout<<S.size()<<endl;
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement