Implement Stack using Queues 解答

Question

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).

Solution

Java Queue Interface

Key to the solution is to use two queues. Queue is strictly First In, First Out.

Two Methods

Method 1: making push operation costly

push(s, x) // x is the element to be pushed and s is stack
  1) Enqueue x to q2
  2) One by one dequeue everything from q1 and enqueue to q2.
  3) Swap the names of q1 and q2 
// Swapping of names is done to avoid one more movement of all elements 
// from q2 to q1. 

pop(s)
  1) Dequeue an item from q1 and return it.

Method 2: making pop operation costly

push(s,  x)
  1) Enqueue x to q1 (assuming size of q1 is unlimited).

pop(s)  
  1) One by one dequeue everything except the last element from q1 and enqueue to q2.
  2) Dequeue the last item of q1, the dequeued item is result, store it.
  3) Swap the names of q1 and q2
  4) Return the item stored in step 2.
// Swapping of names is done to avoid one more movement of all elements 
// from q2 to q1.

 

 1 class MyStack {
 2     Queue<Integer> queue1;
 3     Queue<Integer> queue2;
 4     
 5     public MyStack(){
 6         queue1 = new LinkedList<Integer>();
 7         queue2 = new LinkedList<Integer>();
 8     }
 9     
10     // Push element x onto stack.
11     public void push(int x) {
12         queue1.add(x);
13     }
14 
15     // Removes the element on top of the stack.
16     public void pop() {
17         if (queue1.peek() == null)
18             return;
19         int length = queue1.size();
20         queue2 = new LinkedList<Integer>();
21         while (length > 1) {
22             queue2.add(queue1.remove());
23             length--;
24         }
25         queue1 = queue2;
26     }
27 
28     // Get the top element.
29     public int top() {
30         if (queue1.peek() == null)
31             return -1;
32         queue2 = new LinkedList<Integer>();
33         int length = queue1.size();
34         int lastElement = 0;
35         while (length > 0) {
36             lastElement = queue1.remove();
37             queue2.add(lastElement);
38             length--;
39         }
40         queue1 = queue2;
41         return lastElement;
42     }
43 
44     // Return whether the stack is empty.
45     public boolean empty() {
46         if (queue1.peek() == null)
47             return true;
48         return false;
49     }
50 }

 

posted @ 2015-10-09 02:36  树獭君  阅读(289)  评论(0编辑  收藏  举报