Compare commits

...

10 Commits

Author SHA1 Message Date
jdh
fc334bbfca Update binary_tree.c 2025-04-30 14:08:35 +08:00
3baf08ba4e binary_tree
本次提交主要实现层遍历

Co-Authored-By: Jdhggg <111557398+Jdhggg@users.noreply.github.com>
2025-04-29 22:39:05 +08:00
jdh
ab8a54af3a Merge branch 'master' of https://github.com/jdhnsu/C_DS_Algo 2025-04-29 21:25:27 +08:00
jdh
f7a523b8ca up
Co-Authored-By: Jdhggg <111557398+Jdhggg@users.noreply.github.com>
2025-04-29 18:01:09 +08:00
7b55460f32 Merge branch 'master' of https://github.com/jdhnsu/C_DS_Algo 2025-04-25 21:50:28 +08:00
004db03148 vs2022 及 make 配置更新
Co-Authored-By: Jdhggg <111557398+Jdhggg@users.noreply.github.com>
2025-04-25 21:50:01 +08:00
jdh
4fdf297d56 Update Makefile 2025-04-24 20:05:00 +08:00
jdh
d8bb428f96 up make 2025-04-24 19:53:07 +08:00
08df41a61e Update .gitignore
Co-Authored-By: Jdhggg <111557398+Jdhggg@users.noreply.github.com>
2025-04-19 00:14:23 +08:00
aa4996a940 add linked_queue,up linked_list
添加了 基于链表实现的队列
更改了 链表的打印函数的终止判断条件.

Co-Authored-By: Jdhggg <111557398+Jdhggg@users.noreply.github.com>
2025-04-19 00:13:46 +08:00
11 changed files with 391 additions and 92 deletions

8
.gitignore vendored
View File

@ -57,3 +57,11 @@ dkms.conf
*.recipe
*.tlog
*.log
*.vcxproj
*.filters
Project2.vcxproj.filters
*.filters
*.vcxproj
Project2.vcxproj
Project2.vcxproj.filters
main.c

51
Makefile Normal file
View File

@ -0,0 +1,51 @@
# 定义编译器
CC = gcc
# 定义源文件目录
SRC_DIR = .
# 定义头文件目录
INC_DIR = .
# 定义目标文件目录
OBJ_DIR = obj
# 定义可执行文件名称
TARGET = main
# 定义源文件后缀
SRC_EXT = .c
# 定义目标文件后缀
OBJ_EXT = .o
# 定义源文件列表
SRC_FILES = \
array_queue.c \
array_stack.c \
linked_list_stack.c \
linked_list.c \
sq_list.c \
main.c
# 使用正则表达式替换源文件后缀为.o生成目标文件列表
OBJ_FILES = $(patsubst $(SRC_DIR)/%$(SRC_EXT), $(OBJ_DIR)/%$(OBJ_EXT), $(SRC_FILES))
# 定义编译选项
CFLAGS = -I$(INC_DIR) -Wall -Wextra -pedantic
# 默认目标,调用链接规则
all: $(TARGET)
# 链接规则,将所有目标文件链接成可执行文件
$(TARGET): $(OBJ_FILES)
$(CC) $(CFLAGS) $^ -o $@
# 编译规则,将源文件编译成目标文件
$(OBJ_DIR)/%$(OBJ_EXT): $(SRC_DIR)/%$(SRC_EXT)
@mkdir -p $(OBJ_DIR)
$(CC) $(CFLAGS) -c $< -o $@
# 清理目标文件和可执行文件
clean:
rm -rf $(OBJ_DIR) $(TARGET)

View File

@ -21,16 +21,20 @@
<ItemGroup>
<ClCompile Include="array_queue.c" />
<ClCompile Include="array_stack.c" />
<ClCompile Include="binary_tree.c" />
<ClCompile Include="linked_list.c" />
<ClCompile Include="linked_list_stack.c" />
<ClCompile Include="linked_queue.c" />
<ClCompile Include="main.c" />
<ClCompile Include="sq_list.c" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="array_queue.h" />
<ClInclude Include="array_stack.h" />
<ClInclude Include="binary_tree.h" />
<ClInclude Include="linked_list.h" />
<ClInclude Include="linked_list_stack.h" />
<ClInclude Include="linked_queue.h" />
<ClInclude Include="sq_list.h" />
</ItemGroup>
<PropertyGroup Label="Globals">

View File

@ -33,6 +33,12 @@
<ClCompile Include="sq_list.c">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="linked_queue.c">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="binary_tree.c">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="sq_list.h">
@ -50,5 +56,11 @@
<ClInclude Include="array_queue.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="linked_queue.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="binary_tree.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
</Project>

61
binary_tree.c Normal file
View File

@ -0,0 +1,61 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "binary_tree.h"
tree_node *init_binary_tree(elem_type value)
{
tree_node *node = (tree_node *)malloc(sizeof(tree_node));
if (node == NULL)
{
printf("error: malloc failed from:init_binary_tree\n");
}
node->value = value;
node->left = NULL;
node->right = NULL;
return node;
}
// 插入节点
void insert_tree_node(tree_node *node,tree_node *node_new)
{
if (node == NULL)
{
node ->left = node_new;
return;
}
tree_node *temp = node;
node ->left = node_new;
}
// 删除节点
// 遍历树节点 bfs
void traverse_tree(tree_node *roots)
{
tree_node *queue[3] = {NULL,NULL,NULL};
int front = 0;
int rear = 0;
if (roots == NULL)
{
printf("error: roots is NULL\n");
return;
}
printf("%d\n", roots->value);
while(roots ->left != NULL && roots -> right != NULL)
{
tree_node *temp[2] = {roots->left,roots->right};
for (int i = 0;i<2;i++)
{
if (temp[i] != NULL)
{
queue[rear] = temp[i];
rear = (rear + 1) % 3;
printf("%d ",temp[i]->value);
}
}
printf("\n");
roots = queue[front];
front = (front+1) % 3;
}
}

18
binary_tree.h Normal file
View File

@ -0,0 +1,18 @@
#ifndef BINARY_TREE
#define BINARY_TREE
#define elem_type int
typedef struct tree_node
{
elem_type value;
struct tree_node *left;
struct tree_node *right;
}tree_node;
// 初始化树节点
tree_node *init_binary_tree(elem_type value);
// 插入节点
void insert_tree_node(tree_node *node,tree_node *node_new);
// 删除节点
void delete_node(tree_node *node);
// 遍历树节点
void traverse_tree(tree_node *roots);
#endif // DEBUG

View File

@ -73,11 +73,14 @@ address_node *find_node(node* head, elem_type value)
void print_node_list(node* head)
{
node* head_1 = head;
printf("[%d ",head_1->value);
while (head_1->next != NULL)
//printf("[%d ",head_1->value);
printf("[");
while (1)
{
head_1 = head_1->next;
printf("%d ", head_1->value);
head_1 = head_1->next;
if (head_1 == NULL)
break;
}
printf("]\n");
}

View File

@ -2,13 +2,13 @@
#include <stdlib.h>
#include "linked_list_stack.h"
// 初始栈
// <EFBFBD><EFBFBD>ʼջ
stack_linked* init_stack_linked(void)
{
stack_linked* s = (stack_linked*)malloc(sizeof(stack_linked));
if (s == NULL)
{
printf("内存分配失败!\n");
printf("<EFBFBD>ڴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD>ܣ<EFBFBD>\n");
return NULL;
}
s->top = NULL;
@ -16,13 +16,13 @@ stack_linked* init_stack_linked(void)
return s;
}
// 入栈
// <EFBFBD><EFBFBD>ջ
void push_stack_linked(stack_linked* s, elem_type value)
{
stack_node* node = (stack_node*)malloc(sizeof(stack_node));
if (node == NULL)
{
printf("内存分配失败!\n");
printf("<EFBFBD>ڴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD>ܣ<EFBFBD>\n");
return NULL;
}
node->value = value;
@ -31,7 +31,7 @@ void push_stack_linked(stack_linked* s, elem_type value)
s->size++;
}
// 出栈
// <EFBFBD><EFBFBD>ջ
int pop_stack_linked(stack_linked* s)
{
int flog = 0;

91
linked_queue.c Normal file
View File

@ -0,0 +1,91 @@
#include <stdio.h>
#include <stdlib.h>
#include "linked_queue.h"
link_queue* init_link_queue()
{
link_queue* q = (link_queue*)malloc(sizeof(link_queue));
if (q == NULL)
{
printf("error: malloc from init_link_queue failed\n");
return;
}
q->front = NULL;
q->rear = NULL;
q->size = 0;
return q;
}
// Èë¶Ó
void push_link_queue(link_queue* q, elem_type value)
{
node* new_node = (node*)malloc(sizeof(node));
if (new_node == NULL)
{
printf("error: malloc from push_link_queue failed\n");
return;
}
new_node->value = value;
new_node->next = NULL;
if (q->front == NULL)
{
q->front = new_node;
q->rear = new_node;
}
else
{
q->rear->next = new_node;
q->rear = new_node;
}
q->size++;
}
// ³ö¶Ó
elem_type pop_link_queue(link_queue* q)
{
if (empty_link_queue(q))
{
printf("error: pop from empty link_queue\n");
return;
}
link_queue* tmp = q->front;
elem_type a = q->front->value;
q->front = q->front->next;
q->size--;
return a;
}
// ÅпÕ
int empty_link_queue(link_queue* q)
{
if (q->size == 0)
{
return 1;
}
else
{
return 0;
}
}
// ´òÓ¡
int print_link_queue(link_queue* q)
{
if (empty_link_queue(q))
{
printf("error: queue empty form: print_link_queue\n");
return;
}
node* head = q->front;
printf("[");
while (1)
{
printf(" %d ", head->value);
head = head->next;
if (head == NULL)
break;
}
printf("]\n");
}

34
linked_queue.h Normal file
View File

@ -0,0 +1,34 @@
#ifndef LINKED_QUEUE_H
#define LINKED_QUEUE_H
#define elem_type int
typedef struct node
{
elem_type value;
struct node* next;
} node;
typedef struct linked_queue
{
struct node* front;
struct node* rear;
int size;
}link_queue;
// 初始化
link_queue* init_link_queue();
// 入队
void push_link_queue(link_queue* q, elem_type value);
// 出队
elem_type pop_link_queue(link_queue* q);
// 判空
int empty_link_queue(link_queue* q);
// 打印
int print_link_queue(link_queue* q);
#endif

185
main.c
View File

@ -1,93 +1,87 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "binary_tree.h"
int main(void) {
int main(void)
{
// sq_list* L = init_sq_list();
// sq_list* N = init_sq_list();
// for (int i = 1; i <= 3; i++)
//{
// N->data[i - 1] = i+10;
// }
// for (int i = 1; i <= 10; i++)
//{
// L->data[i - 1] = i;
// }
// N->length = 3;
// L->length = 10;
// print_sq_list(N);
// print_sq_list(L);
// mer_ge_sq_list(L,N);
// print_sq_list(L);
// printf("L->length:[%d]\n", L->length);
/*sq_list* L = init_sq_list();
sq_list* N = init_sq_list();
for (int i = 1; i <= 3; i++)
{
N->data[i - 1] = i+10;
}
for (int i = 1; i <= 10; i++)
{
L->data[i - 1] = i;
}
N->length = 3;
L->length = 10;
print_sq_list(N);
print_sq_list(L);
mer_ge_sq_list(L,N);
print_sq_list(L);
printf("L->length:[%d]\n", L->length);*/
//sq_list* L = init_sq_list();
//sq_list* N = init_sq_list();
//for (int i = 1; i <= 3; i++)
//{
// N->data[i - 1] = i+10;
//}
//for (int i = 1; i <= 10; i++)
//{
// L->data[i - 1] = i;
//}
//N->length = 3;
//L->length = 10;
//print_sq_list(N);
//print_sq_list(L);
//mer_ge_sq_list(L,N);
//print_sq_list(L);
//printf("L->length:[%d]\n", L->length);
/*sq_list* L = init_sq_list();
sq_list* N = init_sq_list();
for (int i = 1; i <= 3; i++)
{
N->data[i - 1] = i+10;
}
for (int i = 1; i <= 10; i++)
{
L->data[i - 1] = i;
}
N->length = 3;
L->length = 10;
print_sq_list(N);
print_sq_list(L);
mer_ge_sq_list(L,N);
print_sq_list(L);
printf("L->length:[%d]\n", L->length);*/
/* array_stack* stack = init_array_stack();
push_array_stack(stack, 1);
push_array_stack(stack, 2);
push_array_stack(stack, 3);*/
/* array_stack* stack = init_array_stack();
push_array_stack(stack, 1);
push_array_stack(stack, 2);
push_array_stack(stack, 3);*/
/* array_queue *q = init_array_queue();
push_array_queue(q,0);
push_array_queue(q,1);
push_array_queue(q,2);
print_array_queue(q);
printf("---------------------\n");
pop_array_queue(q);
print_array_queue(q);
printf("----------\n");
push_array_queue(q, 666);
print_array_queue(q);*/
// node* q1 = init_node(1);
// node* q2 = init_node(2);
// node* q3 = init_node(3);
// node* q4 = init_node(4);
// q1->next = q2;
// q2->next = q3;
// q3->next = q4;
////address_node *n = find_node(q1, 3);
//// printf("%d\n %p\n", n->n,n->p
////int e = get_node_list(q1);
// node* q2_3 = init_node(0);
// insert_node(q2, q2_3);
// print_node_list(q1);
// delete_node(q2);
// print_node_list(q1);
/* array_queue *q = init_array_queue();
push_array_queue(q,0);
push_array_queue(q,1);
push_array_queue(q,2);
print_array_queue(q);
printf("---------------------\n");
pop_array_queue(q);
print_array_queue(q);
printf("----------\n");
push_array_queue(q, 666);
print_array_queue(q);*/
// node* q1 = init_node(1);
// node* q2 = init_node(2);
// node* q3 = init_node(3);
// node* q4 = init_node(4);
// q1->next = q2;
// q2->next = q3;
// q3->next = q4;
// //address_node *n = find_node(q1, 3);
// // printf("%d\n %p\n", n->n,n->p
// //int e = get_node_list(q1);
// node* q2_3 = init_node(0);
// insert_node(q2, q2_3);
// print_node_list(q1);
// delete_node(q2);
// print_node_list(q1);
/*
stack_linked* head = init_stack_linked();
push_stack_linked(head, 1);
push_stack_linked(head, 2);
push_stack_linked(head, 3);
print_linked(head);
pop_stack_linked(head);
print_linked(head);
*/
/* stack_linked* head = init_stack_linked();
push_stack_linked(head, 1);
push_stack_linked(head, 2);
push_stack_linked(head, 3);
print_linked(head);
pop_stack_linked(head);
print_linked(head);*/
// queue_test();
array_queue *q = init_array_queue();
/*array_queue *q = init_array_queue();
push_array_queue(q, 1);
push_array_queue(q, 2);
push_array_queue(q, 3);
@ -97,8 +91,31 @@ stack_linked* head = init_stack_linked();
print_array_queue(q);
printf("----------\n");
push_array_queue(q, 666);
print_array_queue(q);
printf("Hello World!\n");
system("pause");
return 0;
print_array_queue(q);*/
/* link_queue* q = init_link_queue();
push_link_queue(q, 1);
push_link_queue(q, 2);
push_link_queue(q, 3);
print_link_queue(q);
printf("____________________\n");
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);
printf("Hello World!\n");
system("pause");
return 0;
}