Skip to content

Commit

Permalink
Merge pull request #10 from HarshitaSahai/problem6
Browse files Browse the repository at this point in the history
Create sortastack.cpp
  • Loading branch information
HarshitaSahai committed Nov 3, 2019
2 parents d27a559 + 03e4bd2 commit 3e4c42a
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions code/online_challenges/src/GeeksForGeeks/sortastack.cpp
@@ -0,0 +1,60 @@
#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]);
}

0 comments on commit 3e4c42a

Please sign in to comment.