kong
Co-Authored-By: JdhByte <111557398+Jdhggg@users.noreply.github.com>
This commit is contained in:
parent
fb3595a829
commit
8bb1193a79
8
Makefile
8
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)
|
||||
rm -rf $(OBJ_DIR) $(TARGET)
|
||||
|
@ -21,6 +21,7 @@
|
||||
<ItemGroup>
|
||||
<ClCompile Include="array_queue.c" />
|
||||
<ClCompile Include="array_stack.c" />
|
||||
<ClCompile Include="heap.c" />
|
||||
<ClCompile Include="linked_list.c" />
|
||||
<ClCompile Include="linked_list_stack.c" />
|
||||
<ClCompile Include="linked_queue.c" />
|
||||
@ -30,6 +31,7 @@
|
||||
<ItemGroup>
|
||||
<ClInclude Include="array_queue.h" />
|
||||
<ClInclude Include="array_stack.h" />
|
||||
<ClInclude Include="heap.h" />
|
||||
<ClInclude Include="linked_list.h" />
|
||||
<ClInclude Include="linked_list_stack.h" />
|
||||
<ClInclude Include="linked_queue.h" />
|
||||
|
@ -36,6 +36,9 @@
|
||||
<ClCompile Include="linked_queue.c">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="heap.c">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="sq_list.h">
|
||||
@ -56,5 +59,8 @@
|
||||
<ClInclude Include="linked_queue.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="heap.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
107
array_queue.c
107
array_queue.c
@ -1,33 +1,27 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h> // 添加此行以确保 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;i<q->size;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
|
||||
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++;
|
||||
}
|
||||
}
|
45
main.c
45
main.c
@ -1,7 +1,8 @@
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user