|
|
@ -741,46 +741,53 @@ comments: true
|
|
|
|
```java title="array_queue.java"
|
|
|
|
```java title="array_queue.java"
|
|
|
|
/* 基于环形数组实现的队列 */
|
|
|
|
/* 基于环形数组实现的队列 */
|
|
|
|
class ArrayQueue {
|
|
|
|
class ArrayQueue {
|
|
|
|
private int[] nums; // 用于存储队列元素的数组
|
|
|
|
private int[] nums; // 用于存储队列元素的数组
|
|
|
|
private int front = 0; // 头指针,指向队首
|
|
|
|
private int front; // 队首指针,指向队首元素
|
|
|
|
private int rear = 0; // 尾指针,指向队尾 + 1
|
|
|
|
private int queSize; // 队列长度
|
|
|
|
|
|
|
|
|
|
|
|
public ArrayQueue(int capacity) {
|
|
|
|
public ArrayQueue(int capacity) {
|
|
|
|
// 初始化数组
|
|
|
|
|
|
|
|
nums = new int[capacity];
|
|
|
|
nums = new int[capacity];
|
|
|
|
|
|
|
|
front = queSize = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 获取队列的容量 */
|
|
|
|
/* 获取队列的容量 */
|
|
|
|
public int capacity() {
|
|
|
|
public int capacity() {
|
|
|
|
return nums.length;
|
|
|
|
return nums.length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 获取队列的长度 */
|
|
|
|
/* 获取队列的长度 */
|
|
|
|
public int size() {
|
|
|
|
public int size() {
|
|
|
|
int capacity = capacity();
|
|
|
|
return queSize;
|
|
|
|
// 由于将数组看作为环形,可能 rear < front ,因此需要取余数
|
|
|
|
|
|
|
|
return (capacity + rear - front) % capacity;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 判断队列是否为空 */
|
|
|
|
/* 判断队列是否为空 */
|
|
|
|
public boolean isEmpty() {
|
|
|
|
public boolean isEmpty() {
|
|
|
|
return rear - front == 0;
|
|
|
|
return queSize == 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 入队 */
|
|
|
|
/* 入队 */
|
|
|
|
public void offer(int num) {
|
|
|
|
public void offer(int num) {
|
|
|
|
if (size() == capacity()) {
|
|
|
|
if (queSize == capacity()) {
|
|
|
|
System.out.println("队列已满");
|
|
|
|
System.out.println("队列已满");
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 计算尾指针,指向队尾索引 + 1
|
|
|
|
|
|
|
|
// 通过取余操作,实现 rear 越过数组尾部后回到头部
|
|
|
|
|
|
|
|
int rear = (front + queSize) % capacity();
|
|
|
|
// 尾结点后添加 num
|
|
|
|
// 尾结点后添加 num
|
|
|
|
nums[rear] = num;
|
|
|
|
nums[rear] = num;
|
|
|
|
// 尾指针向后移动一位,越过尾部后返回到数组头部
|
|
|
|
queSize++;
|
|
|
|
rear = (rear + 1) % capacity();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 出队 */
|
|
|
|
/* 出队 */
|
|
|
|
public int poll() {
|
|
|
|
public int poll() {
|
|
|
|
int num = peek();
|
|
|
|
int num = peek();
|
|
|
|
// 队头指针向后移动一位,若越过尾部则返回到数组头部
|
|
|
|
// 队首指针向后移动一位,若越过尾部则返回到数组头部
|
|
|
|
front = (front + 1) % capacity();
|
|
|
|
front = (front + 1) % capacity();
|
|
|
|
|
|
|
|
queSize--;
|
|
|
|
return num;
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 访问队首元素 */
|
|
|
|
/* 访问队首元素 */
|
|
|
|
public int peek() {
|
|
|
|
public int peek() {
|
|
|
|
if (isEmpty())
|
|
|
|
if (isEmpty())
|
|
|
@ -796,50 +803,60 @@ comments: true
|
|
|
|
/* 基于环形数组实现的队列 */
|
|
|
|
/* 基于环形数组实现的队列 */
|
|
|
|
class ArrayQueue {
|
|
|
|
class ArrayQueue {
|
|
|
|
private:
|
|
|
|
private:
|
|
|
|
int *nums; // 用于存储队列元素的数组
|
|
|
|
int *nums; // 用于存储队列元素的数组
|
|
|
|
int cap; // 队列容量
|
|
|
|
int front; // 队首指针,指向队首元素
|
|
|
|
int front = 0; // 头指针,指向队首
|
|
|
|
int queSize; // 队列长度
|
|
|
|
int rear = 0; // 尾指针,指向队尾 + 1
|
|
|
|
int queCapacity; // 队列容量
|
|
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
public:
|
|
|
|
ArrayQueue(int capacity) {
|
|
|
|
ArrayQueue(int capacity) {
|
|
|
|
// 初始化数组
|
|
|
|
// 初始化数组
|
|
|
|
cap = capacity;
|
|
|
|
|
|
|
|
nums = new int[capacity];
|
|
|
|
nums = new int[capacity];
|
|
|
|
|
|
|
|
queCapacity = capacity;
|
|
|
|
|
|
|
|
front = queSize = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
~ArrayQueue() {
|
|
|
|
~ArrayQueue() {
|
|
|
|
delete[] nums;
|
|
|
|
delete[] nums;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 获取队列的容量 */
|
|
|
|
/* 获取队列的容量 */
|
|
|
|
int capacity() {
|
|
|
|
int capacity() {
|
|
|
|
return cap;
|
|
|
|
return queCapacity;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 获取队列的长度 */
|
|
|
|
/* 获取队列的长度 */
|
|
|
|
int size() {
|
|
|
|
int size() {
|
|
|
|
// 由于将数组看作为环形,可能 rear < front ,因此需要取余数
|
|
|
|
return queSize;
|
|
|
|
return (capacity() + rear - front) % capacity();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 判断队列是否为空 */
|
|
|
|
/* 判断队列是否为空 */
|
|
|
|
bool empty() {
|
|
|
|
bool empty() {
|
|
|
|
return rear - front == 0;
|
|
|
|
return size() == 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 入队 */
|
|
|
|
/* 入队 */
|
|
|
|
void offer(int num) {
|
|
|
|
void offer(int num) {
|
|
|
|
if (size() == capacity()) {
|
|
|
|
if (queSize == queCapacity) {
|
|
|
|
cout << "队列已满" << endl;
|
|
|
|
cout << "队列已满" << endl;
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 计算队尾指针,指向队尾索引 + 1
|
|
|
|
|
|
|
|
// 通过取余操作,实现 rear 越过数组尾部后回到头部
|
|
|
|
|
|
|
|
int rear = (front + queSize) % queCapacity;
|
|
|
|
// 尾结点后添加 num
|
|
|
|
// 尾结点后添加 num
|
|
|
|
nums[rear] = num;
|
|
|
|
nums[rear] = num;
|
|
|
|
// 尾指针向后移动一位,越过尾部后返回到数组头部
|
|
|
|
queSize++;
|
|
|
|
rear = (rear + 1) % capacity();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 出队 */
|
|
|
|
/* 出队 */
|
|
|
|
void poll() {
|
|
|
|
void poll() {
|
|
|
|
int num = peek();
|
|
|
|
int num = peek();
|
|
|
|
// 队头指针向后移动一位,若越过尾部则返回到数组头部
|
|
|
|
// 队首指针向后移动一位,若越过尾部则返回到数组头部
|
|
|
|
front = (front + 1) % capacity();
|
|
|
|
front = (front + 1) % queCapacity;
|
|
|
|
|
|
|
|
queSize--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 访问队首元素 */
|
|
|
|
/* 访问队首元素 */
|
|
|
|
int peek() {
|
|
|
|
int peek() {
|
|
|
|
if (empty())
|
|
|
|
if (empty())
|
|
|
@ -856,8 +873,8 @@ comments: true
|
|
|
|
class ArrayQueue:
|
|
|
|
class ArrayQueue:
|
|
|
|
def __init__(self, size):
|
|
|
|
def __init__(self, size):
|
|
|
|
self.__nums = [0] * size # 用于存储队列元素的数组
|
|
|
|
self.__nums = [0] * size # 用于存储队列元素的数组
|
|
|
|
self.__front = 0 # 头指针,指向队首
|
|
|
|
self.__front = 0 # 队首指针,指向队首元素
|
|
|
|
self.__rear = 0 # 尾指针,指向队尾 + 1
|
|
|
|
self.__size = 0 # 队列长度
|
|
|
|
|
|
|
|
|
|
|
|
""" 获取队列的容量 """
|
|
|
|
""" 获取队列的容量 """
|
|
|
|
def capacity(self):
|
|
|
|
def capacity(self):
|
|
|
@ -865,45 +882,34 @@ comments: true
|
|
|
|
|
|
|
|
|
|
|
|
""" 获取队列的长度 """
|
|
|
|
""" 获取队列的长度 """
|
|
|
|
def size(self):
|
|
|
|
def size(self):
|
|
|
|
# 由于将数组看作为环形,可能 rear < front ,因此需要取余数
|
|
|
|
return self.__size
|
|
|
|
return (self.capacity() + self.__rear - self.__front) % self.capacity()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
""" 判断队列是否为空 """
|
|
|
|
""" 判断队列是否为空 """
|
|
|
|
def is_empty(self):
|
|
|
|
def is_empty(self):
|
|
|
|
return (self.__rear - self.__front) == 0
|
|
|
|
return self.__size == 0
|
|
|
|
|
|
|
|
|
|
|
|
""" 入队 """
|
|
|
|
""" 入队 """
|
|
|
|
def push(self, val):
|
|
|
|
def push(self, num):
|
|
|
|
if self.size() == self.capacity():
|
|
|
|
assert self.__size < self.capacity(), "队列已满"
|
|
|
|
print("队列已满")
|
|
|
|
# 计算尾指针,指向队尾索引 + 1
|
|
|
|
return False
|
|
|
|
# 通过取余操作,实现 rear 越过数组尾部后回到头部
|
|
|
|
|
|
|
|
rear = (self.__front + self.__size) % self.capacity()
|
|
|
|
# 尾结点后添加 num
|
|
|
|
# 尾结点后添加 num
|
|
|
|
self.__nums[self.__rear] = val
|
|
|
|
self.__nums[rear] = num
|
|
|
|
# 尾指针向后移动一位,越过尾部后返回到数组头部
|
|
|
|
self.__size += 1
|
|
|
|
self.__rear = (self.__rear + 1) % self.capacity()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
""" 出队 """
|
|
|
|
""" 出队 """
|
|
|
|
def poll(self):
|
|
|
|
def poll(self):
|
|
|
|
num = self.peek()
|
|
|
|
num = self.peek()
|
|
|
|
# 队头指针向后移动一位,若越过尾部则返回到数组头部
|
|
|
|
# 队首指针向后移动一位,若越过尾部则返回到数组头部
|
|
|
|
self.__front = (self.__front + 1) % self.capacity()
|
|
|
|
self.__front = (self.__front + 1) % self.capacity()
|
|
|
|
|
|
|
|
self.__size -= 1
|
|
|
|
return num
|
|
|
|
return num
|
|
|
|
|
|
|
|
|
|
|
|
""" 访问队首元素 """
|
|
|
|
""" 访问队首元素 """
|
|
|
|
def peek(self):
|
|
|
|
def peek(self):
|
|
|
|
if self.is_empty():
|
|
|
|
assert not self.is_empty(), "队列为空"
|
|
|
|
print("队列为空")
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
return self.__nums[self.__front]
|
|
|
|
return self.__nums[self.__front]
|
|
|
|
|
|
|
|
|
|
|
|
""" 返回列表用于打印 """
|
|
|
|
|
|
|
|
def to_list(self):
|
|
|
|
|
|
|
|
res = [0] * self.size()
|
|
|
|
|
|
|
|
j = self.__front
|
|
|
|
|
|
|
|
for i in range(self.size()):
|
|
|
|
|
|
|
|
res[i] = self.__nums[(j % self.capacity())]
|
|
|
|
|
|
|
|
j += 1
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
=== "Go"
|
|
|
@ -911,54 +917,53 @@ comments: true
|
|
|
|
```go title="array_queue.go"
|
|
|
|
```go title="array_queue.go"
|
|
|
|
/* 基于环形数组实现的队列 */
|
|
|
|
/* 基于环形数组实现的队列 */
|
|
|
|
type arrayQueue struct {
|
|
|
|
type arrayQueue struct {
|
|
|
|
data []int // 用于存储队列元素的数组
|
|
|
|
nums []int // 用于存储队列元素的数组
|
|
|
|
capacity int // 队列容量(即最多容量的元素个数)
|
|
|
|
front int // 队首指针,指向队首元素
|
|
|
|
front int // 头指针,指向队首
|
|
|
|
queSize int // 队列长度
|
|
|
|
rear int // 尾指针,指向队尾 + 1
|
|
|
|
queCapacity int // 队列容量(即最大容纳元素数量)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// newArrayQueue 基于环形数组实现的队列
|
|
|
|
// newArrayQueue 基于环形数组实现的队列
|
|
|
|
func newArrayQueue(capacity int) *arrayQueue {
|
|
|
|
func newArrayQueue(queCapacity int) *arrayQueue {
|
|
|
|
return &arrayQueue{
|
|
|
|
return &arrayQueue{
|
|
|
|
data: make([]int, capacity),
|
|
|
|
nums: make([]int, queCapacity),
|
|
|
|
capacity: capacity,
|
|
|
|
queCapacity: queCapacity,
|
|
|
|
front: 0,
|
|
|
|
front: 0,
|
|
|
|
rear: 0,
|
|
|
|
queSize: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// size 获取队列的长度
|
|
|
|
// size 获取队列的长度
|
|
|
|
func (q *arrayQueue) size() int {
|
|
|
|
func (q *arrayQueue) size() int {
|
|
|
|
size := (q.capacity + q.rear - q.front) % q.capacity
|
|
|
|
return q.queSize
|
|
|
|
return size
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// isEmpty 判断队列是否为空
|
|
|
|
// isEmpty 判断队列是否为空
|
|
|
|
func (q *arrayQueue) isEmpty() bool {
|
|
|
|
func (q *arrayQueue) isEmpty() bool {
|
|
|
|
return q.rear-q.front == 0
|
|
|
|
return q.queSize == 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// offer 入队
|
|
|
|
// offer 入队
|
|
|
|
func (q *arrayQueue) offer(v int) {
|
|
|
|
func (q *arrayQueue) offer(num int) {
|
|
|
|
// 当 rear == capacity 表示队列已满
|
|
|
|
// 当 rear == queCapacity 表示队列已满
|
|
|
|
if q.size() == q.capacity {
|
|
|
|
if q.queSize == q.queCapacity {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 尾结点后添加
|
|
|
|
// 计算尾指针,指向队尾索引 + 1
|
|
|
|
q.data[q.rear] = v
|
|
|
|
// 通过取余操作,实现 rear 越过数组尾部后回到头部
|
|
|
|
// 尾指针向后移动一位,越过尾部后返回到数组头部
|
|
|
|
rear := (q.front + q.queSize) % q.queCapacity
|
|
|
|
q.rear = (q.rear + 1) % q.capacity
|
|
|
|
// 尾结点后添加 num
|
|
|
|
|
|
|
|
q.nums[rear] = num
|
|
|
|
|
|
|
|
q.queSize++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// poll 出队
|
|
|
|
// poll 出队
|
|
|
|
func (q *arrayQueue) poll() any {
|
|
|
|
func (q *arrayQueue) poll() any {
|
|
|
|
if q.isEmpty() {
|
|
|
|
num := q.peek()
|
|
|
|
return nil
|
|
|
|
// 队首指针向后移动一位,若越过尾部则返回到数组头部
|
|
|
|
}
|
|
|
|
q.front = (q.front + 1) % q.queCapacity
|
|
|
|
v := q.data[q.front]
|
|
|
|
q.queSize--
|
|
|
|
// 队头指针向后移动一位,若越过尾部则返回到数组头部
|
|
|
|
return num
|
|
|
|
q.front = (q.front + 1) % q.capacity
|
|
|
|
|
|
|
|
return v
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// peek 访问队首元素
|
|
|
|
// peek 访问队首元素
|
|
|
@ -966,8 +971,17 @@ comments: true
|
|
|
|
if q.isEmpty() {
|
|
|
|
if q.isEmpty() {
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
v := q.data[q.front]
|
|
|
|
return q.nums[q.front]
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 获取 Slice 用于打印
|
|
|
|
|
|
|
|
func (q *arrayQueue) toSlice() []int {
|
|
|
|
|
|
|
|
rear := (q.front + q.queSize)
|
|
|
|
|
|
|
|
if rear >= q.queCapacity {
|
|
|
|
|
|
|
|
rear %= q.queCapacity
|
|
|
|
|
|
|
|
return append(q.nums[q.front:], q.nums[:rear]...)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return q.nums[q.front:rear]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
@ -976,46 +990,55 @@ comments: true
|
|
|
|
```js title="array_queue.js"
|
|
|
|
```js title="array_queue.js"
|
|
|
|
/* 基于环形数组实现的队列 */
|
|
|
|
/* 基于环形数组实现的队列 */
|
|
|
|
class ArrayQueue {
|
|
|
|
class ArrayQueue {
|
|
|
|
#queue; // 用于存储队列元素的数组
|
|
|
|
#nums; // 用于存储队列元素的数组
|
|
|
|
#front = 0; // 头指针,指向队首
|
|
|
|
#front = 0; // 队首指针,指向队首元素
|
|
|
|
#rear = 0; // 尾指针,指向队尾 + 1
|
|
|
|
#queSize = 0; // 队列长度
|
|
|
|
|
|
|
|
|
|
|
|
constructor(capacity) {
|
|
|
|
constructor(capacity) {
|
|
|
|
this.#queue = new Array(capacity);
|
|
|
|
this.#nums = new Array(capacity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 获取队列的容量 */
|
|
|
|
/* 获取队列的容量 */
|
|
|
|
get capacity() {
|
|
|
|
get capacity() {
|
|
|
|
return this.#queue.length;
|
|
|
|
return this.#nums.length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 获取队列的长度 */
|
|
|
|
/* 获取队列的长度 */
|
|
|
|
get size() {
|
|
|
|
get size() {
|
|
|
|
// 由于将数组看作为环形,可能 rear < front ,因此需要取余数
|
|
|
|
return this.#queSize;
|
|
|
|
return (this.capacity + this.#rear - this.#front) % this.capacity;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 判断队列是否为空 */
|
|
|
|
/* 判断队列是否为空 */
|
|
|
|
empty() {
|
|
|
|
empty() {
|
|
|
|
return this.#rear - this.#front == 0;
|
|
|
|
return this.#queSize == 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 入队 */
|
|
|
|
/* 入队 */
|
|
|
|
offer(num) {
|
|
|
|
offer(num) {
|
|
|
|
if (this.size == this.capacity)
|
|
|
|
if (this.size == this.capacity)
|
|
|
|
throw new Error("队列已满");
|
|
|
|
throw new Error("队列已满");
|
|
|
|
|
|
|
|
// 计算尾指针,指向队尾索引 + 1
|
|
|
|
|
|
|
|
// 通过取余操作,实现 rear 越过数组尾部后回到头部
|
|
|
|
|
|
|
|
const rear = (this.#front + this.size) % this.capacity;
|
|
|
|
// 尾结点后添加 num
|
|
|
|
// 尾结点后添加 num
|
|
|
|
this.#queue[this.#rear] = num;
|
|
|
|
this.#nums[rear] = num;
|
|
|
|
// 尾指针向后移动一位,越过尾部后返回到数组头部
|
|
|
|
this.#queSize++;
|
|
|
|
this.#rear = (this.#rear + 1) % this.capacity;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 出队 */
|
|
|
|
/* 出队 */
|
|
|
|
poll() {
|
|
|
|
poll() {
|
|
|
|
const num = this.peek();
|
|
|
|
const num = this.peek();
|
|
|
|
// 队头指针向后移动一位,若越过尾部则返回到数组头部
|
|
|
|
// 队首指针向后移动一位,若越过尾部则返回到数组头部
|
|
|
|
this.#front = (this.#front + 1) % this.capacity;
|
|
|
|
this.#front = (this.#front + 1) % this.capacity;
|
|
|
|
|
|
|
|
this.#queSize--;
|
|
|
|
return num;
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 访问队首元素 */
|
|
|
|
/* 访问队首元素 */
|
|
|
|
peek() {
|
|
|
|
peek() {
|
|
|
|
if (this.empty())
|
|
|
|
if (this.empty())
|
|
|
|
throw new Error("队列为空");
|
|
|
|
throw new Error("队列为空");
|
|
|
|
return this.#queue[this.#front];
|
|
|
|
return this.#nums[this.#front];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
@ -1025,47 +1048,56 @@ comments: true
|
|
|
|
```typescript title="array_queue.ts"
|
|
|
|
```typescript title="array_queue.ts"
|
|
|
|
/* 基于环形数组实现的队列 */
|
|
|
|
/* 基于环形数组实现的队列 */
|
|
|
|
class ArrayQueue {
|
|
|
|
class ArrayQueue {
|
|
|
|
private queue: number[]; // 用于存储队列元素的数组
|
|
|
|
private nums: number[]; // 用于存储队列元素的数组
|
|
|
|
private front: number = 0; // 头指针,指向队首
|
|
|
|
private front: number; // 队首指针,指向队首元素
|
|
|
|
private rear: number = 0; // 尾指针,指向队尾 + 1
|
|
|
|
private queSize: number; // 队列长度
|
|
|
|
private CAPACITY: number = 1e5;
|
|
|
|
|
|
|
|
constructor(capacity?: number) {
|
|
|
|
constructor(capacity: number) {
|
|
|
|
this.queue = new Array<number>(capacity ?? this.CAPACITY);
|
|
|
|
this.nums = new Array<number>(capacity);
|
|
|
|
|
|
|
|
this.front = this.queSize = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 获取队列的容量 */
|
|
|
|
/* 获取队列的容量 */
|
|
|
|
get capacity(): number {
|
|
|
|
get capacity(): number {
|
|
|
|
return this.queue.length;
|
|
|
|
return this.nums.length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 获取队列的长度 */
|
|
|
|
/* 获取队列的长度 */
|
|
|
|
get size(): number {
|
|
|
|
get size(): number {
|
|
|
|
// 由于将数组看作为环形,可能 rear < front ,因此需要取余数
|
|
|
|
return this.queSize;
|
|
|
|
return (this.capacity + this.rear - this.front) % this.capacity;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 判断队列是否为空 */
|
|
|
|
/* 判断队列是否为空 */
|
|
|
|
empty(): boolean {
|
|
|
|
empty(): boolean {
|
|
|
|
return this.rear - this.front == 0;
|
|
|
|
return this.queSize == 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 入队 */
|
|
|
|
/* 入队 */
|
|
|
|
offer(num: number): void {
|
|
|
|
offer(num: number): void {
|
|
|
|
if (this.size == this.capacity)
|
|
|
|
if (this.size == this.capacity)
|
|
|
|
throw new Error("队列已满");
|
|
|
|
throw new Error("队列已满");
|
|
|
|
|
|
|
|
// 计算尾指针,指向队尾索引 + 1
|
|
|
|
|
|
|
|
// 通过取余操作,实现 rear 越过数组尾部后回到头部
|
|
|
|
|
|
|
|
const rear = (this.front + this.queSize) % this.capacity;
|
|
|
|
// 尾结点后添加 num
|
|
|
|
// 尾结点后添加 num
|
|
|
|
this.queue[this.rear] = num;
|
|
|
|
this.nums[rear] = num;
|
|
|
|
// 尾指针向后移动一位,越过尾部后返回到数组头部
|
|
|
|
this.queSize++;
|
|
|
|
this.rear = (this.rear + 1) % this.capacity;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 出队 */
|
|
|
|
/* 出队 */
|
|
|
|
poll(): number {
|
|
|
|
poll(): number {
|
|
|
|
const num = this.peek();
|
|
|
|
const num = this.peek();
|
|
|
|
// 队头指针向后移动一位,若越过尾部则返回到数组头部
|
|
|
|
// 队首指针向后移动一位,若越过尾部则返回到数组头部
|
|
|
|
this.front = (this.front + 1) % this.capacity;
|
|
|
|
this.front = (this.front + 1) % this.capacity;
|
|
|
|
|
|
|
|
this.queSize--;
|
|
|
|
return num;
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 访问队首元素 */
|
|
|
|
/* 访问队首元素 */
|
|
|
|
peek(): number {
|
|
|
|
peek(): number {
|
|
|
|
if (this.empty())
|
|
|
|
if (this.empty())
|
|
|
|
throw new Error("队列为空");
|
|
|
|
throw new Error("队列为空");
|
|
|
|
return this.queue[this.front];
|
|
|
|
return this.nums[this.front];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
@ -1082,52 +1114,60 @@ comments: true
|
|
|
|
/* 基于环形数组实现的队列 */
|
|
|
|
/* 基于环形数组实现的队列 */
|
|
|
|
class ArrayQueue
|
|
|
|
class ArrayQueue
|
|
|
|
{
|
|
|
|
{
|
|
|
|
private int[] nums; // 用于存储队列元素的数组
|
|
|
|
private int[] nums; // 用于存储队列元素的数组
|
|
|
|
private int front = 0; // 头指针,指向队首
|
|
|
|
private int front; // 队首指针,指向队首元素
|
|
|
|
private int rear = 0; // 尾指针,指向队尾 + 1
|
|
|
|
private int queSize; // 队列长度
|
|
|
|
|
|
|
|
|
|
|
|
public ArrayQueue(int capacity)
|
|
|
|
public ArrayQueue(int capacity)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// 初始化数组
|
|
|
|
|
|
|
|
nums = new int[capacity];
|
|
|
|
nums = new int[capacity];
|
|
|
|
|
|
|
|
front = queSize = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 获取队列的容量 */
|
|
|
|
/* 获取队列的容量 */
|
|
|
|
public int capacity()
|
|
|
|
public int capacity()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return nums.Length;
|
|
|
|
return nums.Length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 获取队列的长度 */
|
|
|
|
/* 获取队列的长度 */
|
|
|
|
public int size()
|
|
|
|
public int size()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
int capacity = this.capacity();
|
|
|
|
return queSize;
|
|
|
|
// 由于将数组看作为环形,可能 rear < front ,因此需要取余数
|
|
|
|
|
|
|
|
return (capacity + rear - front) % capacity;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 判断队列是否为空 */
|
|
|
|
/* 判断队列是否为空 */
|
|
|
|
public bool isEmpty()
|
|
|
|
public bool isEmpty()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return rear - front == 0;
|
|
|
|
return queSize == 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 入队 */
|
|
|
|
/* 入队 */
|
|
|
|
public void offer(int num)
|
|
|
|
public void offer(int num)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (size() == capacity())
|
|
|
|
if (queSize == capacity())
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Console.WriteLine("队列已满");
|
|
|
|
Console.WriteLine("队列已满");
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 计算尾指针,指向队尾索引 + 1
|
|
|
|
|
|
|
|
// 通过取余操作,实现 rear 越过数组尾部后回到头部
|
|
|
|
|
|
|
|
int rear = (front + queSize) % capacity();
|
|
|
|
// 尾结点后添加 num
|
|
|
|
// 尾结点后添加 num
|
|
|
|
nums[rear] = num;
|
|
|
|
nums[rear] = num;
|
|
|
|
// 尾指针向后移动一位,越过尾部后返回到数组头部
|
|
|
|
queSize++;
|
|
|
|
rear = (rear + 1) % capacity();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 出队 */
|
|
|
|
/* 出队 */
|
|
|
|
public int poll()
|
|
|
|
public int poll()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
int num = peek();
|
|
|
|
int num = peek();
|
|
|
|
// 队头指针向后移动一位,若越过尾部则返回到数组头部
|
|
|
|
// 队首指针向后移动一位,若越过尾部则返回到数组头部
|
|
|
|
front = (front + 1) % capacity();
|
|
|
|
front = (front + 1) % capacity();
|
|
|
|
|
|
|
|
queSize--;
|
|
|
|
return num;
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 访问队首元素 */
|
|
|
|
/* 访问队首元素 */
|
|
|
|
public int peek()
|
|
|
|
public int peek()
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -1144,8 +1184,8 @@ comments: true
|
|
|
|
/* 基于环形数组实现的队列 */
|
|
|
|
/* 基于环形数组实现的队列 */
|
|
|
|
class ArrayQueue {
|
|
|
|
class ArrayQueue {
|
|
|
|
private var nums: [Int] // 用于存储队列元素的数组
|
|
|
|
private var nums: [Int] // 用于存储队列元素的数组
|
|
|
|
private var front = 0 // 头指针,指向队首
|
|
|
|
private var front = 0 // 队首指针,指向队首元素
|
|
|
|
private var rear = 0 // 尾指针,指向队尾 + 1
|
|
|
|
private var queSize = 0 // 队列长度
|
|
|
|
|
|
|
|
|
|
|
|
init(capacity: Int) {
|
|
|
|
init(capacity: Int) {
|
|
|
|
// 初始化数组
|
|
|
|
// 初始化数组
|
|
|
@ -1159,14 +1199,12 @@ comments: true
|
|
|
|
|
|
|
|
|
|
|
|
/* 获取队列的长度 */
|
|
|
|
/* 获取队列的长度 */
|
|
|
|
func size() -> Int {
|
|
|
|
func size() -> Int {
|
|
|
|
let capacity = capacity()
|
|
|
|
queSize
|
|
|
|
// 由于将数组看作为环形,可能 rear < front ,因此需要取余数
|
|
|
|
|
|
|
|
return (capacity + rear - front) % capacity
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 判断队列是否为空 */
|
|
|
|
/* 判断队列是否为空 */
|
|
|
|
func isEmpty() -> Bool {
|
|
|
|
func isEmpty() -> Bool {
|
|
|
|
rear - front == 0
|
|
|
|
queSize == 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 入队 */
|
|
|
|
/* 入队 */
|
|
|
@ -1175,19 +1213,22 @@ comments: true
|
|
|
|
print("队列已满")
|
|
|
|
print("队列已满")
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 计算尾指针,指向队尾索引 + 1
|
|
|
|
|
|
|
|
// 通过取余操作,实现 rear 越过数组尾部后回到头部
|
|
|
|
|
|
|
|
int rear = (front + queSize) % capacity();
|
|
|
|
// 尾结点后添加 num
|
|
|
|
// 尾结点后添加 num
|
|
|
|
nums[rear] = num
|
|
|
|
nums[rear] = num;
|
|
|
|
// 尾指针向后移动一位,越过尾部后返回到数组头部
|
|
|
|
queSize++;
|
|
|
|
rear = (rear + 1) % capacity()
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 出队 */
|
|
|
|
/* 出队 */
|
|
|
|
@discardableResult
|
|
|
|
@discardableResult
|
|
|
|
func poll() -> Int {
|
|
|
|
func poll() -> Int {
|
|
|
|
let num = peek()
|
|
|
|
let num = peek()
|
|
|
|
// 队头指针向后移动一位,若越过尾部则返回到数组头部
|
|
|
|
// 队首指针向后移动一位,若越过尾部则返回到数组头部
|
|
|
|
front = (front + 1) % capacity()
|
|
|
|
front = (front + 1) % capacity();
|
|
|
|
return num
|
|
|
|
queSize--;
|
|
|
|
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 访问队首元素 */
|
|
|
|
/* 访问队首元素 */
|
|
|
|