今天开始栈和队列
理论基础
本节参照栈和队列(超详细Java实现) @努力写代码的菜鸟,感谢@努力写代码的菜鸟
栈
后进先出 LIFO (Last In First Out)
入栈(push)
出栈(pop)
Java标准库中提供的栈
Stack继承了Vector,Vector是动态的顺序表,与ArrayList类似,不同的是,Vector是线程安全的。(来源: 栈和队列(超详细Java实现) @努力写代码的菜鸟)
实现的方法
队列
先进先出 FIFIO(First In First Out)
入队列(offer):进行插入操作的一端称为队尾(Tail/Rear)
出队列(poll):进行删除操作的一端称为队头(Head/Front)
Java标准库中提供的栈
Java中的 Queue是一个接口,需要通过实现这个接口的类来实例化对象, LinkedList实现了Queue接口,可以通过LinkedList实例化,如Queue<Integer> queue = new LinkedList<>(); (来源: 栈和队列(超详细Java实现) @努力写代码的菜鸟)
注意:Queue是一个接口!!!(后面做题有在这里犯错)
实现的方法
232.用栈实现队列
题目
思路
用两个栈来实现队列。通过一个in栈用来实现入栈操作,通过一个out栈将in栈出栈后的元素再入栈实现顺序变换,从而做到像队列一样“先进先出”。
代码
class MyQueue {
Stack<Integer> stackIn;
Stack<Integer> stackOut;
/** 实例化 */
public MyQueue() {
stackIn = new Stack<>();
stackOut = new Stack<>();
}
/**将x插入队列 */
public void push(int x) {
//入队列与入栈一样
stackIn.push(x);
}
/**出队列 */
public int pop() {
//判断出栈是否为空
if(stackOut.isEmpty()){
while(!stackIn.isEmpty()){
//当进栈不为空时持续循环,将进栈中的pop出来push到出栈中
stackOut.push(stackIn.pop());
}
}
int result = stackOut.pop();
return result;
}
public int peek() { //获取栈顶元素
int result = this.pop(); //复用类里的pop()
stackOut.push(result);
return result;
}
public boolean empty() {
return stackIn.isEmpty() && stackOut.isEmpty();
}
}
/**
* 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();
*/
注意:第32行复用了类里的pop(),这是因为获取栈顶元素与出栈的一些步骤相同,都需要将stackIn里的元素出栈再入栈到stackOut中。但与出栈操作pop()不同的是,pop里的result已经出栈了,而peek()不需要出栈只需要返回result。因此可以复用pop()后再放回。
在实际编程操作中,函数复用很重要!
当然,也可以将相同的部分封装成一个方法。
class MyQueue {
Deque<Integer> inStack;
Deque<Integer> outStack;
public MyQueue() {
inStack = new ArrayDeque<Integer>();
outStack = new ArrayDeque<Integer>();
}
public void push(int x) {
inStack.push(x);
}
public int pop() {
if (outStack.isEmpty()) {
in2out();
}
return outStack.pop();
}
public int peek() {
if (outStack.isEmpty()) {
in2out();
}
return outStack.peek();
}
public boolean empty() {
return inStack.isEmpty() && outStack.isEmpty();
}
private void in2out() {
while (!inStack.isEmpty()) {
outStack.push(inStack.pop());
}
}
}
作者:LeetCode-Solution
225. 用队列实现栈
题目
思路
这里可以用双队列法,一个队列用于入队,另一个队列用于暂存出队的元素。待第一个队列只剩一个元素时出队,即可像栈一样“后进先出”。
我一开始尝试用双队列法做,但是没有写成功,忽略了可以通过size-1找到队列中是否仅剩最后一个元素。
但这道题目用单队列法更为简单。将size个元素入队,size-1个元素弹出再重新入队。如此循环直至size-1为0循环停止。示例图如下:
代码
单队列法,但是pop前移动
class MyStack {
Queue<Integer> queue;
/**实力化 */
public MyStack() {
//queue = new Queue<>();
//注意,Queue是一个接口,不能实例化。需要通过实现这个接口的类Linkedlist来实例化对象。
queue = new LinkedList<Integer>();
}
public void push(int x) {
//push不受影响
queue.offer(x);
}
public int pop() {
int size = queue.size();//获取队列元素数
size --;
while(size-- > 0){
queue.offer(queue.poll());
}
return queue.poll();
}
public int top() {
int result = this.pop();
queue.offer(result);
return result;
}
public boolean empty() {
return queue.isEmpty();
}
}
/**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* boolean param_4 = obj.empty();
*/
单队列法,但是push后移动(推荐!)(与1相比,此方法push时即将出队元素调整完毕,接下来单pop和top代码语句就变得十分简单,也不用再复用代码。)
class MyStack {
//一个Queue, push后移动
Queue<Integer> queue;
public MyStack() {
queue = new LinkedList<Integer>();
}
public void push(int x) {
//现取size再入队
int n = queue.size();
queue.offer(x);
for(int i =0; i < n; i++){
//对入队前的n个数进行遍历
queue.offer(queue.poll());
}
/** 或者写为:*/
/*queue.offer(x);
int n = queue.size();
for(int i =0; i < n-1; i++){
//对前n-1个数进行遍历
queue.offer(queue.poll());
}*/
}
public int pop() {
return queue.poll();
}
public int top() {
return queue.peek();
}
public boolean empty() {
return queue.isEmpty();
}
}
/**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* boolean param_4 = obj.empty();
*/