算法:只用最少栈结构构造顺序栈

#include <iostream>
#include <stack>
using namespace std;

class STACK
{
public:
    void Push(int val)
    {
        stack<int> temp;

        temp.push(val);

        while (st.empty() == false)
        {
            if (st.top() > temp.top())
            {
                while (st.empty() == false)
                {
                    temp.push(st.top());
                    st.pop();
                }
            }
            else
            {
                int num = temp.top();
                temp.pop();
                temp.push(st.top());
                st.pop();
                temp.push(num);
            }
        }
        while (temp.empty() == false)
        {
            st.push(temp.top());
            temp.pop();
        }
    }
    void Pop()
    {
        st.pop();
    }
    int Top()
    {
        return st.top();
    }
    void Printf()
    {
        stack<int> temp = st;
        while (temp.empty() == false)
        {
            cout << temp.top()<<" ";
            temp.pop();
        }
        cout << endl;
    }
private:
    stack<int> st;
};
int main()
{
    STACK st;
    st.Push(5);
    st.Push(1); 
    st.Push(6);
    st.Push(0);
    st.Push(9);
    st.Push(3);
    st.Printf();
    return 0;
}

《算法:只用最少栈结构构造顺序栈》

点赞