Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // C++ program to sort a stack using an
- // auxiliary stack.
- #include <iostream>
- #include<stack>
- #include<queue>
- using namespace std;
- // This function return the sorted stack
- stack<int> swapStack(stack<int> &a,stack<int> &temp)
- {
- while(!a.empty())
- {
- int x = a.top();
- a.pop();
- int y = a.top();
- a.pop();
- temp.push(y);
- temp.push(x);
- }
- return temp;
- }
- int main()
- {
- stack<int>a ;
- a.push(1);
- a.push(2);
- a.push(3);
- a.push(4);
- stack<int> temp;
- swapStack(a,temp);
- while(!temp.empty())
- {
- int ab = temp.top();
- temp.pop();
- cout<<ab<<endl ;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement