-
Notifications
You must be signed in to change notification settings - Fork 0
/
6.cpp
44 lines (37 loc) · 963 Bytes
/
6.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*************************************************************************
> File Name: 6.cpp
> Author: Louis1992
> Mail: [email protected]
> Blog: http://gzc.github.io
> Created Time: Mon Nov 9 13:55:10 2015
************************************************************************/
#include<iostream>
#include<stack>
using namespace std;
void sortStack(stack<int> &mystack) {
stack<int> sorted;
while(!mystack.empty()) {
int v = mystack.top();
mystack.pop();
while(!sorted.empty() && v < sorted.top()) {
int temp = sorted.top();
sorted.pop();
mystack.push(temp);
}
sorted.push(v);
}
mystack = sorted;
}
int main()
{
stack<int> mystack;
mystack.push(3);
mystack.push(1);
mystack.push(2);
sortStack(mystack);
while(!mystack.empty()) {
cout << mystack.top();
mystack.pop();
}
return 0;
}