-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsortastack.cpp
61 lines (60 loc) · 943 Bytes
/
sortastack.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include<bits/stdc++.h>
using namespace std;
class SortedStack{
public:
stack<int> s;
void sort();
};
void printStack(stack<int> s)
{
while (!s.empty())
{
printf("%d ", s.top());
s.pop();
}
printf("
");
}
int main()
{
int t;
cin>>t;
while(t--)
{
SortedStack *ss = new SortedStack();
int n;
cin>>n;
for(int i=0;i<n;i++)
{
int k;
cin>>k;
ss->s.push(k);
}
ss->sort();
printStack(ss->s);
}
}
}
/*This is a function problem.You only need to complete the function given below*/
/*The structure of the class is
class SortedStack{
public:
stack<int> s;
void sort();
};
*/
/* The below method sorts the stack s
you are required to complete the below method */
void SortedStack :: sort()
{
//Your code here
int n = s.size();
vector<int> v;
while(!s.empty())
{
v.push_back(s.top());
s.pop();
}
std::sort(v.begin(),v.end());
for(int i=0;i<n;i++) s.push(v[i]);
}