文章目录
day10学习内容
day09主要内容
- 队列实现栈
- 栈实现队列
一、使用栈实现队列
声明
本文思路和文字,引用自《代码随想录》
1.1、核心思路
1、用2个栈,模拟实现一个队列。注意队列的特点:先进先出
1.2、代码
public static 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());
}
}
}
1.3、测试用例
class MoniZhan {
public static class MyQueue {
//省略,就是上面的代码
}
public static void main(String[] args) {
MyQueue myQueue = new MyQueue();
myQueue.push(1);
myQueue.push(2);
System.out.println(myQueue.peek());//输出1
System.out.println(myQueue.pop());//输出1
System.out.println(myQueue.empty());//输出false
}
}
1.3.1、关于ArrayDeque.peek()方法
直接看源码中的注释
/**
* Retrieves, but does not remove, the head of the queue represented by
* this deque (in other words, the first element of this deque), or
* returns {@code null} if this deque is empty.
*
* <p>This method is equivalent to {@link #peekFirst()}.
*
* @return the head of the queue represented by this deque, or
* {@code null} if this deque is empty
*/
E peek();
返回队头元素,并且不会删除该元素,如果对头没有元素,则直接返回null
1.3.2、关于ArrayDeque.pop()方法
/**
* Pops an element from the stack represented by this deque. In other
* words, removes and returns the first element of this deque.
*
* <p>This method is equivalent to {@link #removeFirst()}.
*
* @return the element at the front of this deque (which is the top
* of the stack represented by this deque)
* @throws NoSuchElementException if this deque is empty
*/
E pop();
返回并删除队头元素,如果队头没有元素,则抛出NoSuchElementException 异常
二、使用队列实现栈
2.1、核心思路
1、不要惯性思维,简单认为可以用两个队列实现栈,直接使用一个队列实现即可
2.2、代码
class MyStack {
Queue<Integer> queue;
public MyStack() {
queue = new LinkedList<Integer>();
}
public void push(int x) {
int n = queue.size();
queue.offer(x);
for (int i = 0; i < n; i++) {
queue.offer(queue.poll());
}
}
public int pop() {
return queue.poll();
}
public int top() {
return queue.peek();
}
public boolean empty() {
return queue.isEmpty();
}
}
2.3、测试代码
public static void main(String[] args) {
MyStack myQueue = new MyStack();
myQueue.push(1);
myQueue.push(2);
System.out.println(myQueue.top());
System.out.println(myQueue.pop());
System.out.println(myQueue.empty());
}
2.3.1、关于LinkedList.peek()方法
直接看源码中的注释
/**
* Retrieves, but does not remove, the head of this queue,
* or returns {@code null} if this queue is empty.
*
* @return the head of this queue, or {@code null} if this queue is empty
*/
E peek();
2.3.2、关于LinkedList.poll()方法
/**
* Retrieves and removes the head of this queue,
* or returns {@code null} if this queue is empty.
*
* @return the head of this queue, or {@code null} if this queue is empty
*/
E poll();
2.3.2、关于LinkedList.offer()方法
/**
* Adds the specified element as the tail (last element) of this list.
*
* @param e the element to add
* @return {@code true} (as specified by {@link Queue#offer})
* @since 1.5
*/
public boolean offer(E e) {
return add(e);
}
/**
* Appends the specified element to the end of this list.
*
* <p>This method is equivalent to {@link #addLast}.
*
* @param e element to be appended to this list
* @return {@code true} (as specified by {@link Collection#add})
*/
public boolean add(E e) {
linkLast(e);
return true;
}
``
总结
1.感想
- 最轻松的一天。主要就是考察API
2.思维导图
本文思路引用自代码随想录,感谢代码随想录作者。