📚 静态队列(Static Queue)详解教程
一、前言a
好吧好吧,我又来了。一天更新两条这一块!!今天来看看队列吧,队列分为静态队列和循环队列,他们统称为队列。
那么队列是啥?
队列其实就是一条排的长长的队伍,静态队列就像是规定了每天只可以有几个人排队的私人订制餐厅,而循环队列就是接受任何人的超级大餐厅,而且可以无限排队。
今天就浅浅讲讲 静态队列!!!!
本期代码可以在我的github上看到,感兴趣的小伙伴可以去看看,点点starred
点击跳转 >> Github
二、原理
什么是静态队列(Static Queue)?
静态队列,就是用固定长度的数组做成的队列。
它的特点非常简单:
-
只能从尾巴加,只能从头部删
-
指针往前走,不回头
-
空间不能复用
因为它像一条只能往前走、不能循环、不能回头的队伍,用完前面的空间就浪费了,所以我们也可以叫它 死队列(Dead Queue)。
用生活例子讲:排队上车
想象一辆只能坐 5 个人的小巴车:
-
上车的人从最后面排队(入队)
-
下车的人从最前面离开(出队)
-
车子座位固定 5 个,不会变多
一开始:
[ 空, 空, 空, 空, 空 ]
上来 3 个人:
[ 1号, 2号, 3号, 空, 空 ]
又上来 2 个人,满了:
[ 1号, 2号, 3号, 4号, 5号 ]
这时 1 号下车,前面空出一个位置:
[ 空, 2号, 3号, 4号, 5号 ]
但问题来了:
尾巴已经到底了,新的人不能插到前面空位置!
所以 —— 明明有空位,却不能上人!
这就是静态队列最大的特点:空间不能复用。
三、Zig 代码实现
在 Zig 中,是没有宏定义这么一说的,因为 Zig 要达到无隐式结构。所以我们要用常量来代替宏定义。
C 语言写法:
#define MAX 5 // 队列最大容量
Zig 写法:
const MAX = 5;
Zig 里面的 const 会自动推导类型,和 C 的宏定义使用效果基本一致。
Zig 完整代码
// 导入Zig标准库
// Import Zig standard library
const std = @import("std");
// 引入调试打印函数
// Import debug print function
const print = @import("std").debug.print;
// 定义队列最大容量为5
// Define maximum queue capacity as 5
const MAX = 5;
// 定义队列数组,使用MAX指定长度
// Define queue array with length specified by MAX
// 【死队列 Dead Queue】:空间无法复用,队尾满后无法再添加元素
// 【Dead Queue】: Space cannot be reused, no more elements can be added after rear is full
var queue: [MAX]u32 = undefined;
// 队头指针,初始为0
// Front pointer, initial value is 0
var front: u32 = 0;
// 队尾指针,初始为0
// Rear pointer, initial value is 0
var rear: u32 = 0;
// 入队函数:向队列尾部添加元素
// Enqueue function: Add element to the tail of the queue
pub fn enqueue(val: u32) void {
// 判断队尾是否到达最大容量,即队列已满
// Check if rear reaches maximum capacity, meaning queue is full
if (rear >= MAX) {
// 打印队列满提示信息
// Print queue full prompt
print("队列满了 -- Queue is full\n", .{});
return;
}
// 将元素存入当前队尾位置
// Store element into current rear position
queue[rear] = val;
// 队尾指针向后移动一位
// Move rear pointer backward by one position
rear = rear + 1;
}
// 出队函数:从队列头部删除元素
// Dequeue function: Remove element from the head of the queue
pub fn dequeue() void {
// 判断队头与队尾是否相等,即队列为空
// Check if front equals rear, meaning queue is empty
if (front == rear) {
// 打印队列为空提示信息
// Print queue empty prompt
print("队列是空的 -- Queue is empty", .{});
}
front = front + 1;
}
// 打印队列中所有有效元素
// Print all valid elements in the queue
pub fn printQueue() void {
// 打印队列标题
// Print queue title
print("当前队列 -- Current Queue:", .{});
// 从队头遍历到队尾,输出每个元素
// Traverse from front to rear and output each element
for (front..rear) |i| {
print("{} ", .{queue[i]});
}
// 换行,使输出更整洁
// Print new line for cleaner output
print("\n", .{});
}
// 主函数:程序入口,用于测试队列功能
// Main function: Program entry point for testing queue functions
pub fn main() void {
// 依次入队三个元素
// Enqueue three elements in sequence
enqueue(10);
enqueue(20);
enqueue(30);
// 打印当前队列
// Print current queue
printQueue();
// 继续入队元素,直到队列满
// Keep enqueuing elements until queue is full
enqueue(40);
enqueue(50);
enqueue(60);
// 打印已满的队列
// Print full queue
printQueue();
// 执行一次出队操作
// Perform one dequeue operation
dequeue();
// 尝试入队新元素(死队列无法加入)
// Try to enqueue new element (Dead queue cannot add)
enqueue(70);
// 打印最终队列
// Print final queue
printQueue();
}
}
这些代码看起来特别多,其实全是注释,有用的核心代码加起来不到 50 行。
四、C3 代码实现
在 C3 中,也是没有宏定义的,之前我误写了 #define 结果直接报错。
C3 里面的 const 和 Zig 几乎一样,可以自动推导类型。
C3 完整代码
// 导入C3语言标准库
// Import C3 standard library
import std;
// 定义队列最大容量为5
// Define maximum queue capacity as 5
const MAX = 5;
// 定义整型数组队列,长度为MAX
// Define integer array queue with length MAX
// 【死队列 Dead Queue】:空间无法复用,队尾满后无法再添加元素
// 【Dead Queue】: Space cannot be reused, no more elements can be added after rear is full
int[MAX] queue;
// 队头指针,初始值为0
// Front pointer, initial value is 0
int front = 0;
// 队尾指针,初始值为0
// Rear pointer, initial value is 0
int rear = 0;
// 入队函数:向队列尾部添加元素
// Enqueue function: Add element to the tail of the queue
fn void enqueue(int val)
{
// 判断队列是否已满
// Check if the queue is full
if(rear >= MAX)
{
// 打印队列满提示信息
// Print queue full message
io::printn("队列满了 -- Queue is full");
return;
}
// 将元素存入队尾位置,然后队尾指针后移
// Store the element at the tail position, then move the rear pointer backward
queue[rear++] = val;
}
// 出队函数:从队列头部删除元素
// Dequeue function: Remove element from the head of the queue
fn void dequeue()
{
// 判断队列是否为空
// Check if the queue is empty
if(front == rear)
{
// 打印队列为空提示信息
// Print queue empty message
io::printn("队列是空的 -- Queue is empty");
}
// 队头指针后移,实现出队逻辑
// Move front pointer backward to implement dequeue logic
queue[front++];
}
// 打印队列中所有有效元素
// Print all valid elements in the queue
fn void printQueue()
{
// 从队头遍历到队尾,输出每个元素
// Traverse from front to rear and output each element
for(int i = front;i<rear;i++)
{
io::printf("%d ",queue[i]);
}
// 换行,使输出更整洁
// Print new line for cleaner output
io::printn("");
}
// 主函数:程序入口,用于测试队列功能
// Main function: Program entry point for testing queue functions
fn void main()
{
// 初始化队列数组,将所有元素赋值为0
// Initialize queue array and set all elements to 0
for(int i=0;i<MAX;i++)
{
queue[i] = 0;
}
// 入队元素10、20、30
// Enqueue elements 10, 20, 30
enqueue(10);
enqueue(20);
enqueue(30);
// 打印当前队列
// Print current queue
printQueue();
// 继续入队元素40、50
// Continue enqueuing elements 40, 50
enqueue(40);
enqueue(50);
// 尝试入队元素60(队列已满)
// Try enqueuing element 60 (Queue is full)
enqueue(60);
// 打印已满的队列
// Print full queue
printQueue();
// 执行一次出队操作
// Perform one dequeue operation
dequeue();
// 尝试入队新元素(死队列无法加入)
// Try to enqueue new element (Dead queue cannot add)
enqueue(70);
// 打印最终队列
// Print final queue
printQueue();
}
五、总结
静态队列是数据结构中最基础、最简单的队列类型,核心是用固定长度的数组实现,遵循**先进先出(FIFO)**原则,也是新手入门队列概念的首选。
其核心特征可简单概括为 固定、单向、不可复用:
-
固定:队列容量从创建时就确定,无法动态调整
-
单向:元素只能从队尾(rear)加入、从队头(front)删除,两个指针仅向后移动、无法回头
-
不可复用:即使删除元素空出前面空间,也无法再次利用,就像固定座位的小巴车,前排空了也不能让新人上车,因此也被称为死队列
静态队列的优势:逻辑简单、代码易实现、无需复杂内存管理,极度适合新手理解队列核心操作。
静态队列的缺点:空间利用率低、存在浪费、无法满足实际开发的空间复用需求。
总而言之,静态队列是队列的入门版,掌握它的三大核心规则:
固定数组、单向操作、不可复用,
是学习更实用的动态队列、循环队列的基础,也是踏入数据结构世界的关键一步。