Implement Queue by Two Stacks & Implement Stack using Queues

Implement Queue by Two Stacks

Implement the following operations of a queue using stacks.

  • push(x) -- Push element x to the back of queue.
  • pop() -- Removes the element from in front of queue.
  • peek() -- Get the front element.
  • empty() -- Return whether the queue is empty.
 1 class MyQueue {
 2     
 3     private Stack<Integer> stack1, stack2;
 4 
 5     public MyQueue() {
 6         // do initialization if necessary
 7         stack1 = new Stack<Integer>();
 8         stack2 = new Stack<Integer>();
 9     }
10     
11     // Push element x to the back of queue.
12     public void push(int x) {
13         stack1.push(x);
14     }
15 
16     // Removes the element from in front of queue.
17     public void pop() {
18         if (stack2.isEmpty()) {
19             while(!stack1.isEmpty()) {
20                 stack2.push(stack1.pop());
21             }
22         }
23         stack2.pop();
24     }
25 
26     // Get the front element.
27     public int peek() {
28         if (stack2.isEmpty()) {
29             while(!stack1.isEmpty()) {
30                 stack2.push(stack1.pop());
31             }
32         }
33         return stack2.peek();
34     }
35 
36     // Return whether the queue is empty.
37     public boolean empty() {
38         return stack1.isEmpty() && stack2.isEmpty();
39     }
40 }
Implement Stack using Queues

  Implement the following operations of a stack using queues.

  • push(x) -- Push element x onto stack.
  • pop() -- Removes the element on top of the stack.
  • top() -- Get the top element.
  • empty() -- Return whether the stack is empty.

Notes:

    • You must use only standard operations of a queue -- which means only push to backpeek/pop from frontsize, and is empty operations are valid.
    • Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
    • You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).
分析:

用两个queues,第二个queue永远为空,它只用于临时保存数字而已。

 1 class MyStack {
 2     // Push element x onto stack.
 3     LinkedList<Integer> q1 = new LinkedList<Integer>();
 4     LinkedList<Integer> q2 = new LinkedList<Integer>();
 5     
 6     public void push(int x) {
 7         q1.offer(x);
 8     }
 9 
10     // Removes the element on top of the stack.
11     public void pop() {
12        if (!empty()) {
13            while(q1.size() >= 2) {
14                q2.offer(q1.poll());
15            }
16            q1.poll();
17            
18            while(q2.size() >= 1) {
19                q1.offer(q2.poll());
20            }
21        }  
22     }
23 
24     // Get the top element.
25     public int top() {
26         int value = 0;
27         if (!empty()) {
28            while(q1.size() >= 1) {
29                if (q1.size() == 1) {
30                    value = q1.peek();
31                }
32                q2.offer(q1.poll());
33            }
34            
35            while(q2.size() >= 1) {
36                q1.offer(q2.poll());
37            }
38        }
39        return value;
40     }
41 
42     // Return whether the stack is empty.
43     public boolean empty() {
44         return q1.size() == 0;
45     }
46 }
 
posted @ 2016-07-19 05:41  北叶青藤  阅读(314)  评论(0)    收藏  举报