算法通关村第五关——队列练习

1. 用栈实现队列

LeetCode232 先看题意:

请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push、pop、peek、empty):

实现 MyQueue 类:

void push(int x) 将元素 x 推到队列的末尾

int pop() 从队列的开头移除并返回元素

int peek() 返回队列开头的元素

boolean empty() 如果队列为空,返回 true ;否则,返回 false

思路:

可以定义两个栈,一个栈 dataStack 用来保存 push的元素,另一个栈 outStack 就是将 push的元素全部入栈,这时此栈就相当于队列了

注意点:

只有在用户 pop 操作的时候,才将 dataStack 中的数据全部压入到 outStack 中, 但是如果已经将数据全部压入到 outStack 中时,又有新数据要插入,那么在下次的时候,会立马压入到 outStack pop 操作时会立马取出这个新的数据,出现bug,已经解决了此问题,在 push 操作中添加了判断。

代码:

package com.yugutou.charpter5_queue_map.level2;

import java.util.Stack;

public class StackImplementQueue {
    public Stack<Integer> dataStack;
    public Stack<Integer> outStack;

    public StackImplementQueue() {
        dataStack = new Stack();
        outStack = new Stack();
    }

    public static void main(String[] args) {
        StackImplementQueue queue = new StackImplementQueue();
        queue.push(1);
        queue.push(2);
        queue.push(3);
        queue.push(4);
        queue.push(5);

        System.out.println(queue.pop());
        System.out.println(queue.pop());
        System.out.println(queue.pop());
        System.out.println(queue.pop());
        queue.push(6);
        System.out.println(queue.pop());
        System.out.println(queue.pop());
    }

    /**
     * 新元素入栈
     * @param val
     */
    public void push(int val) {
        /*如果当前插入栈为空,并且 输出栈不为空
        说明已经将全部数据保存进 outStack了,
        但在这时,又有新元素入栈,然后又 pop 操作,那么新元素就会被立马入栈到 outStack顶部,
        在这里判断,将 outStack重新写入到 dataStack中
        */
        if (dataStack.isEmpty() && !outStack.isEmpty()) {
            while (!outStack.isEmpty()) {
                dataStack.push(outStack.pop());
            }
        }
        dataStack.push(val);
    }
    

    public int pop() {
        // 把所有 dataStack 中的元素全部移到 outStack中
        while (!dataStack.isEmpty()) {
            outStack.push(dataStack.pop());
        }

        // outStack 出栈
        return outStack.pop();
    }

    /**
     * 必须要两个栈全为空才是空
     * @return
     */
    public boolean empty() {
        return dataStack.isEmpty() && outStack.isEmpty();
    }
}

 

2. 用队列实现栈

leetcode225 先看题意: 请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(push、top、pop 和 empty)。

实现 MyStack 类: void push(int x) 将元素 x 压入栈顶。 int pop() 移除并返回栈顶元素。 int top() 返回栈顶元素。 boolean empty() 如果栈是空的,返回 true ;否则,返回 false 。

思路:

定义两个队列,queue1 用来存储栈内的主要元素,queue2 用来辅助存储元素

入栈时,先将此元素保存进 queue2 中,然后让 queue1 的元素全部保存在 queue2 的后面,然后 让 queue1 = queue2,最后使 queue2 = null,这样就形成了栈

代码:

 

public class QueueImplementStack {
    public Queue<Integer> queue1;
    public Queue<Integer> queue2;

    public QueueImplementStack() {
        queue1 = new LinkedList<Integer>();
        queue2 = new LinkedList<Integer>();
    }


    public static void main(String[] args) {
        QueueImplementStack stack = new QueueImplementStack();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(4);
        stack.push(5);

        stack.printStack();
    }

    /**
     * 插入元素
     * 每次先往 q2 中写入,然后把 q1 元素全部写入 q2,
     * 然后 使 qi = q2,最后 清空 q2,
     * 这样每次新增元素,都是在最前面
     * @param val
     */
    public void push(int val) {
        // 将元素写入 queue2 中
        queue2.offer(val);

        while (!queue1.isEmpty()) {
            // 将 queue1 元素全部写入 queue2
            queue2.offer(queue1.poll());
        }

        // queue 保存 queue2
        queue1 = queue2;

        // 使 queue2 置空
        queue2 = new LinkedList<>();
    }

    /**
     * 出栈
     * @return
     */
    public int pop() {
        return queue1.poll();
    }

    /**
     * 查看栈顶元素
     * @return
     */
    public int top() {
        return queue1.peek();
    }

    /**
     * 判断栈是否为空
     * @return
     */
    public boolean empty() {
        return queue1.isEmpty();
    }

    public void printStack() {
        while (!queue1.isEmpty()) {
            System.out.print(queue1.poll() + "->");
        }
        System.out.println("null");
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值