From 8bb1193a797e5f07286014be2b7fd7dc430993e3 Mon Sep 17 00:00:00 2001 From: Jdhggg <24016020834@stu.nsu.edu.cn> Date: Wed, 21 May 2025 12:46:10 +0800 Subject: [PATCH] kong Co-Authored-By: JdhByte <111557398+Jdhggg@users.noreply.github.com> --- Makefile | 8 +-- Project2.vcxproj | 2 + Project2.vcxproj.filters | 6 +++ array_queue.c | 107 +++++++++++++++++++-------------------- main.c | 45 ++++++++++------ 5 files changed, 92 insertions(+), 76 deletions(-) diff --git a/Makefile b/Makefile index b310f4f..bc70509 100644 --- a/Makefile +++ b/Makefile @@ -21,11 +21,7 @@ OBJ_EXT = .o # 定义源文件列表 SRC_FILES = \ - array_queue.c \ - array_stack.c \ - linked_list_stack.c \ - linked_list.c \ - sq_list.c \ + heap.c \ main.c # 使用正则表达式替换源文件后缀为.o,生成目标文件列表 @@ -48,4 +44,4 @@ $(OBJ_DIR)/%$(OBJ_EXT): $(SRC_DIR)/%$(SRC_EXT) # 清理目标文件和可执行文件 clean: - rm -rf $(OBJ_DIR) $(TARGET) \ No newline at end of file + rm -rf $(OBJ_DIR) $(TARGET) diff --git a/Project2.vcxproj b/Project2.vcxproj index 9556046..fa7e553 100644 --- a/Project2.vcxproj +++ b/Project2.vcxproj @@ -21,6 +21,7 @@ + @@ -30,6 +31,7 @@ + diff --git a/Project2.vcxproj.filters b/Project2.vcxproj.filters index 652c5cc..2a140bc 100644 --- a/Project2.vcxproj.filters +++ b/Project2.vcxproj.filters @@ -36,6 +36,9 @@ 源文件 + + 源文件 + @@ -56,5 +59,8 @@ 头文件 + + 头文件 + \ No newline at end of file diff --git a/array_queue.c b/array_queue.c index fcc66e6..40d15b0 100644 --- a/array_queue.c +++ b/array_queue.c @@ -1,33 +1,27 @@ #include #include +#include // 添加此行以确保 memset 函数可用 #include "array_queue.h" -/*@------------- - # 设计思路更新(原版漏洞太多): - 设计圆环式队列,当rear == size时,循环到数组的[0]位,当然这样的队列仍然遵循`先进先出原则` - ## 实现思路: - 通过取模运算,使得rear和front都在数组的[0,size-1]范围内,这样就能实现循环队列的功能. - # 有点解析: - 在传统的队列中,当队尾到达队列的末尾时,即使队列前面还有空闲空间,也无法继续入队,导致空间浪费。而环形队列通过取模运算,\n - 使得队尾指针可以在到达队列末尾后,从头开始继续使用队列的空闲空间,从而提高了空间的利用率。 -@-------------*/ - // 初始化队列 array_queue *init_array_queue() { - array_queue *q = (array_queue *)malloc(sizeof(array_queue)); - if (q == NULL) - {printf("error: malloc failed[From init_queue]"); return NULL;} - memset(q,0,sizeof(array_queue)); - q->front = 0; - q->rear = 0; - q->size = 0; - return q; + array_queue *q = (array_queue *)malloc(sizeof(array_queue)); + if (q == NULL) + { + printf("error: malloc failed[From init_queue]"); + return NULL; + } + memset(q, 0, sizeof(array_queue)); + q->front = 0; + q->rear = 0; + q->size = 0; + return q; } // 判空 -int empty_array_queue(array_queue *q) +int empty_array_queue(array_queue* q) { if (q->size == 0) return 1; @@ -35,55 +29,58 @@ int empty_array_queue(array_queue *q) return 0; } -// 判断满 + + +// 判满 int full_array_queue(array_queue *q) { - if (q->size == MAX_QUEUE) - return 1; - else - return 0; + if (q->size == MAX_QUEUE) + return 1; + else + return 0; } // 入队 void push_array_queue(array_queue *q, elem_type value) { - if (full_array_queue(q)) - {printf("error: queue is full[From push_queue]"); return;} - q->data[q->rear] = value; - q->rear = (q->front + q->size + 1) % MAX_QUEUE; - q->size++; -} + if (full_array_queue(q)) + { + printf("error: queue is full[From push_queue]"); + return; + } + q->data[q->rear] = value; + q->rear = (q->front + q->size + 1) % MAX_QUEUE; + q->size++; +} // 出队 elem_type pop_array_queue(array_queue *q) { - if (empty_array_queue(q)) - {printf("error: queue is empty[From pop_queue]"); return -1;} - elem_type value = q->data[q->front]; - q->front = (q->front + 1) % MAX_QUEUE; - q->size--; - return value; + if (empty_array_queue(q)) + { + printf("error: queue is empty[From pop_queue]"); + return -1; + } + elem_type value = q->data[q->front]; + q->front = (q->front + 1) % MAX_QUEUE; + q->size--; + return value; } // 打印队列 void print_array_queue(array_queue *q) { - if (empty_array_queue(q)) - { - printf("error :\n"); - return ; - } - int j = q->front; - for (int i=0;isize;i++) - { - printf("["); - printf("%d ",q->data[j%MAX_QUEUE]); - printf("]\n"); - j++; - } -} - - -// void return def => fuck shit -// if for while malloc def sleep pause NULL -// #include $time_noon \ No newline at end of file + if (empty_array_queue(q)) + { + printf("error :\n"); + return; + } + int j = q->front; + for (int i = 0; i < q->size; i++) + { + printf("["); + printf("%d ", q->data[j % MAX_QUEUE]); + printf("]\n"); + j++; + } +} \ No newline at end of file diff --git a/main.c b/main.c index d3b53e0..bfce666 100644 --- a/main.c +++ b/main.c @@ -1,7 +1,8 @@ +#define _CRT_SECURE_NO_WARNINGS #include #include #include -#include "binary_tree.h" +#include "heap.h" int main(void) { // sq_list* L = init_sq_list(); @@ -101,20 +102,34 @@ int main(void) { pop_link_queue(q); print_link_queue(q);*/ - tree_node *root = init_binary_tree(1); - tree_node *node1 = init_binary_tree(2); - tree_node *node2 = init_binary_tree(3); - tree_node *node3 = init_binary_tree(4); - tree_node *node4 = init_binary_tree(5); - tree_node *node5 = init_binary_tree(6); - tree_node *node6 = init_binary_tree(7); - root->left = node1; - root->right = node2; - node1->left = node3; - node1->right = node4; - node2->left = node5; - node2->right = node6; - traverse_tree(root); + //tree_node *root = init_binary_tree(1); + //tree_node *node1 = init_binary_tree(2); + //tree_node *node2 = init_binary_tree(3); + //tree_node *node3 = init_binary_tree(4); + //tree_node *node4 = init_binary_tree(5); + //tree_node *node5 = init_binary_tree(6); + //tree_node *node6 = init_binary_tree(7); + //root->left = node1; + //root->right = node2; + //node1->left = node3; + //node1->right = node4; + //node2->left = node5; + //node2->right = node6; + //traverse_tree(root); + + + +// heap +heap* tree = heap_init(); +int flog_s = 0; +scanf("%d", &flog_s); +for (int i = 1; i <= flog_s; i++) +{ + heap_push(tree, i); +} +heap_print(tree, 0); + + printf("Hello World!\n"); system("pause"); return 0;