Advertisement
skb50bd

Reversing Stack

Dec 11th, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.66 KB | None | 0 0
  1. Reverse All the Elements of a Stack within That Stack... w/ only Stack Operations.
  2.  
  3. #include <iostream>
  4. #include <stack>
  5.  
  6. using namespace std;
  7.  
  8. int main() {
  9.     stack <int> S;
  10.     stack <int> temp;
  11.     stack <int> temp2;
  12.  
  13.     cout << "Enter the Stack Elements (-1 to stop): ";
  14.  
  15.     int n;
  16.     while(true) {
  17.         cin >> n;
  18.         if(n < 0) break;
  19.  
  20.         S.push(n);
  21.     }
  22.  
  23.     while(!S.empty()) {
  24.         temp.push(S.top());
  25.         S.pop();
  26.     }
  27.  
  28.     while(!temp.empty()) {
  29.         temp2.push(temp.top());
  30.         temp.pop();
  31.     }
  32.  
  33.     while(!temp2.empty()) {
  34.         S.push(temp2.top());
  35.         temp2.pop();
  36.     }
  37.  
  38.     return 0;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement