面试题7用两个栈实现队列

题目意思:两个栈实现队列的appendTail和deleteHead

思路:push到stack1,pop从stack2,stack2为空,就把stack1的挪过来

 1 class Queue{
 2     private:
 3         stack<int> s[2];
 4     public:
 5         void push(int x){
 6             s[0].push(x);
 7         }
 8         void pop(){
 9             if(s[1].empty()){
10                 while(s[0].size()>0){
11                     s[1].push(s[0].top());
12                     s[0].pop();
13                 }
14             }
15             if(s[1].empty()){
16                 cout<<"呵呵";
17                 return;
18             }
19             cout<<(s[1].top());
20             s[1].pop();
21         }
22 };

 

posted @ 2015-06-17 10:23  影翕  阅读(114)  评论(0编辑  收藏  举报