两个栈实现一个顺序栈

来源:互联网 发布:电影照片是什么软件 编辑:程序博客网 时间:2024/06/11 10:17

#include <iostream>

#include <stack>

usingnamespacestd;

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(); return0;}

0 0