diff --git a/Chapter1/C1-绪论.md b/Chapter01/C1-绪论.md similarity index 100% rename from Chapter1/C1-绪论.md rename to Chapter01/C1-绪论.md diff --git a/Chapter1/gcc与vim的使用/hello.c b/Chapter01/gcc与vim的使用/hello.c similarity index 100% rename from Chapter1/gcc与vim的使用/hello.c rename to Chapter01/gcc与vim的使用/hello.c diff --git a/Chapter1/基本概念-编程注意问题/a.c b/Chapter01/基本概念-编程注意问题/a.c similarity index 100% rename from Chapter1/基本概念-编程注意问题/a.c rename to Chapter01/基本概念-编程注意问题/a.c diff --git a/Chapter1/基本概念-编程注意问题/hello.c b/Chapter01/基本概念-编程注意问题/hello.c similarity index 100% rename from Chapter1/基本概念-编程注意问题/hello.c rename to Chapter01/基本概念-编程注意问题/hello.c diff --git a/Chapter1/基本概念-编程注意问题/return.c b/Chapter01/基本概念-编程注意问题/return.c similarity index 100% rename from Chapter1/基本概念-编程注意问题/return.c rename to Chapter01/基本概念-编程注意问题/return.c diff --git a/Chapter2/C2-数据类型,运算符和表达式.md b/Chapter02/C2-数据类型,运算符和表达式.md similarity index 100% rename from Chapter2/C2-数据类型,运算符和表达式.md rename to Chapter02/C2-数据类型,运算符和表达式.md diff --git a/Chapter2/变量/auto.c b/Chapter02/变量/auto.c similarity index 100% rename from Chapter2/变量/auto.c rename to Chapter02/变量/auto.c diff --git a/Chapter2/变量/minproj/main.c b/Chapter02/变量/minproj/main.c similarity index 100% rename from Chapter2/变量/minproj/main.c rename to Chapter02/变量/minproj/main.c diff --git a/Chapter2/变量/minproj/proj.c b/Chapter02/变量/minproj/proj.c similarity index 100% rename from Chapter2/变量/minproj/proj.c rename to Chapter02/变量/minproj/proj.c diff --git a/Chapter2/变量/minproj/proj.h b/Chapter02/变量/minproj/proj.h similarity index 100% rename from Chapter2/变量/minproj/proj.h rename to Chapter02/变量/minproj/proj.h diff --git a/Chapter2/变量/minproj_extern/main.c b/Chapter02/变量/minproj_extern/main.c similarity index 100% rename from Chapter2/变量/minproj_extern/main.c rename to Chapter02/变量/minproj_extern/main.c diff --git a/Chapter2/变量/minproj_extern/proj.c b/Chapter02/变量/minproj_extern/proj.c similarity index 100% rename from Chapter2/变量/minproj_extern/proj.c rename to Chapter02/变量/minproj_extern/proj.c diff --git a/Chapter2/变量/minproj_extern/proj.h b/Chapter02/变量/minproj_extern/proj.h similarity index 100% rename from Chapter2/变量/minproj_extern/proj.h rename to Chapter02/变量/minproj_extern/proj.h diff --git a/Chapter2/常量/define.c b/Chapter02/常量/define.c similarity index 100% rename from Chapter2/常量/define.c rename to Chapter02/常量/define.c diff --git a/Chapter2/数据类型/bool.c b/Chapter02/数据类型/bool.c similarity index 100% rename from Chapter2/数据类型/bool.c rename to Chapter02/数据类型/bool.c diff --git a/Chapter2/数据类型/float.c b/Chapter02/数据类型/float.c similarity index 100% rename from Chapter2/数据类型/float.c rename to Chapter02/数据类型/float.c diff --git a/Chapter2/运算符/op.c b/Chapter02/运算符/op.c similarity index 100% rename from Chapter2/运算符/op.c rename to Chapter02/运算符/op.c diff --git a/Chapter3/C3-输入输出专题.md b/Chapter03/C3-输入输出专题.md similarity index 100% rename from Chapter3/C3-输入输出专题.md rename to Chapter03/C3-输入输出专题.md diff --git a/Chapter3/IO/getchar_putchar.c b/Chapter03/IO/getchar_putchar.c similarity index 100% rename from Chapter3/IO/getchar_putchar.c rename to Chapter03/IO/getchar_putchar.c diff --git a/Chapter3/IO/gets_puts.c b/Chapter03/IO/gets_puts.c similarity index 100% rename from Chapter3/IO/gets_puts.c rename to Chapter03/IO/gets_puts.c diff --git a/Chapter3/IO/makefile b/Chapter03/IO/makefile similarity index 100% rename from Chapter3/IO/makefile rename to Chapter03/IO/makefile diff --git a/Chapter3/IO/printf.c b/Chapter03/IO/printf.c similarity index 100% rename from Chapter3/IO/printf.c rename to Chapter03/IO/printf.c diff --git a/Chapter3/IO/scanf.c b/Chapter03/IO/scanf.c similarity index 100% rename from Chapter3/IO/scanf.c rename to Chapter03/IO/scanf.c diff --git a/Chapter3/IO/test.c b/Chapter03/IO/test.c similarity index 100% rename from Chapter3/IO/test.c rename to Chapter03/IO/test.c diff --git a/Chapter4/C4-流程控制.md b/Chapter04/C4-流程控制.md similarity index 100% rename from Chapter4/C4-流程控制.md rename to Chapter04/C4-流程控制.md diff --git a/Chapter4/if.c b/Chapter04/if.c similarity index 100% rename from Chapter4/if.c rename to Chapter04/if.c diff --git a/Chapter4/loop.c b/Chapter04/loop.c similarity index 100% rename from Chapter4/loop.c rename to Chapter04/loop.c diff --git a/Chapter4/switch.c b/Chapter04/switch.c similarity index 100% rename from Chapter4/switch.c rename to Chapter04/switch.c diff --git a/Chapter5/C5-数组.md b/Chapter05/C5-数组.md similarity index 100% rename from Chapter5/C5-数组.md rename to Chapter05/C5-数组.md diff --git a/Chapter5/char.c b/Chapter05/char.c similarity index 100% rename from Chapter5/char.c rename to Chapter05/char.c diff --git a/Chapter5/char_func.c b/Chapter05/char_func.c similarity index 100% rename from Chapter5/char_func.c rename to Chapter05/char_func.c diff --git a/Chapter5/douarr.c b/Chapter05/douarr.c similarity index 100% rename from Chapter5/douarr.c rename to Chapter05/douarr.c diff --git a/Chapter5/simple1.c b/Chapter05/simple1.c similarity index 100% rename from Chapter5/simple1.c rename to Chapter05/simple1.c diff --git a/Chapter5/simple_test.c b/Chapter05/simple_test.c similarity index 100% rename from Chapter5/simple_test.c rename to Chapter05/simple_test.c diff --git a/Chapter6/C6-指针.md b/Chapter06/C6-指针.md similarity index 100% rename from Chapter6/C6-指针.md rename to Chapter06/C6-指针.md diff --git a/Chapter6/arr.c b/Chapter06/arr.c similarity index 100% rename from Chapter6/arr.c rename to Chapter06/arr.c diff --git a/Chapter6/chararr.c b/Chapter06/chararr.c similarity index 100% rename from Chapter6/chararr.c rename to Chapter06/chararr.c diff --git a/Chapter6/const.c b/Chapter06/const.c similarity index 100% rename from Chapter6/const.c rename to Chapter06/const.c diff --git a/Chapter6/douarr.c b/Chapter06/douarr.c similarity index 100% rename from Chapter6/douarr.c rename to Chapter06/douarr.c diff --git a/Chapter6/point.c b/Chapter06/point.c similarity index 100% rename from Chapter6/point.c rename to Chapter06/point.c diff --git a/Chapter6/point1.c b/Chapter06/point1.c similarity index 100% rename from Chapter6/point1.c rename to Chapter06/point1.c diff --git a/Chapter6/point_arr.c b/Chapter06/point_arr.c similarity index 100% rename from Chapter6/point_arr.c rename to Chapter06/point_arr.c diff --git a/Chapter7/C7-函数.md b/Chapter07/C7-函数.md similarity index 100% rename from Chapter7/C7-函数.md rename to Chapter07/C7-函数.md diff --git a/Chapter7/arr.c b/Chapter07/arr.c similarity index 100% rename from Chapter7/arr.c rename to Chapter07/arr.c diff --git a/Chapter7/chararr.c b/Chapter07/chararr.c similarity index 100% rename from Chapter7/chararr.c rename to Chapter07/chararr.c diff --git a/Chapter7/douarr.c b/Chapter07/douarr.c similarity index 100% rename from Chapter7/douarr.c rename to Chapter07/douarr.c diff --git a/Chapter7/fun_point.c b/Chapter07/fun_point.c similarity index 100% rename from Chapter7/fun_point.c rename to Chapter07/fun_point.c diff --git a/Chapter7/func.c b/Chapter07/func.c similarity index 100% rename from Chapter7/func.c rename to Chapter07/func.c diff --git a/Chapter7/hello.c b/Chapter07/hello.c similarity index 100% rename from Chapter7/hello.c rename to Chapter07/hello.c diff --git a/Chapter7/main.c b/Chapter07/main.c similarity index 100% rename from Chapter7/main.c rename to Chapter07/main.c diff --git a/Chapter7/min_max.c b/Chapter07/min_max.c similarity index 100% rename from Chapter7/min_max.c rename to Chapter07/min_max.c diff --git a/Chapter7/swap.c b/Chapter07/swap.c similarity index 100% rename from Chapter7/swap.c rename to Chapter07/swap.c diff --git a/Chapter8/C8-构造类型.md b/Chapter08/C8-构造类型.md similarity index 100% rename from Chapter8/C8-构造类型.md rename to Chapter08/C8-构造类型.md diff --git a/Chapter8/enum.c b/Chapter08/enum.c similarity index 100% rename from Chapter8/enum.c rename to Chapter08/enum.c diff --git a/Chapter8/struct1.c b/Chapter08/struct1.c similarity index 100% rename from Chapter8/struct1.c rename to Chapter08/struct1.c diff --git a/Chapter8/typedef.c b/Chapter08/typedef.c similarity index 100% rename from Chapter8/typedef.c rename to Chapter08/typedef.c diff --git a/Chapter9/arr.c b/Chapter09/arr.c similarity index 100% rename from Chapter9/arr.c rename to Chapter09/arr.c diff --git a/Chapter9/malloc.c b/Chapter09/malloc.c similarity index 100% rename from Chapter9/malloc.c rename to Chapter09/malloc.c diff --git a/Chapter9/test.c b/Chapter09/test.c similarity index 100% rename from Chapter9/test.c rename to Chapter09/test.c diff --git a/Chapter9/动态内存管理.md b/Chapter09/动态内存管理.md similarity index 100% rename from Chapter9/动态内存管理.md rename to Chapter09/动态内存管理.md diff --git a/Chapter11/ds/line/list/linklist/simple/head/list.c b/Chapter11/ds/line/list/linklist/simple/head/list.c new file mode 100644 index 0000000..c5eb2f0 --- /dev/null +++ b/Chapter11/ds/line/list/linklist/simple/head/list.c @@ -0,0 +1,156 @@ +#include +#include + +#include "list.h" + +list *list_create() +{ + list *me; + me = malloc(sizeof(*me)); + if (NULL == me) + return NULL; + + me->next = NULL; + + return me; +} + +// 从第一个有效节点作为第0个 +int list_insert_at(list *me, int i, datatype *data) +{ + int k = 0; + list *node = me, *newnode; + + if (i < 0) + return -1; + // return -EINVAL; + + while (k < i && node != NULL) + { + node = node->next; + k++; + } + + if (NULL != node) + { + newnode = malloc(sizeof(*newnode)); + if (NULL == newnode) + return -2; + + newnode->data = *data; + newnode->next = NULL; + + newnode->next = node->next; + node->next = newnode; + + return 0; + } + else + return -3; +} + +// 有序插入 +int list_order_insert(list *me, datatype *data) +{ + list *p = me, *q; + + while (p->next && p->next->data < *data) + p = p->next; + + q = malloc(sizeof(*q)); + if (NULL == q) + return -1; + + q->data = *data; + q->next = p->next; + p->next = q; + + return 0; +} + +int list_delete_at(list *me, int i, datatype *data) +{ + int k = 0; + list *p = me, *q; + + *data = -1; + + if (i < 0) + return -1; + + while (k < i) + { + p = p->next; + k++; + } + + if (p) + { + q = p->next; + p->next = q->next; + *data = q->data; + free(q); + q = NULL; + + return 0; + } + else + return -2; +} + +int list_delete(list *me, datatype *data) +{ + list *p = me, *q; + while (p->next && p->next->data != *data) + p = p->next; + + if (NULL == p->next) + return -1; + else + { + q = p->next; + p->next = q->next; + free(q); + q = NULL; + } + + return 0; +} + +int list_isempty(list *me) +{ + if (NULL == me->next) + return 0; + return 1; +} + +void list_display(list *me) +{ + list *node = me->next; + if (0 == list_isempty(me)) + return; + + while (NULL != node) + { + printf("%d ", node->data); + node = node->next; + } + printf("\n"); + + return; +} + +void list_destroy(list *me) +{ + list *node, *next; + + for (node = me->next; node != NULL; node = next) + { + next = node->next; + free(node); + } + + free(me); + + return; +} \ No newline at end of file diff --git a/Chapter11/ds/line/list/linklist/simple/head/list.h b/Chapter11/ds/line/list/linklist/simple/head/list.h new file mode 100644 index 0000000..399f0aa --- /dev/null +++ b/Chapter11/ds/line/list/linklist/simple/head/list.h @@ -0,0 +1,26 @@ +#ifndef LIST_H__ +#define LIST_H__ + +typedef int datatype; + +typedef struct node_st +{ + datatype data; + struct node_st *next; +}list; + +list *list_create(); + +int list_insert_at(list *me, int i, datatype *data); +int list_order_insert(list *me, datatype *data); + +int list_delete_at(list *me, int i, datatype *data); +int list_delete(list *me, datatype *data); + +int list_isempty(list *me); + +void list_display(list *me); +void list_destroy(list *me); + + +#endif \ No newline at end of file diff --git a/Chapter11/ds/line/list/linklist/simple/head/main.c b/Chapter11/ds/line/list/linklist/simple/head/main.c new file mode 100644 index 0000000..929022b --- /dev/null +++ b/Chapter11/ds/line/list/linklist/simple/head/main.c @@ -0,0 +1,44 @@ +#include +#include + +#include "list.h" + +int main() +{ + list *l; + datatype arr[] = {12, 9, 23, 2, 34, 6, 45}; + + l = list_create(); + if (NULL == l) + exit(1); + + for (int i = 0; i < sizeof(arr) / sizeof(*arr); i++) + { + // if (list_insert_at(l, 0, &arr[i])) + if (list_order_insert(l, &arr[i])) + exit(1); + } + + list_display(l); + + datatype value; + int err; + + err = list_delete_at(l, 2, &value); + if (err) + exit(1); + list_display(l); + printf("delete:%d\n", value); + + + +#if 0 + int value = 12; + list_delete(l, &value); + list_display(l); +#endif + + list_destroy(l); + + exit(0); +} \ No newline at end of file diff --git a/Chapter11/ds/line/list/linklist/simple/head/makefile b/Chapter11/ds/line/list/linklist/simple/head/makefile new file mode 100644 index 0000000..040d391 --- /dev/null +++ b/Chapter11/ds/line/list/linklist/simple/head/makefile @@ -0,0 +1,7 @@ +all:main + +main:main.o list.o + $(CC) $^ -o $@ + +clean: + rm *.o main -rf \ No newline at end of file diff --git a/Chapter11/ds/line/list/linklist/simple/nohead/main.c b/Chapter11/ds/line/list/linklist/simple/nohead/main.c new file mode 100644 index 0000000..d7575e7 --- /dev/null +++ b/Chapter11/ds/line/list/linklist/simple/nohead/main.c @@ -0,0 +1,48 @@ +#include +#include + +#include "nohead.h" + +int main() +{ + int i, ret; + struct node_st *list = NULL; + struct score_st tmp; + + for (i = 0; i < 7; i++) + { + tmp.id = i; + snprintf(tmp.name, NAMESIZE, "stu%d", i); + tmp.math = rand() % 100; + tmp.chinese = rand() % 100; + + // list = list_insert(list, &tmp); + ret = list_insert(&list, &tmp); + if (0 != ret) + exit(1); + } + + + list_show(list); + + printf("\n\n"); + + int id = 13; + struct score_st *ptr; + ptr = list_find(&list, id); + if (NULL == ptr) + printf("Can not find!\n"); + else + printf("%d %s %d %d", ptr->id, ptr->name, ptr->math, ptr->chinese); + + +#if 0 + list_delete(&list); + + list_show(list); +#endif + + list_destroy(&list); + + exit(0); +} \ No newline at end of file diff --git a/Chapter11/ds/line/list/linklist/simple/nohead/makefile b/Chapter11/ds/line/list/linklist/simple/nohead/makefile new file mode 100644 index 0000000..90ab9bc --- /dev/null +++ b/Chapter11/ds/line/list/linklist/simple/nohead/makefile @@ -0,0 +1,7 @@ +all:main + +main:main.o nohead.o + $(CC) $^ -o $@ + +clean: + rm *.o main -rf \ No newline at end of file diff --git a/Chapter11/ds/line/list/linklist/simple/nohead/nohead.c b/Chapter11/ds/line/list/linklist/simple/nohead/nohead.c new file mode 100644 index 0000000..574dc96 --- /dev/null +++ b/Chapter11/ds/line/list/linklist/simple/nohead/nohead.c @@ -0,0 +1,106 @@ +#include +#include + +#include "nohead.h" + + +#if 0 +struct node_st *list_insert(struct node_st *list, struct score_st *data) +{ + struct node_st *new; + + new = malloc(sizeof(*new)); + if (NULL == new) + return NULL; + + new->data = *data; + new->next = list; + list = new; + + return list; +} +#endif + +int list_insert(struct node_st **list, struct score_st *data) +{ + struct node_st *new; + + new = malloc(sizeof(*new)); + if (NULL == new) + return -1; + + new->data = *data; + new->next = *list; + *list = new; + + return 0; +} + +void list_show(struct node_st *list) +{ + struct node_st *cur; + + for (cur = list; NULL != cur; cur = cur->next) + { + printf("%d %s %d %d\n", + cur->data.id, + cur->data.name, + cur->data.math, + cur->data.chinese); + } +} + +// 删除首位 +int list_delete(struct node_st **list) +{ + struct node_st *cur; + + if (NULL == *list) + return -1; + + cur = *list; + *list = (*list)->next; + + free(cur); + + return 0; +} + +struct score_st *list_find(struct node_st **list, int id) +{ + struct node_st *cur; + + if (NULL == *list) + return NULL; + + // for (cur = *list; NULL != cur->next; cur = cur->next) + // !!! 是cur不为空 + for (cur = *list; NULL != cur; cur = cur->next) + { + if (id == cur->data.id) + { + // printf("%d %s %d %d\n", + // cur->data.id, + // cur->data.name, + // cur->data.math, + // cur->data.chinese); + return &(cur->data); + } + } + + return NULL; +} + +int list_destroy(struct node_st **list) +{ + struct node_st *cur; + + if (NULL == list) + return -1; + + for (cur = *list; NULL != cur; cur = *list) + { + *list = cur->next; + free(cur); + } +} \ No newline at end of file diff --git a/Chapter11/ds/line/list/linklist/simple/nohead/nohead.h b/Chapter11/ds/line/list/linklist/simple/nohead/nohead.h new file mode 100644 index 0000000..71883ce --- /dev/null +++ b/Chapter11/ds/line/list/linklist/simple/nohead/nohead.h @@ -0,0 +1,26 @@ +#define NAMESIZE 32 + +struct score_st +{ + int id; + char name[NAMESIZE]; + int math; + int chinese; +}; + +struct node_st +{ + struct score_st data; + struct node_st *next; +}; + +// struct node_st *list_insert(struct node_st *list, struct score_st *data); +int list_insert(struct node_st **list, struct score_st *data); + +void list_show(struct node_st *list); + +int list_delete(struct node_st **list); + +struct score_st *list_find(struct node_st **list, int id); + +int list_destroy(struct node_st **list); diff --git a/Chapter11/数据结构.md b/Chapter11/数据结构.md index e69de29..c8a1454 100644 --- a/Chapter11/数据结构.md +++ b/Chapter11/数据结构.md @@ -0,0 +1,35 @@ +# 数据结构 + +## 架构梳理 + +- 线性(1:1) + + - 线性表 + + - 顺序存储 --> arr + + - 链式存储 --> 指针 (有头,无头) + + 有头是指有一个不存数据的头,始终作为这个链表的起点。 + + 会更加简单,无头的话,更改首部节点会麻烦。 + + - 单链表 + - 循环 + - 不循环 + - 双向链表 + - 循环 + - 不循环 + + - 栈 + + - 队列 + +- 树状(1:N) + + **递归**。**递归**转**非递归**。 + +- 图(N:M) + + +