C语言顺序表的实现

在学习数据结构的时候老师大多给出的是伪代码,本文实现了数据结构中线性表的c语言表达,一方面以供学习者参考,另一方面作为自己学习的记录。使用的IDE是VS2019,作为c语言的初学者,许多地方做的不够完善还请海涵。在代码过程中遇到的问题与解答已附在代码中。

1.代码部分

结构体的定义:

1
2
3
4
5
6
7
8
9
10
typedef struct {
char *name[5];
int price[5];
}NumberList;


typedef struct {
NumberList *lists;
int length;
}Lineartable;

主函数部分:(由于我只是在测试各个函数的运行情况,主函数部分大家可自行修改)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
int main()
{
Lineartable* sqlist = (Lineartable*)malloc(sizeof(Lineartable));
NumberList* nlist = (NumberList*)malloc(sizeof(NumberList));
int i, result;
char *check;
char* books[5] = { "The great Gastby", "Harry Potter", "Sea and the old", "Education of love", "Island" };
int money[5] = { 100, 200, 138, 36, 60 };
if (sqlist && nlist) //If "if" sentence is deleted, the computer doesn't konw if function malloc offers enough memory for sqlist. A warning will be existed.
{
for (i = 0; i <= 4; i++) {
nlist->name[i] = books[i];
nlist->price[i] = money[i];
}
sqlist->lists = nlist;
sqlist->length = 5;
printf("The book is: %s\n", nlist->name[0]); //Q1: How to print a string array? A1: Just print the point of array with "%s".
printf("%d book in library now.\n", sqlist->length);
ShowBooks(sqlist);
//LocateElem(sqlist);
//ListInsert(sqlist);
printf("The book is: %s\n", sqlist->lists->name[3]);
ShowBooks(sqlist);
check = GetElem(nlist, 3); //Q2: Why isn't check a pointer? A2: You forget to define check as a pointer.
printf("%s is at the position.\n", check);
//Clearlist(sqlist);
printf("%d book in library now.\n", sqlist->length); //annotation for lines:ctrl + k, ctrl + c
DeleteList(sqlist);
printf("%d book in library now.\n", sqlist->length);
ShowBooks(sqlist);
result = IsEmpty(sqlist);
if (result)
{
printf("Lineartable is empty now.");
}
else
{
printf("Lineartable is not empty.");
}
}
return 0;
}

子函数部分:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
void DestoryList(Lineartable *sqlist, NumberList* lists) {
if (sqlist->lists->name && sqlist->lists->price)
{
free(sqlist);
free(lists);
}
}


void Clearlist(Lineartable *sqlist) {
int i;
for (i = 0; i <= 4; i++)
{
sqlist->lists->name[i] = "";
sqlist->lists->price[i] = 0;
sqlist->length = 0;
}
}


int Getlenth(Lineartable* sqlist) {
return sqlist->length;
}


int IsEmpty(Lineartable* sqlist) {
if (sqlist->length == 0)
{
return 1;
}
else
{
return 0;
}
}


char* GetElem(NumberList* lists, int i){
return lists->name[i];
}


void LocateElem(Lineartable* sqlist){
char bn[50];
int i;
printf("Input the book you are looking for:");
gets_s(bn, 50);
const char* pointer1 = &bn; //Pay attention here: function strcmp only accept its parameters in form of const type *, in this case, bn and name should be transformed
const char* pointer2 = &(sqlist->lists->name);
for(i=0;i<=sqlist->length;i++)
{
if (strcmp(pointer1, pointer2) == 0)
{
printf("The book is at the position %d", i);
}
}
}


void ListInsert(Lineartable* sqlist) { //Q4:Why can't I change the Lineartable forever even with a "static"? A4: You should place "static" before book[30]
int i, j;
static char book[30];
printf("Input the position and book name you would like to insert: ");
scanf_s("%d\n", &i);
printf("Input the book name:\n");
setvbuf(stdin, NULL, _IOFBF, 512);
gets_s(book, 30); //Q3: Why this line shows before the last one? A4:Becasue of the exist of buffer.
char* pointer = (char*)book;
if (i < 1 || i > sqlist->length)
{
printf("Fail to insert!");
}
else
{
for (j = sqlist->length - 1; j >= i - 1; j--)
{
sqlist->lists->name[j + 1] = sqlist->lists->name[j];
sqlist->lists->price[j + 1] = sqlist->lists->price[j];
}
sqlist->lists->name[i] = pointer;
}
}


void ShowBooks(Lineartable* sqlist)
{
int i;
for (i = 0; i <= 4; i++) {
printf("Book %d is: %s\n", i+1, sqlist->lists->name[i]);
}

}


void DeleteList(Lineartable* sqlist)
{
int i, j;
char* pointer = "";
printf("Input the position you want to delete:");
scanf_s("%d", &i);
if (i < 1 || i > sqlist->length)
{
printf("Fail to delete!");
}
else
{
for (j = i; j < sqlist->length; j++)
{
sqlist->lists->name[j - 1] = sqlist->lists->name[j];
sqlist->lists->price[j - 1] = sqlist->lists->price[j];
}
sqlist->length -= 1;
sqlist->lists->name[4] = pointer;
sqlist->lists->price[4] = 0;
}

}

2.反思与改进

反思与改进:
1.对于c语言的内存机制还不够清晰,初次写长段c的代码报错很多很多。
2.尝试在多个.c文件下进行后续数据结构的表达。
3.接口与出口做的不够好,在未来尝试不做任何返回,只接受最简单基础的单一变量,在函数内部完成各项功能。

查看评论