用队列实现stack

用queue实现stack

题目要求: Implement the following operations

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

其中 use only standard operations of a queue -- which means only push to backpeek/pop from frontsize, and is emptyoperations 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).

 

方法1:用两个queue

用两个queue的话很容易实现,类似于用stack实现queue的思路。其中一个queue主要用于改变element顺序时,临时存储element。

Solution 1 : making pop operation costly

pop和top时,因为需要取最后一个,需要把q中前面的移动到q2中。

In push operation, the new element is always enqueued to q1.
In pop() operation, if q2 is empty then all the elements except the last, are moved to q2.
Finally the last element is dequeued from q1 and returned.

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.

class MyStack {
    //update when pop and top operation
    //solution 2: can update when push
    Queue<Integer> q = new LinkedList<Integer>();
    Queue<Integer> q2 = new LinkedList<Integer>();
    
    // Push element x onto stack.
    public void push(int x) {
        q.offer(x);
    }

    // Removes the element on top of the stack.
    public void pop() {
        while(q.size() > 1){//all element to q2 except last element
            q2.offer(q.poll());
        }
        
        int last= q.poll();
        //no need to push back, can just swap name of the two queue
        q = q2;
        q2 = new LinkedList<Integer>();
        
    }

    // Get the top element.
    public int top() {
         
         while(q.size() > 1){//all element to q2 except last element
           
            q2.offer(q.poll());
        }
        int last = q.poll();
        q2.offer(last);
        //no need to push back, can just swap name of the two queue
        q = q2;
        q2 = new LinkedList<Integer>();
        return last;
    }

    // Return whether the stack is empty.
    public boolean empty() {
        return q.isEmpty();
    }
}
View Code

 

Solution 2: Make push operation costly

push添加元素x的时候,加到q2,然后再把q中剩余添加到q2中x后面,这样就实现了LIFO。

This method makes sure that newly entered element is always at the front of 'q1',
so that pop operation just dequeues from 'q1'. 'q2' is used to put every new element at front of 'q1'.

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.

 

class MyStack {
    //update when push
   
    Queue<Integer> q = new LinkedList<Integer>();
    Queue<Integer> q2 = new LinkedList<Integer>();
    
    // Push element x onto stack.
    public void push(int x) {
        q2.offer(x);
        while(!q.isEmpty()){
            q2.offer(q.poll());
        }
        //swap q and q2 name or push back to q
        while(!q2.isEmpty()){
            q.offer(q2.poll());
        }
    }

    // Removes the element on top of the stack.
    public void pop() {
        q.poll();
    }

    // Get the top element.
    public int top() {
        return q.peek();
    }

    // Return whether the stack is empty.
    public boolean empty() {
        return q.isEmpty();
    }
}
View Code

方法2:使用一个queue

在方法1中的push costly方法的基础上优化,减少空间

class MyStack {
    //update when push
   
    Queue<Integer> q = new LinkedList<Integer>();
    
    // Push element x onto stack.
    public void push(int x) {
        q.offer(x);
        int size = q.size();
        while(size > 1){
            q.offer(q.poll());
            size--;
        }
        
    }

    // Removes the element on top of the stack.
    public void pop() {
        q.poll();
    }

    // Get the top element.
    public int top() {
        return q.peek();
    }

    // Return whether the stack is empty.
    public boolean empty() {
        return q.isEmpty();
    }
}

在pop and top costly 方法基础上优化,用一个元素mark peek即可

class MyStack {
    //update when pop and top operation
    //use 1 queue
    Queue<Integer> q = new LinkedList<Integer>();
    
    private Integer topElement = null;
    
    // Push element x onto stack.
    public void push(int x) {
        topElement = x;
        q.offer(x);
    }

    // Removes the element on top of the stack.
    public void pop() {
        int size = q.size();
        Integer second = null;
        while(size > 1){//all element to q2 except last element
            second = q.poll();
            q.offer(second);
            size--;
        }
        topElement = second;//the second last element
        q.poll();//pop the last element
        
    }

    // Get the top element.
    public int top() {
       return topElement;
    }

    // Return whether the stack is empty.
    public boolean empty() {
        return q.isEmpty();
    }
}

 

posted @ 2015-06-13 06:59  xiaomaoliu  阅读(206)  评论(0编辑  收藏  举报