From 2f73e9b770f20a1cbcba4e1a228744f41c2fb6c9 Mon Sep 17 00:00:00 2001 From: Xi-Row Date: Sun, 24 Nov 2024 23:10:01 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=97=E8=A1=A8=E7=AB=A0=E8=8A=82=E4=B8=AD,C?= =?UTF-8?q?=E8=AF=AD=E8=A8=80=E5=AE=9E=E7=8E=B0=E5=88=97=E8=A1=A8=E6=89=93?= =?UTF-8?q?=E5=8D=B0=E6=97=B6=E6=9C=89=E8=AF=AF,=E6=95=85=E4=BD=9C?= =?UTF-8?q?=E6=AD=A4=E4=BF=AE=E6=94=B9!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- codes/c/chapter_stack_and_queue/array_queue.c | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/codes/c/chapter_stack_and_queue/array_queue.c b/codes/c/chapter_stack_and_queue/array_queue.c index 84b3eae6d..3f28a893f 100644 --- a/codes/c/chapter_stack_and_queue/array_queue.c +++ b/codes/c/chapter_stack_and_queue/array_queue.c @@ -73,7 +73,14 @@ int pop(ArrayQueue *queue) { queue->queSize--; return num; } - +void print_queue(ArrayQueue *queue) +{ + int tmp =queue->front; + int rear =(queue->front+queue->queSize-1)%queue->queCapacity; + for(;tmp != rear;tmp++,tmp%=queue->queCapacity) + printf("%d,",queue->nums[tmp]); + printf("%d]\n",queue->nums[rear]); +} /* Driver Code */ int main() { /* 初始化队列 */ @@ -86,7 +93,7 @@ int main() { push(queue, 2); push(queue, 5); push(queue, 4); - printf("队列 queue = "); + printf("队列 queue = ["); printArray(queue->nums, queue->queSize); /* 访问队首元素 */ @@ -95,8 +102,8 @@ int main() { /* 元素出队 */ peekNum = pop(queue); - printf("出队元素 pop = %d ,出队后 queue = ", peekNum); - printArray(queue->nums, queue->queSize); + printf("出队元素 pop = %d ,出队后 queue = [", peekNum); + print_queue(queue); /* 获取队列的长度 */ int queueSize = size(queue); @@ -110,12 +117,12 @@ int main() { for (int i = 0; i < 10; i++) { push(queue, i); pop(queue); - printf("第 %d 轮入队 + 出队后 queue = ", i); - printArray(queue->nums, queue->queSize); + printf("第 %d 轮入队 + 出队后 queue = [", i); + print_queue(queue); } // 释放内存 delArrayQueue(queue); return 0; -} +} \ No newline at end of file