From 49d822b62638e27d06ad46b9277f6c2ad2068f4a Mon Sep 17 00:00:00 2001 From: doyo2024 <1158523053@qq.com> Date: Mon, 25 Nov 2024 11:02:18 +0800 Subject: [PATCH] Add files via upload --- posts/2024/2_advanced_C/code/ds_s3/queue.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/posts/2024/2_advanced_C/code/ds_s3/queue.c b/posts/2024/2_advanced_C/code/ds_s3/queue.c index 8a3d3a2..25d4015 100644 --- a/posts/2024/2_advanced_C/code/ds_s3/queue.c +++ b/posts/2024/2_advanced_C/code/ds_s3/queue.c @@ -17,6 +17,7 @@ void Init(); ElemType Front(); ElemType Pop(); void Push(ElemType data); +void Print(); int main() { @@ -38,6 +39,10 @@ int main() } else if (!strcmp(inst, "push")) { scanf("%d", &data); Push(data); + } else if (!strcmp(inst, "print")) { + Print(); + } else if (!strcmp(inst, "exit")) { + break; } else { printf("Error: Undefined Instruction.\n"); } @@ -83,10 +88,21 @@ void Push(ElemType data) { struct node * ptr = NewSpace; ptr->data = data; ptr->next = NULL; - if (QueueTail == NULL) { + if (QueueHead == NULL) { QueueHead = QueueTail = ptr; // 注意如果队列本来是空的,要把队头也指向新元素 } else { QueueTail->next = ptr; // 由于是从队尾入队,所以注意指针的指向 QueueTail = ptr; } } + +void Print() { + struct node *now = QueueHead; + int cnt = 0; + while (now != NULL) { + cnt++; + printf("%d ", now->data); + now = now->next; + } + printf("\nThere are %d elements in the list.\n", cnt); +}