diff --git a/codes/c/chapter_stack_and_queue/CMakeLists.txt b/codes/c/chapter_stack_and_queue/CMakeLists.txt index 8b2153ca5..58c58aa8a 100644 --- a/codes/c/chapter_stack_and_queue/CMakeLists.txt +++ b/codes/c/chapter_stack_and_queue/CMakeLists.txt @@ -1,3 +1,4 @@ add_executable(array_stack array_stack.c) add_executable(linkedlist_stack linkedlist_stack.c) -add_executable(array_queue array_queue.c) \ No newline at end of file +add_executable(array_queue array_queue.c) +add_executable(linkedlist_queue linkedlist_queue.c) diff --git a/codes/c/chapter_stack_and_queue/linkedlist_queue.c b/codes/c/chapter_stack_and_queue/linkedlist_queue.c new file mode 100644 index 000000000..cd0da6952 --- /dev/null +++ b/codes/c/chapter_stack_and_queue/linkedlist_queue.c @@ -0,0 +1,128 @@ +/** + * File: linkedlist_queue.c + * Created Time: 2023-03-13 + * Author: Gonglja (glj0@outlook.com) + */ + +#include "../include/include.h" + +/* 基于链表实现的队列 */ +struct LinkedListQueue { + ListNode *front, *rear; + int queSize; +}; + +typedef struct LinkedListQueue LinkedListQueue; + +/* 构造方法 */ +LinkedListQueue *newLinkedListQueue() { + LinkedListQueue *queue = (LinkedListQueue *) malloc(sizeof(LinkedListQueue)); + queue->front = NULL; + queue->rear = NULL; + queue->queSize = 0; +} + +/* 析构方法 */ +void delLinkedListQueue(LinkedListQueue *queue) { + // 释放所有结点 + for (int i=0; iqueSize && queue->front != NULL; i++) { + ListNode *tmp = queue->front; + queue->front = queue->front->next; + free(tmp); + } + // 释放 queue 结构体 + free(queue); +} + +/* 获取队列的长度 */ +int size(LinkedListQueue *queue) { + return queue->queSize; +} + +/* 判断队列是否为空 */ +bool empty(LinkedListQueue *queue) { + return (size(queue) == 0); +} + +/* 入队 */ +void push(LinkedListQueue *queue, int num) { + // 尾结点处添加 node + ListNode *node = newListNode(num); + // 如果队列为空,则令头、尾结点都指向该结点 + if (queue->front == NULL) { + queue->front = node; + queue->rear = node; + } + // 如果队列不为空,则将该结点添加到尾结点后 + else { + queue->rear->next = node; + queue->rear = node; + } + queue->queSize++; +} + +/* 访问队首元素 */ +int peek(LinkedListQueue *queue) { + assert(size(queue) && queue->front); + return queue->front->val; +} + +/* 出队 */ +void poll(LinkedListQueue *queue) { + int num = peek(queue); + ListNode *tmp = queue->front; + queue->front = queue->front->next; + free(tmp); + queue->queSize--; +} + +/* 打印队列 */ +void printLinkedListQueue(LinkedListQueue *queue) { + int arr[queue->queSize]; + // 拷贝链表中的数据到数组 + int i; + ListNode *node; + for (i=0, node = queue->front; i < queue->queSize && queue->front != queue->rear; i++){ + arr[i] = node->val; + node = node->next; + } + printArray(arr, queue->queSize); +} + +/* Driver Code */ +int main() { + /* 初始化队列 */ + LinkedListQueue *queue = newLinkedListQueue(); + + /* 元素入队 */ + push(queue, 1); + push(queue, 3); + push(queue, 2); + push(queue, 5); + push(queue, 4); + printf("队列 queue = "); + printLinkedListQueue(queue); + + /* 访问队首元素 */ + int peekNum = peek(queue); + printf("队首元素 peek = %d\r\n", peekNum); + + /* 元素出队 */ + poll(queue); + printf("出队元素 poll = %d,出队后 queue = ", peekNum); + printLinkedListQueue(queue); + + /* 获取队列的长度 */ + int queueSize = size(queue); + printf("队列长度 size = %d\r\n", queueSize); + + /* 判断队列是否为空 */ + bool isEmpty = empty(queue); + printf("队列是否为空 = %s\r\n", isEmpty ? "true" : "false"); + + // 释放内存 + delLinkedListQueue(queue); + + return 0; +} +