Website Summary Files Commits Branches @commit @diff @stats
path: root / Seminar02 / bonusTask01.cpp
blob: 6e4101691baaad2ab2bb335c3e3b0d3b2395357d (plain) (blame)
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
#include <iostream>
#include <stack>

void sortStack(std::stack<int> &first)
{
    std::stack<int> second;
    int currentElement = 0;
    while (!first.empty())
    {
        currentElement = first.top();
        first.pop();

        while (!second.empty() && second.top() > currentElement)
        {
            first.push(second.top());
            second.pop();
        }
        second.push(currentElement);
    }

    while (!second.empty())
    {
        first.push(second.top());
        second.pop();
    }
}

void stackPrint(std::stack<int> st)
{
    while (!st.empty())
    {
        std::cout << st.top() << " ";
        st.pop();
    }
    std::cout << std::endl;
}

int main()
{
    std::stack<int> st;
    st.push(8);
    st.push(2);
    st.push(4);
    st.push(1);
    st.push(5);
    st.push(3);

    stackPrint(st);
    sortStack(st);
    stackPrint(st);
}