extra practice stacks

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
700 B
2
Indexable
Never
#include <iostream>
#include <stack>
#include <list>
using namespace std;
void printStack(stack<int>s){
    while(!s.empty()){
        cout<<s.top()<<" ";
        s.pop();
    }
}
stack<int> sortedStack(stack <int> s1){
    list<int> list1;
    stack<int>s2;
    while(!s1.empty()){
        list1.push_back(s1.top());
        s1.pop();
    }
    list1.sort();
    for (int i : list1){
        s1.push(i);
    }
    while(!s1.empty()){
        s2.push(s1.top());
        s1.pop();
    }
    return s2;
}
int main() {
    stack<int>st;
    int n,numbers;
    cin>>n;
    for(int i = 0; i < n; i++){
        cin>>numbers;
        st.push(numbers);
    }
    printStack(sortedStack(st));

    return 0;
}