Shirlies
宁静专注认真的程序媛~

题目

用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。

链接

http://www.nowcoder.com/practice/54275ddae22f475981afa2244dd448c6?tpId=13&tqId=11158&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking

代码

 1 class Solution
 2 {
 3 public:
 4     void push(int node) {
 5         this->stack1.push(node);
 6     }
 7 
 8     int pop() {
 9         if(stack2.empty()){
10             while(!stack1.empty()){
11                 stack2.push(stack1.top());
12                 stack1.pop();
13             }
14         }
15         //如果两个栈都为空,应该要报错,如何报错,题目木有说
16         
17         int ans = stack2.top();
18         stack2.pop();
19         return ans;
20     }
21 
22 private:
23     stack<int> stack1;
24     stack<int> stack2;
25 };
View Code

 

posted on 2016-08-15 14:55  Shirlies  阅读(154)  评论(0)    收藏  举报