-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort-Stack.cpp
More file actions
52 lines (40 loc) · 772 Bytes
/
Copy pathsort-Stack.cpp
File metadata and controls
52 lines (40 loc) · 772 Bytes
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
45
46
47
48
49
50
51
52
#include<iostream>
#include<stack>
using namespace std;
void sortedInsert(stack<int>& s, int topelement)
{
if(s.empty() || s.top()<topelement)
{
s.push(topelement);
return;
}
int element = s.top();
s.pop();
sortedInsert(s, topelement);
s.push(element);
}
void sortStack(stack<int>& s)
{
if(s.empty())
return;
int topelement = s.top();
s.pop();
sortStack(s);
sortedInsert(s, topelement);
}
int main()
{
stack<int> s;
s.push(4);
s.push(7);
s.push(1);
s.push(0);
sortStack(s);
cout<<"Sorted stack:"<<endl;
while (!s.empty())
{
cout<<s.top()<<" ";
s.pop();
}
return 0;
}