Leetcode 232. 用栈实现队列 JAVA实现

本人整理出的两种方法,都是用两个栈实现队列。

用栈实现队列

法一

入队要用到栈一s1,s2; 出队只需用到栈一s1。

//该方法主要处理的是入栈操作。入队前,将s2作为中间容器,将s1的数据全部倒入s2,在将元素入队,最后将元素倒回s1
//因为此时s1保持了先进先出的特性,所以出队是直接从s1的栈顶出栈

import java.util.Stack;
class MyQueue {
private Stack<Integer> s1;
private Stack<Integer> s2;
/** Initialize your data structure here. */
public MyQueue() {
s1 = new Stack<>();
s2 = new Stack<>();
}

/** Push element x to the back of queue. */
public void push(int x) {
    if(s1.isEmpty())
        s1.push(x);
    else{
        while(!s1.isEmpty())
            s2.push(s1.pop());
        s2.push(x);
        while(!s2.isEmpty())
            s1.push(s2.pop());
    }

}
/** Removes the element from in front of queue and returns that element. */
public int pop() {
return s1.pop();
}

/** Get the front element. */
public int peek() {
    return s1.peek();
}

//只需要判断s1是否为空,s2仅仅是入队时的一个中间容器
public boolean empty() {
    return s1.isEmpty();
}
}

法二

入队只需直接s1.push(x);出队需要用到s1 s2

//出队时,如果s2为空,则将s1的数据全部倒入s2,注意,是全部倒入。倒入之后将栈顶出栈。
//当s2不为空时,则直接s2.pop()

import java.util.Stack;

class MyQueue {

private Stack<Integer> s1;//输入栈
private Stack<Integer> s2;//输出栈
/** Initialize your data structure here. */
public MyQueue() {
    s1 = new Stack<>();
    s2 = new Stack<>();
}

/** Push element x to the back of queue. */
public void push(int x) {
    s1.push(x);
}

/** Removes the element from in front of queue and returns that element. */
public int pop() {
    //只有当输出栈为空时才能向输出栈倒入数据
    if(s2.isEmpty())
        while(!s1.isEmpty())
            s2.push(s1.pop());
    return s2.pop();

}

/** Get the front element. */
public int peek() {
    if(s2.isEmpty())
        while(!s1.isEmpty())
            s2.push(s1.pop());
    return s2.peek();
}

/** Returns whether the queue is empty. */
public boolean empty() {
    // 当输入栈和输出栈同时为空时才能判断为空
    return s1.isEmpty() && s2.isEmpty();
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值