From ce1f4aec3e27c09bad495a164be00f07d958bff3 Mon Sep 17 00:00:00 2001 From: Didnelpsun <2675350965@qq.com> Date: Wed, 21 Apr 2021 23:55:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E9=98=9F=E5=88=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Code/Code.vcxproj | 4 + Code/Code.vcxproj.filters | 18 +++++ Code/link_queue.h | 154 ++++++++++++++++++++++++++++++++++++++ Data-Structrue/queue.md | 5 +- 4 files changed, 180 insertions(+), 1 deletion(-) create mode 100644 Code/link_queue.h diff --git a/Code/Code.vcxproj b/Code/Code.vcxproj index 16c93f9..fd762cf 100644 --- a/Code/Code.vcxproj +++ b/Code/Code.vcxproj @@ -141,12 +141,16 @@ + + + + diff --git a/Code/Code.vcxproj.filters b/Code/Code.vcxproj.filters index 2dc6959..54435f4 100644 --- a/Code/Code.vcxproj.filters +++ b/Code/Code.vcxproj.filters @@ -21,6 +21,9 @@ 源文件 + + 头文件 + @@ -29,5 +32,20 @@ 头文件 + + 头文件 + + + 头文件 + + + 头文件 + + + 头文件 + + + 头文件 + \ No newline at end of file diff --git a/Code/link_queue.h b/Code/link_queue.h new file mode 100644 index 0000000..a14f176 --- /dev/null +++ b/Code/link_queue.h @@ -0,0 +1,154 @@ +#include +#include +#include "head.h" + +// н +typedef struct LinkQueueNode { + element_type data; + struct LinkQueueNode* next; +} LinkQueueNode; + +// +typedef struct { + // еĶͷָͶβָ + LinkQueueNode* front, * rear; +} LinkQueue; + +// ʼͷڵ +int InitLinkQueueWithHead(LinkQueue* queue) { + // ʼʱͷָͶβָ붼ָͷ + queue->front = queue->rear = (LinkQueueNode*)malloc(sizeof(LinkQueueNode)); + if (queue->front) { + queue->front->next = NULL; + } + if (queue->front == NULL || queue->rear == NULL) { + printf("InitLinkQueueWithHead:ʼڴʧܣ"); + return 1; + } + return 0; +} + +// ʼͷڵ +int InitLinkQueueWithoutHead(LinkQueue* queue) { + // ʼʱͷβ㶼ָNULL + queue->front = NULL; + queue->rear = NULL; +} + +// жϴͷǷΪ +int IsLinkQueueEmptyWithHead(LinkQueue* queue) { + // ͷָͶβָָͬһط + if (queue->front == queue->rear) { + return 1; + } + else { + return 0; + } +} + +// жϲͷǷΪ +int IsLinkQueueEmptyWithoutHead(LinkQueue* queue) { + // ͷָͶβָһΪNULL + if (queue->front == NULL) { + return 1; + } + else { + return 0; + } +} + +// ͷڵ +int EnterLinkQueueWithHead(LinkQueue* queue, element_type elem) { + LinkQueueNode* p = (LinkQueueNode*)malloc(sizeof(LinkQueueNode)); + if (p) { + // ݸֵ + p->data = elem; + // nextָNULL + p->next = NULL; + // ½뵽rear֮ + queue->rear->next = p; + // ޸βָ + queue->rear = p; + return 0; + } + else { + printf("EnterLinkQueueWithHead:㽨ʧܣ"); + return 1; + } +} + +// ͷڵ +int EnterLinkQueueWithoutHead(LinkQueue* queue, element_type elem) { + LinkQueueNode* p = (LinkQueueNode*)malloc(sizeof(LinkQueueNode)); + if (p) { + // ݸֵ + p->data = elem; + // nextָNULL + p->next = NULL; + // ǿնвһԪ + if (queue->front == NULL) { + // ͷָ½Ԫ + queue->front = p; + } + else { + // ½뵽rear֮ + queue->rear->next = p; + } + // ޸βָ + queue->rear = p; + return 0; + } + else { + printf("EnterLinkQueueWithHead:㽨ʧܣ"); + return 1; + } +} + +// ͷӳ +int ExitLinkQueueWithHead(LinkQueue* queue, element_type* elem) { + // ǿն + if (queue->front == queue->rear) { + printf("ExitLinkQueueWithHead:ѿ޷ӣ"); + return 1; + } + // һֵΪͷ㣨ͷ㣩һ + LinkQueueNode* p = queue->front->next; + // صһԪ + *elem = p->data; + // ޸ͷnextָָ + // һ + queue->front->next = p->next; + // һ + if (queue->rear == p) { + queue->rear = queue->front; + } + // mallocĿռͷ + free(p); + return 0; +} + +// ͷӳ +int ExitLinkQueueWithoutHead(LinkQueue* queue, element_type* elem) { + // ǿն + if (queue->front == NULL) { + printf("ExitLinkQueueWithoutHead:ѿ޷ӣ"); + return 1; + } + // һֵΪͷ + LinkQueueNode* p = queue->front; + // صһԪ + *elem = p->data; + // ޸ͷָָ + // һ + queue->front = p->next; + // һ + if (queue->rear == p) { + // ͷβָ붼ֵΪNULL + queue->front = NULL; + queue->rear = NULL; + } + // mallocĿռͷ + free(p); + return 0; +} + diff --git a/Data-Structrue/queue.md b/Data-Structrue/queue.md index feea57a..55684a1 100644 --- a/Data-Structrue/queue.md +++ b/Data-Structrue/queue.md @@ -26,4 +26,7 @@ 当如果我们必须保证所有的存储空间被利用,可以定义一个size表明队列当前的长度,就可以完全利用所有空间。 -同理我们可以定义一个int类型的tag,当进行删除操作就置tag为0,插入操作时置tag为1,只有删除才可能队空,只有插入才可能队满,所以就可以根据这个来判断。 \ No newline at end of file +同理我们可以定义一个int类型的tag,当进行删除操作就置tag为0,插入操作时置tag为1,只有删除才可能队空,只有插入才可能队满,所以就可以根据这个来判断。 + +## 链队 +