diff --git a/.gitignore b/.gitignore index 63732c5..4df5985 100644 --- a/.gitignore +++ b/.gitignore @@ -1,13 +1,15 @@ +# 忽略所有文件 * # 不忽略目录 -!/**/ +!*/ +# 不忽略这些文件和文件类型 !.gitignore !README.md - !LICENSE - !**/*.c !**/*.h -!**/*.md \ No newline at end of file +!**/*.md +!**/makefile +!**/Makefile diff --git a/Chapter10/testmake/makefile b/Chapter10/testmake/makefile new file mode 100644 index 0000000..1d351c3 --- /dev/null +++ b/Chapter10/testmake/makefile @@ -0,0 +1,12 @@ +OBJS=main.o tool1.o tool2.o +CC=gcc +CFLAGS+=-c -Wall -g + +mytool:$(OBJS) + $(CC) $^ -o $@ + +%.o:%.c + $(CC) $^ $(CFLAGS) -o $@ + +clean: + $(RM) *.o mytool -r \ No newline at end of file diff --git a/Chapter11/ds/line/list/arr/main.c b/Chapter11/ds/line/list/arr/main.c new file mode 100644 index 0000000..09a039d --- /dev/null +++ b/Chapter11/ds/line/list/arr/main.c @@ -0,0 +1,81 @@ +#include +#include + +#include "sqlist.h" + +int main() +{ + sqlist *list = NULL; + sqlist *list1 = NULL; + + datatype arr[] = {12, 23, 34, 45, 56}; + datatype arr1[] = {89, 90, 78, 67, 56, 43}; + + int i, err; + + list = sqlist_create(); + // sqlist_create1(&list); + if (NULL == list) + { + fprintf(stderr, "sqlist_create() failed!\n"); + exit(1); + } + + list1 = sqlist_create(); + if (NULL == list1) + { + fprintf(stderr, "sqlist_create() failed!\n"); + exit(1); + } + + + for (i = 0; i < sizeof(arr) / sizeof(*arr); i++) + { + // !!! '=' 的优先级低于 '!=' + if ((err = sqlist_insert(list, 0, &arr[i])) != 0) + { + if (-1 == err) + fprintf(stderr, "The arr is full.\n"); + else if (-2 == err) + fprintf(stderr, "The pos you want to insert is wrong.\n"); + else + fprintf(stderr, "Error!\n"); + exit(1); + } + } + + for (i = 0; i < sizeof(arr1) / sizeof(*arr1); i++) + { + // !!! '=' 的优先级低于 '!=' + if ((err = sqlist_insert(list1, 0, &arr1[i])) != 0) + { + if (-1 == err) + fprintf(stderr, "The arr is full.\n"); + else if (-2 == err) + fprintf(stderr, "The pos you want to insert is wrong.\n"); + else + fprintf(stderr, "Error!\n"); + exit(1); + } + } + + // !!! 用打印行号排查段错误 + // printf("%d\n", __LINE__); + sqlist_display(list); + sqlist_display(list1); + + sqlist_union(list, list1); + sqlist_display(list); + + + +#if 0 + sqlist_delete(list, 1); + sqlist_display(list); +#endif + + sqlist_destroy(list); + sqlist_destroy(list1); + + exit(0); +} \ No newline at end of file diff --git a/Chapter11/ds/line/list/arr/makefile b/Chapter11/ds/line/list/arr/makefile new file mode 100644 index 0000000..585ac5a --- /dev/null +++ b/Chapter11/ds/line/list/arr/makefile @@ -0,0 +1,7 @@ +all:main + +main:main.o sqlist.o + $(CC) $^ -o $@ + +clean: + rm *.o main -rf \ No newline at end of file diff --git a/Chapter11/ds/line/list/arr/sqlist.c b/Chapter11/ds/line/list/arr/sqlist.c new file mode 100644 index 0000000..a06390c --- /dev/null +++ b/Chapter11/ds/line/list/arr/sqlist.c @@ -0,0 +1,131 @@ +#include +#include + +#include "sqlist.h" + +sqlist *sqlist_create() +{ + sqlist *me; + + me = malloc(sizeof(*me)); + if (NULL == me) + return NULL; + + me->last = -1; + + return me; +} + +void sqlist_create1(sqlist **ptr) +{ + *ptr = malloc(sizeof(**ptr)); + if (NULL == *ptr) + return; + + (*ptr)->last = -1; + return; +} + +int sqlist_insert(sqlist *me, int i, datatype *data) +{ + int j; + + if (DATASIZE - 1 == me->last) + return -1; + + if (i < 0 || i > me->last + 1) + return -2; + + for (j = me->last; i <= j; j--) + { + me->data[j + 1] = me->data[j]; + } + + // TODO: 仅限常规数据结构 + me->data[i] = *data; + me->last++; + + return 0; +} + +int sqlist_delete(sqlist *me, int i) +{ + int j; + + if (i < 0 || i > me->last) + return -1; + + for (j = i + 1; j <= me->last; j++) + { + me->data[j - 1] = me->data[j]; + } + + me->last--; + + return 0; +} + +int sqlist_find(sqlist *me, datatype *data) +{ + if (0 == sqlist_isempty(me)) + return -1; + + + for (int i = 0; i < me->last; i++) + { + if (me->data[i] == *data) + return i; + } + return -2; +} + +int sqlist_isempty(sqlist *me) +{ + if (-1 == me->last) + return 0; + return -1; +} + +int sqlist_setempty(sqlist *me) +{ + me->last = -1; + return 0; +} + +int sqlist_getnum(sqlist *me) +{ + return me->last + 1; +} + +void sqlist_display(sqlist *me) +{ + if (-1 == me->last) + return; + for (int i = 0; i <= me->last; i++) + { + printf("%d ", me->data[i]); + } + printf("\n"); +} + +int sqlist_destroy(sqlist *me) +{ + free(me); + return 0; +} + +int sqlist_union(sqlist *list1, sqlist *list2) +{ + // list1 -> 12 23 34 45 56 + // list2 -> 78 89 56 23 10 + + int i = 0; + + for (i = 0; i <= list2->last; i++) + { + if (sqlist_find(list1, &list2->data[i]) < 0) + { + sqlist_insert(list1, 0, &list2->data[i]); + } + } +} diff --git a/Chapter11/ds/line/list/arr/sqlist.h b/Chapter11/ds/line/list/arr/sqlist.h new file mode 100644 index 0000000..cb23d29 --- /dev/null +++ b/Chapter11/ds/line/list/arr/sqlist.h @@ -0,0 +1,36 @@ +#ifndef SQLIST_H__ +#define SQLIST_H__ + +#define DATASIZE 1024 + +typedef int datatype; + +typedef struct node_st +{ + datatype data[DATASIZE]; + int last; +} sqlist; + +sqlist *sqlist_create(); +void sqlist_create1(sqlist **me); + +int sqlist_insert(sqlist *, int i, datatype *data); + +int sqlist_delete(sqlist *, int i); + +int sqlist_find(sqlist *, datatype *data); + +int sqlist_isempty(sqlist *); + +int sqlist_setempty(sqlist *); + +int sqlist_getnum(sqlist *); + +void sqlist_display(sqlist *); + +int sqlist_destroy(sqlist *); + +int sqlist_union(sqlist *, sqlist *); + + +#endif \ No newline at end of file diff --git a/Chapter11/数据结构.md b/Chapter11/数据结构.md new file mode 100644 index 0000000..e69de29 diff --git a/Chapter3/IO/makefile b/Chapter3/IO/makefile new file mode 100644 index 0000000..af919d5 --- /dev/null +++ b/Chapter3/IO/makefile @@ -0,0 +1,15 @@ +CC=cc +CFLAGS= +LDFLAGS=-lm +TARGET=test +SRC=test.c +OBJ=$(SRC:.c=.o) + +$(TARGET): $(OBJ) + $(CC) -o $@ $^ $(LDFLAGS) + +%.o: %.c + $(CC) $(CFLAGS) -c $< -o $@ + +clean: + rm -f $(TARGET) $(OBJ)