Leetcode 232. 用栈实现队列

class MyQueue {
    private Stack<Integer> stacka = new Stack<Integer>();
    private Stack<Integer> stackb = new Stack<Integer>();
    /** Initialize your data structure here. */
    public MyQueue() {

    }
    /*
 2.总结操作就是:
            *  入队:将元素进栈A
            *  出队:判断栈B是否为空,如果为空,则将栈A中所有元素pop,并push进栈B,栈B出栈;如果不为空,栈B直接出栈。
    */
    /** Push element x to the back of queue. */
    public void push(int x) {
        stacka.push(x);
    }

    /** 从队列的开头移除并返回元素 */
    /*
    Stack.peek() 函数返回栈顶的元素,但不弹出该栈顶元素。
    Stack.pop() 函数返回栈顶的元素,并且将该栈顶元素出栈。
     */
    public int pop() {
        //如果stackb不为空 返回stackb的栈顶元素
        int peekvalue=-1;
        if(!this.empty()){//队列不空,stackb不空
            peekvalue = stackb.pop();
        }
        return peekvalue;
    }
    /**  返回队列开头的元素. */
    public int peek() {
        //如果stackb不为空 返回stackb的栈顶元素
        int peekvalue=-1;
        if(!this.empty()){//队列不空,stackb不空
            peekvalue = stackb.peek();
        }
        return peekvalue;
    }

    /** 队空判断实际也是stackb 是否empty的判断. */
    public boolean empty() {
        //stacka--->stackb
        while (!stacka.isEmpty()){
            int value = stacka.pop();
            stackb.push(value);
        }
        if (stackb.isEmpty())
            return true;//空的
        else
            return false;
    }
}

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue obj = new MyQueue();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.peek();
 * boolean param_4 = obj.empty();
 */

错误代码段分析:

push 1 2 3 4

pop 1

push 5     此时的stackb 2345

pop 5      此时pop 结果是5而不是预想中的2

解决办法:

判断stackb为空的时候   再把stacka的元素压到stackb

push1 此时的stacka 1

push2 此时的stacka 1 2

push3 此时的stacka 1 2 3

push4 此时的stacka 1 2 3 4

pop()    此时stackb为空 首先执行stacka的元素压到stackb 1234 再执行pop 1 此时的stackb 234

push 5     此时的stacka 5

pop() 2   此时的stackb 34

pop() 3  此时的stackb 4

pop() 4 此时 stackb为空   

pop()5  此时的stackb 空  首先执行stacka的元素压到stackb 此时的stackb 5 再执行pop5 此时的stackb 为空
关键代码段
​​​​​​​public int pop() {
    //如果stackb不为空 返回stackb的栈顶元素
    int peekvalue=-1;
    if(stackb.isEmpty()){//B空,先把A进B,再pop
         while(!stacka.isEmpty())
              stackb.push(stacka.pop());
    }
    return stackb.pop();//B不空,直接pop
}

由此可以看出队列为空时 

stackb.isEmpty()&&stacka.isEmpty()

最终的代码以及测试的案例:

class MyQueue {
     int front =0;
    int end =0;
    private Stack<Integer> stacka = new Stack<Integer>();
    private Stack<Integer> stackb = new Stack<Integer>();

    /** Initialize your data structure here. */
    public MyQueue() {

    }
    /*
 2.总结操作就是:
            *  入队:将元素进栈A
            *  出队:判断栈B是否为空,如果为空,则将栈A中所有元素pop,并push进栈B,栈B出栈;如果不为空,栈B直接出栈。
    */
    /** Push element x to the back of queue. */
    public void push(int x) {
        stacka.push(x);
    }

    /** 从队列的开头移除并返回元素 */
    /*
    Stack.peek() 函数返回栈顶的元素,但不弹出该栈顶元素。
    Stack.pop() 函数返回栈顶的元素,并且将该栈顶元素出栈。
     */
    public int pop() {
        //如果stackb不为空 返回stackb的栈顶元素
        if(stackb.isEmpty()){//B空,先把A进B,再pop
            while(!stacka.isEmpty())
                stackb.push(stacka.pop());
        }
        return stackb.pop();//B不空,直接pop
    }
    /**  返回队列开头的元素. */
    public int peek() {
        //如果stackb不为空 返回stackb的栈顶元素
        if(stackb.isEmpty()){//B空,先把A进B,再pop
            while(!stacka.isEmpty())
                stackb.push(stacka.pop());
        }
        return stackb.peek();
    }

    /** 队空判断实际也是stackb 是否empty的判断. */
    public boolean empty() {
        if (stackb.isEmpty()&&stacka.isEmpty())
            return true;//空的
        else
            return false;
    }

//     public static void main(String[] args) {
//         MyQueue myQueue = new MyQueue();
//         //["MyQueue","push","push","push","push","pop","push","pop","pop","pop","pop"]
//         //[[],[1],[2],[3],[4],[],[5],[],[],[],[]]
//         myQueue.push(1);
//         myQueue.push(2);
//         myQueue.push(3);
//         myQueue.push(4);
// //        System.out.println(myQueue.peek());//1
//         System.out.println(myQueue.pop()); /** 从队列的开头移除并返回元素 */
//         myQueue.push(5);
//         System.out.println(myQueue.pop());
//         System.out.println(myQueue.pop());
//         System.out.println(myQueue.pop());
//         System.out.println(myQueue.pop());
// //        System.out.println(myQueue.peek());//2
// //        System.out.println(myQueue.empty());
//     }
}

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue obj = new MyQueue();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.peek();
 * boolean param_4 = obj.empty();
 */

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值