队列(Queue)
1. 概念
队列:只允许在一端进行插入数据操作,在另一端进行删除数据操作的特殊线性表
队列具有先进先出FIFO(First In First Out)
入队列:进行插入操作的一端称为队尾(Tail/Rear)
出队列:进行删除操作的一端称为队头(Head/Front)
2. 链表队列
实现:
队列也可以数组和链表的结构实现,使用链表的结构实现更优一些,因为如果使用数组的结构,出队列在数组头上出数据,效率会比较低
class Node {
int val;
Node next;
Node(int val, Node next) {
this.val = val;
this.next = next;
}
Node(int val) {
this(val, null);
}
}
public class MyQueue {
private Node head = null;
private Node tail = null;
private int size = 0;
public void offer(int v) {
Node node = new Node(v);
if (tail == null) {
head = node;
} else {
tail.next = node;
}
tail = node;
size++;
}
public int poll() {
if (size == 0) {
throw new RuntimeException("队列为空");
}
Node oldHead = head;
head = head.next;
if (head == null) {
tail = null;
}
size--;
return oldHead.val;
}
public int peek() {
if (size == 0) {
throw new RuntimeException("队列为空");
}
return head.val;
}
public boolean isEmpty() {
return size == 0;
}
public int size() {
return size;
}
}
3. 双端队列 (Deque)
双端队列(deque)是指允许两端都可以进行入队和出队操作的队列,deque 是 “double ended queue” 的简称。 那就说明元素可以从队头出队和入队,也可以从队尾出队和入队
4. 循环队列
我这里考虑的循环队列是底层用数组实现的,队列类中定义一个数组属性,在构造期中初始化这个数组,数组最后一个元素置空当做循环队列的尾部
所以我们创建数组的时候数组长度应该为队列长度+1
当front和rear指向同一个元素时,队列为空
当rear的下一个时front时,队列满
考虑到是一个循环数组,所以下标会在一个范围内重复,使用求模运算%来解决
数组下标循环的小技巧
1. 下标最后再往后(offset 小于 array.length): index = (index + offset) % array.length
2. 下标最前再往前(offset 小于 array.length): index = (index + array.length - offset) % array.length
代码实现:
class MyCircularQueue {
public int[] elem;
public int front;
public int rear;
public MyCircularQueue(int k) {
this.elem = new int[k + 1];
}
public boolean enQueue(int value) {
if(isFull()){
return false;
}
this.elem[this.rear] = value;
this.rear = (this.rear + 1) % this.elem.length;
return true;
}
public boolean deQueue() {
if(isEmpty()){
return false;
}
this.front = (this.front + 1) % this.elem.length;
return true;
}
public int Front() {
if(isEmpty()){
return -1;
}
return this.elem[this.front];
}
public int Rear() {
if(isEmpty()){
return -1;
}
return this.rear == 0 ? this.elem[this.elem.length - 1] : this.elem[this.rear - 1];
}
public boolean isEmpty() {
return this.front == this.rear;
}
public boolean isFull() {
return (this.rear + 1) % this.elem.length == this.front ? true : false;
}
}