Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1.  
  2. // C++ program to sort a stack using an
  3. // auxiliary stack.
  4. #include <iostream>
  5. #include<stack>
  6. #include<queue>
  7. using namespace std;
  8.  
  9. // This function return the sorted stack
  10. stack<int> swapStack(stack<int> &a,stack<int> &temp)
  11. {
  12.  
  13.  
  14. while(!a.empty())
  15. {
  16. int x = a.top();
  17. a.pop();
  18. int y = a.top();
  19. a.pop();
  20.  
  21. temp.push(y);
  22. temp.push(x);
  23.  
  24.  
  25. }
  26. return temp;
  27.  
  28. }
  29.  
  30.  
  31.  
  32. int main()
  33. {
  34. stack<int>a ;
  35.  
  36. a.push(1);
  37. a.push(2);
  38. a.push(3);
  39. a.push(4);
  40.  
  41. stack<int> temp;
  42. swapStack(a,temp);
  43.  
  44.  
  45. while(!temp.empty())
  46. {
  47. int ab = temp.top();
  48. temp.pop();
  49. cout<<ab<<endl ;
  50. }
  51.  
  52.  
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement