Compare commits
No commits in common. "master" and "main_jdh" have entirely different histories.
6
Makefile
6
Makefile
@ -21,7 +21,11 @@ OBJ_EXT = .o
|
||||
|
||||
# 定义源文件列表
|
||||
SRC_FILES = \
|
||||
heap.c \
|
||||
array_queue.c \
|
||||
array_stack.c \
|
||||
linked_list_stack.c \
|
||||
linked_list.c \
|
||||
sq_list.c \
|
||||
main.c
|
||||
|
||||
# 使用正则表达式替换源文件后缀为.o,生成目标文件列表
|
||||
|
@ -21,7 +21,7 @@
|
||||
<ItemGroup>
|
||||
<ClCompile Include="array_queue.c" />
|
||||
<ClCompile Include="array_stack.c" />
|
||||
<ClCompile Include="heap.c" />
|
||||
<ClCompile Include="binary_tree.c" />
|
||||
<ClCompile Include="linked_list.c" />
|
||||
<ClCompile Include="linked_list_stack.c" />
|
||||
<ClCompile Include="linked_queue.c" />
|
||||
@ -31,7 +31,7 @@
|
||||
<ItemGroup>
|
||||
<ClInclude Include="array_queue.h" />
|
||||
<ClInclude Include="array_stack.h" />
|
||||
<ClInclude Include="heap.h" />
|
||||
<ClInclude Include="binary_tree.h" />
|
||||
<ClInclude Include="linked_list.h" />
|
||||
<ClInclude Include="linked_list_stack.h" />
|
||||
<ClInclude Include="linked_queue.h" />
|
||||
|
@ -36,7 +36,7 @@
|
||||
<ClCompile Include="linked_queue.c">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="heap.c">
|
||||
<ClCompile Include="binary_tree.c">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
@ -59,7 +59,7 @@
|
||||
<ClInclude Include="linked_queue.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="heap.h">
|
||||
<ClInclude Include="binary_tree.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
|
103
array_queue.c
103
array_queue.c
@ -1,27 +1,33 @@
|
||||
#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;
|
||||
@ -29,58 +35,55 @@ 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++;
|
||||
}
|
||||
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
|
94
heap.c
94
heap.c
@ -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
17
heap.h
@ -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
45
main.c
@ -1,8 +1,7 @@
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "heap.h"
|
||||
#include "binary_tree.h"
|
||||
int main(void) {
|
||||
|
||||
// sq_list* L = init_sq_list();
|
||||
@ -102,34 +101,20 @@ 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);
|
||||
|
||||
|
||||
|
||||
// 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);
|
||||
|
||||
|
||||
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_dfs(root);
|
||||
printf("Hello World!\n");
|
||||
system("pause");
|
||||
return 0;
|
||||
|
Loading…
x
Reference in New Issue
Block a user