数据结构-线性表
This commit is contained in:
8
.gitignore
vendored
8
.gitignore
vendored
@@ -1,13 +1,15 @@
|
|||||||
|
# 忽略所有文件
|
||||||
*
|
*
|
||||||
|
|
||||||
# 不忽略目录
|
# 不忽略目录
|
||||||
!/**/
|
!*/
|
||||||
|
|
||||||
|
# 不忽略这些文件和文件类型
|
||||||
!.gitignore
|
!.gitignore
|
||||||
!README.md
|
!README.md
|
||||||
|
|
||||||
!LICENSE
|
!LICENSE
|
||||||
|
|
||||||
!**/*.c
|
!**/*.c
|
||||||
!**/*.h
|
!**/*.h
|
||||||
!**/*.md
|
!**/*.md
|
||||||
|
!**/makefile
|
||||||
|
!**/Makefile
|
||||||
|
|||||||
12
Chapter10/testmake/makefile
Normal file
12
Chapter10/testmake/makefile
Normal file
@@ -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
|
||||||
81
Chapter11/ds/line/list/arr/main.c
Normal file
81
Chapter11/ds/line/list/arr/main.c
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#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);
|
||||||
|
}
|
||||||
7
Chapter11/ds/line/list/arr/makefile
Normal file
7
Chapter11/ds/line/list/arr/makefile
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
all:main
|
||||||
|
|
||||||
|
main:main.o sqlist.o
|
||||||
|
$(CC) $^ -o $@
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm *.o main -rf
|
||||||
131
Chapter11/ds/line/list/arr/sqlist.c
Normal file
131
Chapter11/ds/line/list/arr/sqlist.c
Normal file
@@ -0,0 +1,131 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#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]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
36
Chapter11/ds/line/list/arr/sqlist.h
Normal file
36
Chapter11/ds/line/list/arr/sqlist.h
Normal file
@@ -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
|
||||||
0
Chapter11/数据结构.md
Normal file
0
Chapter11/数据结构.md
Normal file
15
Chapter3/IO/makefile
Normal file
15
Chapter3/IO/makefile
Normal file
@@ -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)
|
||||||
Reference in New Issue
Block a user