用俩个队列实现栈的功能

队列实现栈

1.解题思路

  • 首先明确栈和队列分别有什么特点:栈是先进后出队列是先进先出
  1. 假设现在有qu1和qu2俩个队列;
  2. 现在你有一组数字[12,23,45,55,35];
  3. 你先将12,23,34进入队列qu2,因为实现的是栈的功能,现在要进行出栈操作,栈是后进先出,那么应该最先出去的是34;
  4. 但是现在34在队尾,所以你是不是要先把12,34先移动到旁边那个队列qu1这样,将qu2剩余的34直接弹出就可以;
  5. 假设你现在要弹23,同样的道理;(前提是另外一个队列为空)
  6. 假设你弹出34之后,没有弹出23,而是继续往进去加数字,那么应该加入哪个队列;
  7. 现在qu1有数字,那么你就加入到qu1中(哪个队列不为空就加入到哪一个中);
    在这里插入图片描述

2.具体实现

2.1入队
//入队
    public void push(int x) {
        //1、谁不为空  入到哪个队列就好了
        //2、两个都为空   放到第一个qu1当中
        if (!qu1.isEmpty()){
            qu1.offer(x);
        }else if (!qu2.isEmpty()){
            qu2.offer(x);
        } else {
            qu1.offer(x);
        }
        usedSize++;
    }
2.2出队
在这里插入代码片//出队
    public int pop() {
        //1.先将前面size-1个移动到空的队列
        //2.将队列最后一个弹出,就是类似于出栈顺序
        //3.反复进行,直到俩个队列都为空的时候出队完毕
        if (empty()){//俩个队列都空
            return -1;
        }
        int size = usedSize;
        if (!qu1.isEmpty()){
            for (int i = 0; i < size-1; i++) {
                qu2.offer(qu1.poll());
            }
            usedSize--;
            return qu1.poll();
        }else{

            for (int i = 0; i < size-1; i++) {
                qu1.offer(qu2.poll());
            }
            usedSize--;
            return qu2.poll();
        }

    }
2.3弹栈顶
 //弹栈顶
    public int top() {
        if (empty()){//俩个队列都空
            return -1;
        }
        int tmp = -1;
        int size = usedSize;
        if (!qu1.isEmpty()){
            for (int i = 0; i < size ; i++) {
                tmp = qu1.poll();
                qu2.offer(tmp);
            }
        }else{
            for (int i = 0; i < size; i++) {
                tmp = qu2.poll();
                qu1.offer(tmp);
            }
        }
        return tmp;
    }
2.4判断是否为空
public boolean empty() {
        if(qu1.isEmpty() && qu2.isEmpty()){
            return true;
        }
        return false;
    }

3.完整代码

import java.util.LinkedList;
import java.util.Queue;

//用队列实现栈(需要俩个队)
class MyStack {

    private Queue<Integer> qu1;
    private Queue<Integer> qu2;
    public int usedSize;


    /** Initialize your data structure here. */
    //构造方法
    public MyStack() {
        qu1 = new LinkedList<>();
        qu2 = new LinkedList<>();

    }

    /** Push element x onto stack. */
    //入队
    public void push(int x) {
        //1、谁不为空  入到哪个队列就好了
        //2、两个都为空   放到第一个qu1当中
        if (!qu1.isEmpty()){
            qu1.offer(x);
        }else if (!qu2.isEmpty()){
            qu2.offer(x);
        } else {
            qu1.offer(x);
        }
        usedSize++;
    }

    /** Removes the element on top of the stack and returns that element. */
    //出队
    public int pop() {
        //1.先将前面size-1个移动到空的队列
        //2.将队列最后一个弹出,就是类似于出栈顺序
        //3.反复进行,直到俩个队列都为空的时候出队完毕
        if (empty()){//俩个队列都空
            return -1;
        }
        int size = usedSize;
        if (!qu1.isEmpty()){
            for (int i = 0; i < size-1; i++) {
                qu2.offer(qu1.poll());
            }
            usedSize--;
            return qu1.poll();
        }else{

            for (int i = 0; i < size-1; i++) {
                qu1.offer(qu2.poll());
            }
            usedSize--;
            return qu2.poll();
        }

    }

    /** Get the top element. */
    //弹栈顶
    public int top() {
        if (empty()){//俩个队列都空
            return -1;
        }
        int tmp = -1;
        int size = usedSize;
        if (!qu1.isEmpty()){
            for (int i = 0; i < size ; i++) {
                tmp = qu1.poll();
                qu2.offer(tmp);
            }
        }else{
            for (int i = 0; i < size; i++) {
                tmp = qu2.poll();
                qu1.offer(tmp);
            }
        }
        return tmp;
    }

    /** Returns whether the stack is empty. */
    public boolean empty() {
        if(qu1.isEmpty() && qu2.isEmpty()){
            return true;
        }
        return false;
    }
}


public class TestDemo2 {
    public static void main(String[] args) {
        MyStack myStack = new MyStack();
        myStack.push(10);
        myStack.push(20);
        myStack.push(30);
        System.out.println(myStack.top());
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值