队列:FIFO、假溢出与回绕
数组容量和可存元素数的约定要一致。
逐步查看固定输入下的数据变化。这里回放本地C程序的实际结果;下载源码后可修改输入并重新编译。
实际运行范围
当前固定输入有 23 项断言通过。本次实验创建的动态节点已全部释放。当前结果对应本章固定输入,改变输入后请重新编译核对。
编译器:Zig 0.14.1 cc;标准:C11;本轮环境:Windows。
编译:zig cc -std=c11 -Wall -Wextra -Werror -pedantic c-dsa-queue.c dsa.c -o demo.exe,再运行demo.exe。
本章完整入口
#include "test_support.h"
int main(void){
puts("MBSKY | mbsky.com | independently authored C11 case");
Queue q={0};int out;CHECK(!queue_pop(&q,&out));for(int i=1;i<=4;++i)CHECK(queue_push(&q,i*10));CHECK(!queue_push(&q,99));state("initial-full-reserved-slot",q.data,4);CHECK(queue_pop(&q,&out)&&out==10);CHECK(queue_pop(&q,&out)&&out==20);CHECK(queue_push(&q,50));CHECK(queue_push(&q,60));CHECK(q.rear==1);int popped[4];for(size_t i=0;i<4;++i)CHECK(queue_pop(&q,&popped[i]));equal_values(popped,(int[]){30,40,50,60},4);state("FIFO-after-wrap",popped,4);CHECK(!queue_pop(&q,&out));CHECK(queue_push(&q,-1));CHECK(queue_pop(&q,&out)&&out==-1);state("empty-after-legal-negative-one",(int[]){(int)q.front,(int)q.rear},2);
return finish();
}