Compare commits

..

No commits in common. "master" and "main_jdh" have entirely different histories.

7 changed files with 80 additions and 199 deletions

View File

@ -21,7 +21,11 @@ OBJ_EXT = .o
# 定义源文件列表 # 定义源文件列表
SRC_FILES = \ SRC_FILES = \
heap.c \ array_queue.c \
array_stack.c \
linked_list_stack.c \
linked_list.c \
sq_list.c \
main.c main.c
# 使用正则表达式替换源文件后缀为.o生成目标文件列表 # 使用正则表达式替换源文件后缀为.o生成目标文件列表

View File

@ -21,7 +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="binary_tree.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" />
@ -31,7 +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="binary_tree.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" />

View File

@ -36,7 +36,7 @@
<ClCompile Include="linked_queue.c"> <ClCompile Include="linked_queue.c">
<Filter>源文件</Filter> <Filter>源文件</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="heap.c"> <ClCompile Include="binary_tree.c">
<Filter>源文件</Filter> <Filter>源文件</Filter>
</ClCompile> </ClCompile>
</ItemGroup> </ItemGroup>
@ -59,7 +59,7 @@
<ClInclude Include="linked_queue.h"> <ClInclude Include="linked_queue.h">
<Filter>头文件</Filter> <Filter>头文件</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="heap.h"> <ClInclude Include="binary_tree.h">
<Filter>头文件</Filter> <Filter>头文件</Filter>
</ClInclude> </ClInclude>
</ItemGroup> </ItemGroup>

View File

@ -1,19 +1,25 @@
#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;}
printf("error: malloc failed[From init_queue]"); memset(q,0,sizeof(array_queue));
return NULL;
}
memset(q, 0, sizeof(array_queue));
q->front = 0; q->front = 0;
q->rear = 0; q->rear = 0;
q->size = 0; q->size = 0;
@ -21,7 +27,7 @@ array_queue *init_array_queue()
} }
// 判空 // 判空
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;
@ -29,9 +35,7 @@ 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)
@ -44,10 +48,7 @@ int full_array_queue(array_queue *q)
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;}
printf("error: queue is full[From push_queue]");
return;
}
q->data[q->rear] = value; q->data[q->rear] = value;
q->rear = (q->front + q->size + 1) % MAX_QUEUE; q->rear = (q->front + q->size + 1) % MAX_QUEUE;
q->size++; q->size++;
@ -57,10 +58,7 @@ void push_array_queue(array_queue *q, elem_type value)
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;}
printf("error: queue is empty[From pop_queue]");
return -1;
}
elem_type value = q->data[q->front]; elem_type value = q->data[q->front];
q->front = (q->front + 1) % MAX_QUEUE; q->front = (q->front + 1) % MAX_QUEUE;
q->size--; q->size--;
@ -73,14 +71,19 @@ 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

94
heap.c
View File

@ -1,94 +0,0 @@
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include "heap.h"
void heap_swap(heap *tree,int x,int y)
{
elem_t tt = tree->data[x];
tree->data[x] = tree->data[y];
tree->data[y] = tt;
}
heap* heap_init()
{
heap *tree = (heap *)malloc(sizeof(heap));
memset(tree,0,sizeof(heap));
return tree;
}
int heap_get_right(heap *tree,int i)
{
return 2 * i + 2;
}
int heap_get_left(heap *tree,int i)
{
return 2 * i + 1;
}
int heap_get_parent(heap *tree,int i)
{
return (i - 1)/2;
}
void heap_push(heap *tree,int val)
{
if (tree->size == MAX_S-1)
{
printf("heap is full\n");
return;
}
tree->data[tree->size] = val;
int flog_i = tree->size;
while(1)
{
int flog_p = heap_get_parent(tree,flog_i);
if (tree->data[flog_i] <= tree->data[flog_p] || flog_p<0)
{
break;
}
heap_swap(tree,flog_i,flog_p);
flog_i = flog_p;
}
tree->size++;
}
void heap_print(heap* tree,int root)
{
if (tree->size <= 0) return;
int* queue = (int*)malloc(tree->size * sizeof(int));
int front = 0;
int rear = 1;
int count = 1;
queue[0] = root;
printf("[%d]\n", tree->data[root]);
while (front < rear && count < tree->size)
{
int level_size = rear - front;
for (int i = 0; i < level_size && count < tree->size; i++)
{
int current = queue[front++];
int left = heap_get_left(tree, current);
int right = heap_get_right(tree, current);
if (left < tree->size) {
printf("[%d]", tree->data[left]);
queue[rear++] = left;
count++;
}
if (right < tree->size) {
printf("[%d]", tree->data[right]);
queue[rear++] = right;
count++;
}
}
printf("\n");
}
free(queue);
}

17
heap.h
View File

@ -1,17 +0,0 @@
#ifndef HEAP_H
#define HEAP_H
#define MAX_S 256
#define elem_t int
typedef struct heap {
elem_t data[MAX_S];
int size;
}heap;
void heap_swap(heap *tree,int x,int y);
int heap_get_left(heap *tree,int i);
int heap_get_right(heap *tree,int i);
int heap_get_parent(heap *tree,int i);
heap* heap_init();
void heap_push(heap *tree,elem_t val);
elem_t heap_pop(heap *tree);
void heap_print(heap *tree,int root);
#endif

45
main.c
View File

@ -1,8 +1,7 @@
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "heap.h" #include "binary_tree.h"
int main(void) { int main(void) {
// sq_list* L = init_sq_list(); // sq_list* L = init_sq_list();
@ -102,34 +101,20 @@ 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_dfs(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;