2022-11-29 01:10:22 +08:00
|
|
|
// File: array_queue.go
|
|
|
|
// Created Time: 2022-11-28
|
|
|
|
// Author: Reanon (793584285@qq.com)
|
|
|
|
|
|
|
|
package chapter_stack_and_queue
|
|
|
|
|
2022-12-03 20:25:24 +08:00
|
|
|
/* 基于环形数组实现的队列 */
|
2023-01-03 14:39:31 +08:00
|
|
|
type arrayQueue struct {
|
2023-02-01 03:23:29 +08:00
|
|
|
nums []int // 用于存储队列元素的数组
|
|
|
|
front int // 队首指针,指向队首元素
|
|
|
|
queSize int // 队列长度
|
|
|
|
queCapacity int // 队列容量(即最大容纳元素数量)
|
2022-11-29 01:10:22 +08:00
|
|
|
}
|
|
|
|
|
2023-02-09 04:45:06 +08:00
|
|
|
/* 初始化队列 */
|
2023-02-01 03:23:29 +08:00
|
|
|
func newArrayQueue(queCapacity int) *arrayQueue {
|
2023-01-03 14:39:31 +08:00
|
|
|
return &arrayQueue{
|
2023-02-01 03:23:29 +08:00
|
|
|
nums: make([]int, queCapacity),
|
|
|
|
queCapacity: queCapacity,
|
|
|
|
front: 0,
|
|
|
|
queSize: 0,
|
2022-11-29 01:10:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-09 04:45:06 +08:00
|
|
|
/* 获取队列的长度 */
|
2023-01-03 14:39:31 +08:00
|
|
|
func (q *arrayQueue) size() int {
|
2023-02-01 03:23:29 +08:00
|
|
|
return q.queSize
|
2022-11-29 01:10:22 +08:00
|
|
|
}
|
|
|
|
|
2023-02-09 04:45:06 +08:00
|
|
|
/* 判断队列是否为空 */
|
2023-01-03 14:39:31 +08:00
|
|
|
func (q *arrayQueue) isEmpty() bool {
|
2023-02-01 03:23:29 +08:00
|
|
|
return q.queSize == 0
|
2022-11-29 01:10:22 +08:00
|
|
|
}
|
|
|
|
|
2023-02-09 04:45:06 +08:00
|
|
|
/* 入队 */
|
2023-02-02 01:43:01 +08:00
|
|
|
func (q *arrayQueue) push(num int) {
|
2023-02-01 03:23:29 +08:00
|
|
|
// 当 rear == queCapacity 表示队列已满
|
|
|
|
if q.queSize == q.queCapacity {
|
2022-11-29 01:10:22 +08:00
|
|
|
return
|
|
|
|
}
|
2023-12-28 18:06:09 +08:00
|
|
|
// 计算队尾指针,指向队尾索引 + 1
|
|
|
|
// 通过取余操作实现 rear 越过数组尾部后回到头部
|
2023-02-01 03:23:29 +08:00
|
|
|
rear := (q.front + q.queSize) % q.queCapacity
|
2023-02-16 02:17:15 +08:00
|
|
|
// 将 num 添加至队尾
|
2023-02-01 03:23:29 +08:00
|
|
|
q.nums[rear] = num
|
|
|
|
q.queSize++
|
2022-11-29 01:10:22 +08:00
|
|
|
}
|
|
|
|
|
2023-02-09 04:45:06 +08:00
|
|
|
/* 出队 */
|
2023-03-13 21:58:21 +08:00
|
|
|
func (q *arrayQueue) pop() any {
|
2023-02-01 03:23:29 +08:00
|
|
|
num := q.peek()
|
2024-05-31 17:38:27 +08:00
|
|
|
if num == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-12-28 18:06:09 +08:00
|
|
|
// 队首指针向后移动一位,若越过尾部,则返回到数组头部
|
2023-02-01 03:23:29 +08:00
|
|
|
q.front = (q.front + 1) % q.queCapacity
|
|
|
|
q.queSize--
|
|
|
|
return num
|
2022-11-29 01:10:22 +08:00
|
|
|
}
|
|
|
|
|
2023-02-09 04:45:06 +08:00
|
|
|
/* 访问队首元素 */
|
2023-01-03 14:39:31 +08:00
|
|
|
func (q *arrayQueue) peek() any {
|
|
|
|
if q.isEmpty() {
|
2022-11-29 01:10:22 +08:00
|
|
|
return nil
|
|
|
|
}
|
2023-02-01 03:23:29 +08:00
|
|
|
return q.nums[q.front]
|
2022-11-29 01:10:22 +08:00
|
|
|
}
|
|
|
|
|
2023-02-09 04:45:06 +08:00
|
|
|
/* 获取 Slice 用于打印 */
|
2023-01-03 14:39:31 +08:00
|
|
|
func (q *arrayQueue) toSlice() []int {
|
2023-02-01 03:23:29 +08:00
|
|
|
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]
|
2022-11-29 01:10:22 +08:00
|
|
|
}
|