kong
Co-Authored-By: JdhByte <111557398+Jdhggg@users.noreply.github.com>
This commit is contained in:
parent
fb3595a829
commit
8bb1193a79
6
Makefile
6
Makefile
@ -21,11 +21,7 @@ OBJ_EXT = .o
|
|||||||
|
|
||||||
# 定义源文件列表
|
# 定义源文件列表
|
||||||
SRC_FILES = \
|
SRC_FILES = \
|
||||||
array_queue.c \
|
heap.c \
|
||||||
array_stack.c \
|
|
||||||
linked_list_stack.c \
|
|
||||||
linked_list.c \
|
|
||||||
sq_list.c \
|
|
||||||
main.c
|
main.c
|
||||||
|
|
||||||
# 使用正则表达式替换源文件后缀为.o,生成目标文件列表
|
# 使用正则表达式替换源文件后缀为.o,生成目标文件列表
|
||||||
|
@ -21,6 +21,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="array_queue.c" />
|
<ClCompile Include="array_queue.c" />
|
||||||
<ClCompile Include="array_stack.c" />
|
<ClCompile Include="array_stack.c" />
|
||||||
|
<ClCompile Include="heap.c" />
|
||||||
<ClCompile Include="linked_list.c" />
|
<ClCompile Include="linked_list.c" />
|
||||||
<ClCompile Include="linked_list_stack.c" />
|
<ClCompile Include="linked_list_stack.c" />
|
||||||
<ClCompile Include="linked_queue.c" />
|
<ClCompile Include="linked_queue.c" />
|
||||||
@ -30,6 +31,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="array_queue.h" />
|
<ClInclude Include="array_queue.h" />
|
||||||
<ClInclude Include="array_stack.h" />
|
<ClInclude Include="array_stack.h" />
|
||||||
|
<ClInclude Include="heap.h" />
|
||||||
<ClInclude Include="linked_list.h" />
|
<ClInclude Include="linked_list.h" />
|
||||||
<ClInclude Include="linked_list_stack.h" />
|
<ClInclude Include="linked_list_stack.h" />
|
||||||
<ClInclude Include="linked_queue.h" />
|
<ClInclude Include="linked_queue.h" />
|
||||||
|
@ -36,6 +36,9 @@
|
|||||||
<ClCompile Include="linked_queue.c">
|
<ClCompile Include="linked_queue.c">
|
||||||
<Filter>源文件</Filter>
|
<Filter>源文件</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="heap.c">
|
||||||
|
<Filter>源文件</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="sq_list.h">
|
<ClInclude Include="sq_list.h">
|
||||||
@ -56,5 +59,8 @@
|
|||||||
<ClInclude Include="linked_queue.h">
|
<ClInclude Include="linked_queue.h">
|
||||||
<Filter>头文件</Filter>
|
<Filter>头文件</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="heap.h">
|
||||||
|
<Filter>头文件</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
103
array_queue.c
103
array_queue.c
@ -1,33 +1,27 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h> // 添加此行以确保 memset 函数可用
|
||||||
#include "array_queue.h"
|
#include "array_queue.h"
|
||||||
|
|
||||||
/*@-------------
|
|
||||||
# 设计思路更新(原版漏洞太多):
|
|
||||||
设计圆环式队列,当rear == size时,循环到数组的[0]位,当然这样的队列仍然遵循`先进先出原则`
|
|
||||||
## 实现思路:
|
|
||||||
通过取模运算,使得rear和front都在数组的[0,size-1]范围内,这样就能实现循环队列的功能.
|
|
||||||
# 有点解析:
|
|
||||||
在传统的队列中,当队尾到达队列的末尾时,即使队列前面还有空闲空间,也无法继续入队,导致空间浪费。而环形队列通过取模运算,\n
|
|
||||||
使得队尾指针可以在到达队列末尾后,从头开始继续使用队列的空闲空间,从而提高了空间的利用率。
|
|
||||||
@-------------*/
|
|
||||||
|
|
||||||
|
|
||||||
// 初始化队列
|
// 初始化队列
|
||||||
array_queue *init_array_queue()
|
array_queue *init_array_queue()
|
||||||
{
|
{
|
||||||
array_queue *q = (array_queue *)malloc(sizeof(array_queue));
|
array_queue *q = (array_queue *)malloc(sizeof(array_queue));
|
||||||
if (q == NULL)
|
if (q == NULL)
|
||||||
{printf("error: malloc failed[From init_queue]"); return NULL;}
|
{
|
||||||
memset(q,0,sizeof(array_queue));
|
printf("error: malloc failed[From init_queue]");
|
||||||
q->front = 0;
|
return NULL;
|
||||||
q->rear = 0;
|
}
|
||||||
q->size = 0;
|
memset(q, 0, sizeof(array_queue));
|
||||||
return q;
|
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)
|
if (q->size == 0)
|
||||||
return 1;
|
return 1;
|
||||||
@ -35,55 +29,58 @@ int empty_array_queue(array_queue *q)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 判断满
|
|
||||||
|
|
||||||
|
// 判满
|
||||||
int full_array_queue(array_queue *q)
|
int full_array_queue(array_queue *q)
|
||||||
{
|
{
|
||||||
if (q->size == MAX_QUEUE)
|
if (q->size == MAX_QUEUE)
|
||||||
return 1;
|
return 1;
|
||||||
else
|
else
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 入队
|
// 入队
|
||||||
void push_array_queue(array_queue *q, elem_type value)
|
void push_array_queue(array_queue *q, elem_type value)
|
||||||
{
|
{
|
||||||
if (full_array_queue(q))
|
if (full_array_queue(q))
|
||||||
{printf("error: queue is full[From push_queue]"); return;}
|
{
|
||||||
q->data[q->rear] = value;
|
printf("error: queue is full[From push_queue]");
|
||||||
q->rear = (q->front + q->size + 1) % MAX_QUEUE;
|
return;
|
||||||
q->size++;
|
}
|
||||||
|
q->data[q->rear] = value;
|
||||||
|
q->rear = (q->front + q->size + 1) % MAX_QUEUE;
|
||||||
|
q->size++;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 出队
|
// 出队
|
||||||
elem_type pop_array_queue(array_queue *q)
|
elem_type pop_array_queue(array_queue *q)
|
||||||
{
|
{
|
||||||
if (empty_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];
|
printf("error: queue is empty[From pop_queue]");
|
||||||
q->front = (q->front + 1) % MAX_QUEUE;
|
return -1;
|
||||||
q->size--;
|
}
|
||||||
return value;
|
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)
|
void print_array_queue(array_queue *q)
|
||||||
{
|
{
|
||||||
if (empty_array_queue(q))
|
if (empty_array_queue(q))
|
||||||
{
|
{
|
||||||
printf("error :\n");
|
printf("error :\n");
|
||||||
return ;
|
return;
|
||||||
}
|
}
|
||||||
int j = q->front;
|
int j = q->front;
|
||||||
for (int i=0;i<q->size;i++)
|
for (int i = 0; i < q->size; i++)
|
||||||
{
|
{
|
||||||
printf("[");
|
printf("[");
|
||||||
printf("%d ",q->data[j%MAX_QUEUE]);
|
printf("%d ", q->data[j % MAX_QUEUE]);
|
||||||
printf("]\n");
|
printf("]\n");
|
||||||
j++;
|
j++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// void return def => fuck shit
|
|
||||||
// if for while malloc def sleep pause NULL
|
|
||||||
// #include $time_noon
|
|
45
main.c
45
main.c
@ -1,7 +1,8 @@
|
|||||||
|
#define _CRT_SECURE_NO_WARNINGS
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "binary_tree.h"
|
#include "heap.h"
|
||||||
int main(void) {
|
int main(void) {
|
||||||
|
|
||||||
// sq_list* L = init_sq_list();
|
// sq_list* L = init_sq_list();
|
||||||
@ -101,20 +102,34 @@ int main(void) {
|
|||||||
pop_link_queue(q);
|
pop_link_queue(q);
|
||||||
print_link_queue(q);*/
|
print_link_queue(q);*/
|
||||||
|
|
||||||
tree_node *root = init_binary_tree(1);
|
//tree_node *root = init_binary_tree(1);
|
||||||
tree_node *node1 = init_binary_tree(2);
|
//tree_node *node1 = init_binary_tree(2);
|
||||||
tree_node *node2 = init_binary_tree(3);
|
//tree_node *node2 = init_binary_tree(3);
|
||||||
tree_node *node3 = init_binary_tree(4);
|
//tree_node *node3 = init_binary_tree(4);
|
||||||
tree_node *node4 = init_binary_tree(5);
|
//tree_node *node4 = init_binary_tree(5);
|
||||||
tree_node *node5 = init_binary_tree(6);
|
//tree_node *node5 = init_binary_tree(6);
|
||||||
tree_node *node6 = init_binary_tree(7);
|
//tree_node *node6 = init_binary_tree(7);
|
||||||
root->left = node1;
|
//root->left = node1;
|
||||||
root->right = node2;
|
//root->right = node2;
|
||||||
node1->left = node3;
|
//node1->left = node3;
|
||||||
node1->right = node4;
|
//node1->right = node4;
|
||||||
node2->left = node5;
|
//node2->left = node5;
|
||||||
node2->right = node6;
|
//node2->right = node6;
|
||||||
traverse_tree(root);
|
//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");
|
printf("Hello World!\n");
|
||||||
system("pause");
|
system("pause");
|
||||||
return 0;
|
return 0;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user