zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    java入门经验分享——记面向对象先导课程学习感想
    HashCode方法整理
    Java中vector用法整理
    Java中Iterator用法整理
    org.springframework.data.redis.RedisConnectionFailureException
    dubbo服务启动正常,但是访问不到服务,在监测中心也找不服务的原因之一
    【转】Elasticsearch Java Rest Client 指南
    【转】mybatis根据mapper执行sql的过程
    转:IDEA异常解决: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
    ES的常用查询与聚合
  • 原文地址:https://www.cnblogs.com/ireneyanglan/p/4862789.html
Copyright © 2011-2022 走看看