Implement Queue using Stacks

 1 class Queue {
 2     stack<int> input,output;
 3 public:
 4     // Push element x to the back of queue.
 5     void push(int x) {
 6         input.push(x);
 7         
 8     }
 9 
10     // Removes the element from in front of queue.
11     void pop(void) {
12       peek();
13       output.pop();
14     }
15 
16     // Get the front element.
17     int peek(void) {
18         if(output.empty())
19         {
20             while(!input.empty())
21             {
22             output.push(input.top());
23             input.pop();
24             }
25         }
26         return output.top();
27         
28     }
29 
30     // Return whether the queue is empty.
31     bool empty(void) {
32         if(input.empty()&&output.empty())
33         return true;
34         
35     }
36 };

 

posted on 2015-10-23 10:42  RenewDo  阅读(99)  评论(0编辑  收藏  举报

导航