美文网首页
用两个队列实现栈

用两个队列实现栈

作者: Rarestzhou | 来源:发表于2018-09-08 09:37 被阅读0次

思路:

  1. 先往 queue1 顺次插入1,2,3,4,5,此时按照栈的规则应先出来 5,所以先将1,2,3,4 出队列 queue1,并入队列 queue2,5 出队列,queue2 当前状态是:1,2,3,4,queue1当前状态为空;
  2. 将1,2,3入队列 queue1,4 出队列,此时,queue2 为空,queue1 有 1,2,3...
  3. 重复以上步骤

代码实现:

/**
 * 题目类型:队列
 *
 * 题目要求:用两个队列实现栈
 *
 * 思路:1. 先往 queue1 顺次插入1,2,3,4,5,此时按照栈的规则应先出来 5,所以先将1,2,3,4 出队列 queue1,并入队列 queue2,5 出队列
 *  queue2 当前状态是:1,2,3,4,queue1当前状态为空;
 *  2. 将1,2,3入队列 queue1,4 出队列,此时,queue2 为空,queue1 有 1,2,3...
 *  3. 重复以上步骤
 */
public class QueueToStack {

    private Queue<Integer> queue1 = new ArrayDeque<Integer>();

    private Queue<Integer> queue2 = new ArrayDeque<Integer>();

    /**
     *  向栈中压入数据
     * @param data
     */
    private void push(Integer data) {
        if (queue1.isEmpty() && queue2.isEmpty()) {
            queue1.add(data);
        }

        //如果queue1为空,queue2有数据,直接放入queue2
        if (queue1.isEmpty()) {
            queue2.add(data);
        }

        if (queue2.isEmpty()) {
            queue1.add(data);
        }
    }

    /**
     * 从栈中弹出数据
     * @return
     */
    private Integer pop() throws Exception {
        if (queue1.isEmpty() && queue2.isEmpty()) {
            throw new Exception("stack is empty");
        }

        //如果queue1中没有元素,queue2中有元素,将queue2中的元素依次放入queue1中,弹出最后一个元素
        if(queue1.isEmpty()){
            while(queue2.size() > 1){
                queue1.add(queue2.poll());
            }
            return queue2.poll();
        }

        //如果queue2中没有元素,queue1中有元素,将其queue1中的元素依次放入queue2中,弹出最后一个元素
        if(queue2.isEmpty()){
            while(queue1.size() > 1){
                // poll() Retrieves and removes the head of this queue 若队列为空,则返回 null
                // peek() Retrieves, but does not remove, the head of this queue
                queue2.add(queue1.poll()); 
            }
            return queue1.poll();
        }

        return null;
    }

    @Test
    public void testQueue() throws Exception {
        QueueToStack queueToStack = new QueueToStack();
        queueToStack.push(1);
        queueToStack.push(0);
        queueToStack.push(6);
        queueToStack.push(9);
        System.out.println(queueToStack.pop());
        System.out.println(queueToStack.pop());

        queueToStack.push(2);
        System.out.println(queueToStack.pop());
        System.out.println(queueToStack.pop());
        System.out.println(queueToStack.pop());
    }
}

相关文章

  • 面试题9: 用两个栈实现队列

    9-1 用两个栈实现队列 9-2 用两个队列实现栈

  • 栈&队列

    一、栈&队列总结 栈/队列的应用接雨水验证栈序列滑动窗口的最大值 栈/队列的特殊实现用两个栈实现队列用两个队列实现...

  • 用两个栈实现队列,用两个队列实现堆栈

    参考:剑指Offer面试题7(Java版):用两个栈实现队列与用两个队列实现栈 用两个栈实现队列stack1作为入...

  • 手撕栈队列

    【面试题07:用两个栈实现队列】 题目:利用两个栈实现队列的插入,取队首,判断非空等函数。拓展:用两个队列实现栈,...

  • 数据结构——栈和队列

    用数组实现栈和队列 用栈实现队列 用队列实现栈 栈和队列的经典算法题最小间距栈宠物收养所 数组实现栈和队列 用数组...

  • LeetCode 每日一题 [43] 用两个栈实现队列

    LeetCode 用两个栈实现队列 [简单] 用两个栈实现一个队列。队列的声明如下,请实现它的两个函数 appen...

  • 总结的笔试/面试算法题

    目录 1. 栈和队列1.用两个队列实现栈2.用两个栈实现队列3.实现一个栈,可以用常数级时间找出栈中的最小值4.判...

  • 剑指Offer

    09 用两个栈实现队列 用两个栈实现一个队列。队列的声明如下,请实现它的两个函数 appendTail 和 del...

  • 剑指Offer(五)

    剑指Offer(五) 用两个栈实现队列 题目描述: 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列...

  • LeetCode题解之用两个栈实现队列

    用两个栈实现队列 题目描述 用两个栈实现一个队列。队列的声明如下,请实现它的两个函数 appendTail 和 d...

网友评论

      本文标题:用两个队列实现栈

      本文链接:https://www.haomeiwen.com/subject/pxjkgftx.html