一、队列(Queue)
1.1 概念
队列只允许在一端进行插入,在另一端进行删除数据操作的特殊线性表,队列遵循先进先出FIFO(FIrst In First Out)的原则。
入队列:进行插入操作的一端称为队尾(Tail/Rear)
出队列:进行删除操作的一端称为队头(Head/Front)
举例:食堂打饭排队,排在前面的先离开,排在后面的后离开。
1.2 队列的使用
在Java中,Queue是个接口,底层使用链表来实现的。
方法 | 功能 |
---|---|
boolean offer(E e) | 入队列 |
E poll() | 出队列 |
peek() | 获取队头元素 |
int size() | 获取队列中有效元素的个数 |
boolean isEmpty() | 检测队列是否为空 |
注意:Queue是个接口,在实例化时必须实例化LinkedList的对象,因为LinkedList实现了Queue接口。
public static void main(String[] args) {
Queue<Integer> q = new LinkedList<>();
q.offer(1);
q.offer(2);
q.offer(3);
q.offer(4);
q.offer(5); // 从队尾入队列
System.out.println(q.size());
System.out.println(q.peek()); // 获取队头元素
q.poll();
System.out.println(q.poll()); // 从队头出队列,并将删除的元素返回
if(q.isEmpty()){
System.out.println("队列空");
}else{
System.out.println(q.size());
}
}
1.3 模拟实现队列
1.链式结构实现
public class MyQueue {
//双向链表实现队列
static class ListNode {
public int val;
public ListNode prev;
public ListNode next;
public ListNode(int val) {
this.val = val;
}
}
public ListNode first = null;
public ListNode last = null;
public int usedSize = 0;
public void offer(int val) {
ListNode node = new ListNode(val);
if(isEmpty()){
first = last = node;
usedSize++;
return;
}
node.prev = last;
last.next = node;
last = node;
usedSize++;
}
public int poll() {
if(isEmpty()) {
throw new EmptyQueueException();
}
int valPoll = first.val;
first = first.next;
if(first != null) {
first.prev = null;
}
usedSize--;
return valPoll;
}
public int peek() {
if(isEmpty()) {
throw new EmptyQueueException();
}
return first.val;
}
public boolean isEmpty() {
return usedSize == 0;
//return first == 0;
}
}
2. 顺序结构实现
class MyCircularQueue {
public int front;
public int rear;
public int[] elem;
public MyCircularQueue(int k) {
elem = new int[k+1];
front = rear = 0;
}
public boolean enQueue(int value) {
if(isFull()) {
return false;
}
elem[rear] = value;
rear = (rear + 1) % elem.length;
return true;
}
public boolean deQueue() {
if(isEmpty()) {
return false;
}
front = (front + 1)%elem.length;
return true;
}
public int Front() {
if(isEmpty()){
return -1;
}
return elem[front];
}
public int Rear() {
if(isEmpty()) {
return -1;
}
int index = (rear == 0) ? elem.length-1 : rear-1;
return elem[index];
}
public boolean isEmpty() {
return front == rear;
}
public boolean isFull() {
return (rear + 1) % elem.length == front;
}
}
二、双端队列(Deque)
双端队列(deque)是指允许两端都可以进行入队和出队操作的队列,deque 是 “double ended queue” 的简称。 那就说明元素可以从队头出队和入队,也可以从队尾出队和入队。
Deque是一个接口,使用时必须创建LinkedList的对象
在实际工程中,使用Deque接口是比较多的,栈和队列均可以使用该接口。
Deque stack = new ArrayDeque<>();//双端队列的线性实现
Deque queue = new LinkedList<>();//双端队列的链式实现
三、题型
1.1 用队列实现栈
class MyStack {
public Queue<Integer> qu1;
public Queue<Integer> qu2;
public MyStack(){
qu1 = new LinkedList<>();
qu2 = new LinkedList<>();
}
public void push(int val) {
if (!qu1.isEmpty()) {
qu1.offer(val);
}else if (!qu2.isEmpty()) {
qu2.offer(val);
}else {
qu1.offer(val);
}
}
public int pop() {
if(empty()) {
return -1;
}
if(!qu1.isEmpty()) {
int size = qu1.size();
for(int i = 0;i < size - 1;i++) {
qu2.offer(qu1.poll());
}
return qu1.poll();
}else {
int size = qu2.size();
for(int i = 0;i < size - 1;i++) {
qu1.offer(qu2.poll());
}
return qu2.poll();
}
}
public int top() {
if(empty()) {
return -1;
}
if(!qu1.isEmpty()) {
int size = qu1.size();
int val = 0;
for (int i = 0;i < size;i++) {
val = qu1.poll();
qu2.offer(val);
}
return val;
}else {
int size = qu2.size();
int val = 0;
for (int i = 0; i < size; i++) {
val = qu2.poll();
qu1.offer(val);
}
return val;
}
}
public boolean empty() {
return qu1.isEmpty() && qu2.isEmpty();
}
}
1.2 用栈实现队列
class MyQueue {
public Stack<Integer> stack1;
public Stack<Integer> stack2;
public MyQueue() {
stack1 = new Stack<>();
stack2 = new Stack<>();
}
public void push(int val) {
stack1.push(val);
}
public int pop() {
if(empty()) {
return -1;
}
if(stack2.isEmpty()) {
while(!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
}
return stack2.pop();
}
public int peek() {
if(empty()) {
return -1;
}
if(stack2.isEmpty()) {
while(!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
}
return stack2.peek();
}
public boolean empty() {
return stack1.isEmpty() && stack2.isEmpty();
}
}