Advertisement
skb50bd

Copying Stack

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