forked from OpenGenus/cosmos
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from HarshitaSahai/problem6
Create sortastack.cpp
- Loading branch information
Showing
1 changed file
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]); | ||
} |