双向循环链表实现—通讯录(学生管理系统,自行车管理系统,影院管理系统)—C语言课设(万能模板)—数据结构—用文件存储数据

大家好,如果觉得我这篇文章写的不错并且对你有帮助的话就关注一下呗。
这是我关于双向循环链表的博客,可以点进去康康啦
编译器是VS2019,依旧是分为三个文件
我先把三个文件的原码放出来,然后对于函数 一 一 解释。

申请空间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct Contact* BuyNewNode(char* name, int age, char* sex, char* tel, char* adr)
{
struct Contact* newnode = (struct Contact*)malloc(sizeof(struct Contact));
assert(newnode);
newnode->next = newnode;
newnode->front = newnode;
strcpy(newnode->name , name);
newnode->age = age;
strcpy(newnode->sex , sex);
strcpy(newnode->tel , tel);
strcpy(newnode->adr , adr);
return newnode;
}

先将文件中的数据存到链表中

这里用 feof判断文件光标后面是否有内容,如果光标后面没有内容则返回非0,如果有内容则返回0,党光标后面有内容的时候我们需要读取文件,所以循环条件为 while(!feof)

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
void SaveFileData(PC*phead)
{

FILE* fp = fopen("data.txt", "r");
assert(phead);
if (fp == NULL)
{
printf("%s", strerror(errno));
return;
}
getc(fp);
if (!feof(fp))
{
rewind(fp);
}
while (!feof(fp))
{

PC* newnode = BuyNewNode("000000", 0, "000000", "00000", "000000");
fscanf(fp, "%s ", newnode->name);
fscanf(fp, "%d ", &(newnode->age));
fscanf(fp, "%s ", newnode->sex);
fscanf(fp, "%s ", newnode->tel);
fscanf(fp, "%s ", newnode->adr);
PC* head = phead->front;
head->next = newnode;
newnode->front = head;
newnode->next = phead;
phead->front = newnode;
}
fclose(fp);
fp = NULL;
}

1.添加联系人

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void ADDPc(PC* phead)
{
PC* newnode = BuyNewNode("000000", 0, "000000", "00000", "000000");
assert(phead);
assert(newnode);
printf("请输入姓名\n");
scanf("%s", newnode->name);
printf("请输入年龄\n");
scanf("%d", &(newnode->age));
printf("请输入性别\n");
scanf("%s", newnode->sex);
printf("请输入电话\n");
scanf("%s", newnode->tel);
printf("请输入地址\n");
scanf("%s", newnode->adr);
PC* head = phead->front;
head->next = newnode;
newnode->front = head;
newnode->next = phead;
phead->front = newnode;
printf("添加成功\n");
}

2.删除联系人

1
2
3
4
5
6
7
8
9
10
11
void DelPc(PC* pos)
{
assert(pos);
PC* Front = pos->front;
PC* Next = pos->next;
Front->next = Next;
Next->front = Front;
free(pos);
pos = NULL;
}

3.查找联系人

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
PC* Find_by_name( PC* phead, char* name)
{
assert(phead);
PC* pos = phead->next;
while (pos !=phead)
{
if (0 == strcmp(name, pos->name))
{
return pos;
}
pos = pos->next;
}
return NULL;
}

4.修改联系人

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
void MoDify(PC* pos)
{
char str_name2[NAM_MAX] = "0";
char str_sex2[SEX_MAX] = "0";
char str_tel2[TEL_MAX] = "0";
char str_adr2[ADR_MAX] = "0";
assert(pos);

printf("请输入新的姓名\n");
scanf("%s", str_name2);
strcpy(pos->name, str_name2);

printf("请输入新的年龄\n");
scanf("%d", &(pos->age));

printf("请输入新的性别\n");
scanf("%s", str_sex2);
strcpy(pos->sex, str_sex2);

printf("请输入新的电话\n");
scanf("%s",str_tel2);
strcpy(pos->tel, str_tel2);

printf("请输入新的地址\n");
scanf("%s", str_adr2);
strcpy(pos->adr, str_adr2);

}

5.显示所有联系人

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void Print(PC* phead)
{
PC* cur = phead->next;
assert(phead);
if (cur == phead)
{
printf("还没有添加信息\n");
}
printf("姓名 年龄 性别 电话 地址\n");
while (cur != phead)
{
printf("%-15s\t%-5d\t%-5s\t%-15s\t%-10s\n", cur->name, cur->age, cur->sex, cur->tel, cur->adr);
cur = cur->next;
}
}

6.清空所有联系人

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void DesTroy(PC* phead)
{
assert(phead);
PC* cur=phead->next;
while (cur != phead)
{
PC* Next = cur->next;
free(cur);
cur = Next;
}
phead->next = phead;
phead->front = phead;
}

7.按名字排序所有联系人

由于qsort是用于一个连续的空间,所以,这里用冒泡排序。
算个数的时候要加上phead,但是排序的时候不要排phead

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
void Compare_ByName(PC*phead)
{
int i =0;
int j = 0;
PC* cur = phead->next;
PC* Next = cur->next;
char Name_s[NAM_MAX] = "0";
int age_s=0;
char sex_s[SEX_MAX] = "0";
char tel_s[TEL_MAX] = "0";
char adr_s[ADR_MAX] = "0";
assert(phead);

for (i = 0; i < Size(phead)+1; i++)
{
for (j = 0; j < Size(phead) - i ;j++)
{
if ((strcmp(cur->name, Next->name) > 0) && (cur != phead) && (Next != phead))
{
strcpy(Name_s, cur->name);
strcpy(cur->name, Next->name);
strcpy(Next->name, Name_s);

age_s = cur->age;
cur->age = Next->age;
Next->age = age_s;

strcpy(sex_s, cur->sex);
strcpy(cur->sex, Next->sex);
strcpy(Next->sex, sex_s);

strcpy(tel_s, cur->tel);
strcpy(cur->tel, Next->tel);
strcpy(Next->tel, tel_s);

strcpy(adr_s, cur->adr);
strcpy(cur->adr, Next->adr);
strcpy(Next->adr, adr_s);
}
cur = cur->next;
Next = Next->next;
}
}
}

8.将数据保存在文件中

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
void SaveData(PC* phead)
{
FILE* fp = NULL;
PC* cur = phead->next;
assert(phead);
fp = fopen("data.txt", "w");
if (fp == NULL)
{
printf("%s", strerror(errno));
return;
}
while (cur != phead)
{
fprintf(fp, "%s ", cur->name);
fprintf(fp, "%d ", cur->age);
fprintf(fp, "%s ", cur->sex);
fprintf(fp, "%s ", cur->tel);
fprintf(fp, "%s ", cur->adr);
fprintf(fp, "\n");//换行
cur = cur->next;
}
fclose(fp);
fp = NULL;
}

在这里插入图片描述
contact.h文件中包含所需要的头文件以及函数的声明:

contact.h文件

以下就是contact.h的内容

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
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
#include<windows.h>
#include<errno.h>

#define NAM_MAX 20
#define SEX_MAX 10
#define TEL_MAX 20
#define ADR_MAX 20

typedef struct Contact
{
struct Contact* front;
char name[NAM_MAX];
int age;
char sex[SEX_MAX];
char tel[TEL_MAX];
char adr[ADR_MAX];
struct Contact* next;
}PC;

//申请空间√
PC* BuyNewNode(char* name, int age, char* sex, char* tel, char* adr);
void ADDPc(PC* phead);//增加联系人√
void DelPc(PC* pos);//删除联系人√
PC* Find_by_name(PC* phead,char* name);//通过姓名寻找联系人√
void Print(const PC* phead);//打印联系人√
void DesTroy(PC* phead);//销毁通讯录√
void MoDify(PC* pos);//修改联系人信息√
int Size(PC* phead);//计算数据数量√
void Compare_ByName(PC*phead);//按名字排序 abcdefg......√
void SaveData(PC*phead);// 保存数据√
void SaveFileData(PC*phead);//先将文件中的数据存到链表中√

contac.c是每个函数的实现:

contac.c文件

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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#define _CRT_SECURE_NO_WARNINGS 1
#include"contact.h"
struct Contact* BuyNewNode(char* name, int age, char* sex, char* tel, char* adr)
{
struct Contact* newnode = (struct Contact*)malloc(sizeof(struct Contact));
assert(newnode);
newnode->next = newnode;
newnode->front = newnode;
strcpy(newnode->name , name);
newnode->age = age;
strcpy(newnode->sex , sex);
strcpy(newnode->tel , tel);
strcpy(newnode->adr , adr);
return newnode;
}
void SaveFileData(PC*phead)
{

FILE* fp = fopen("data.txt", "r");
assert(phead);
if (fp == NULL)
{
printf("%s", strerror(errno));
return;
}
getc(fp);
while (!feof(fp))
{
rewind(fp);
PC* newnode = BuyNewNode("000000", 0, "000000", "00000", "000000");
fscanf(fp, "%s ", newnode->name);
fscanf(fp, "%d ", &(newnode->age));
fscanf(fp, "%s ", newnode->sex);
fscanf(fp, "%s ", newnode->tel);
fscanf(fp, "%s ", newnode->adr);
PC* head = phead->front;
head->next = newnode;
newnode->front = head;
newnode->next = phead;
phead->front = newnode;
}
fclose(fp);
fp = NULL;
}
void Print(PC* phead)
{
PC* cur = phead->next;
assert(phead);
if (cur == phead)
{
printf("还没有添加信息\n");
}
printf("姓名 年龄 性别 电话 地址\n");
while (cur != phead)
{
printf("%-15s\t%-5d\t%-5s\t%-15s\t%-10s\n", cur->name, cur->age, cur->sex, cur->tel, cur->adr);
cur = cur->next;
}
}
void ADDPc(PC* phead)
{
PC* newnode = BuyNewNode("000000", 0, "000000", "00000", "000000");
assert(phead);
assert(newnode);
printf("请输入姓名\n");
scanf("%s", newnode->name);
printf("请输入年龄\n");
scanf("%d", &(newnode->age));
printf("请输入性别\n");
scanf("%s", newnode->sex);
printf("请输入电话\n");
scanf("%s", newnode->tel);
printf("请输入地址\n");
scanf("%s", newnode->adr);
PC* head = phead->front;
head->next = newnode;
newnode->front = head;
newnode->next = phead;
phead->front = newnode;
printf("添加成功\n");
}
PC* Find_by_name( PC* phead, char* name)
{
assert(phead);
PC* pos = phead->next;
while (pos !=phead)
{
if (0 == strcmp(name, pos->name))
{
return pos;
}
pos = pos->next;
}
return NULL;
}
void DelPc(PC* pos)
{
assert(pos);
PC* Front = pos->front;
PC* Next = pos->next;
Front->next = Next;
Next->front = Front;
free(pos);
pos = NULL;
}
void DesTroy(PC* phead)
{
assert(phead);
PC* cur=phead->next;
while (cur != phead)
{
PC* Next = cur->next;
free(cur);
cur = Next;
}
free(phead);
phead = NULL;
}
void MoDify(PC* pos)
{
char str_name2[NAM_MAX] = "0";
char str_sex2[SEX_MAX] = "0";
char str_tel2[TEL_MAX] = "0";
char str_adr2[ADR_MAX] = "0";
assert(pos);

printf("请输入新的姓名\n");
scanf("%s", str_name2);
strcpy(pos->name, str_name2);

printf("请输入新的年龄\n");
scanf("%d", &(pos->age));

printf("请输入新的性别\n");
scanf("%s", str_sex2);
strcpy(pos->sex, str_sex2);

printf("请输入新的电话\n");
scanf("%s",str_tel2);
strcpy(pos->tel, str_tel2);

printf("请输入新的地址\n");
scanf("%s", str_adr2);
strcpy(pos->adr, str_adr2);

}
int Size(PC* phead)
{
int sz = 0;
PC* cur = phead->next;
assert(phead);
while (cur != phead)
{
sz++;
cur = cur->next;
}
return sz;
}

void Compare_ByName(PC*phead)
{
int i =0;
int j = 0;
PC* cur = phead->next;
PC* Next = cur->next;
char Name_s[NAM_MAX] = "0";
int age_s=0;
char sex_s[SEX_MAX] = "0";
char tel_s[TEL_MAX] = "0";
char adr_s[ADR_MAX] = "0";
assert(phead);

for (i = 0; i < Size(phead)+1; i++)
{
for (j = 0; j < Size(phead) - i ;j++)
{
if ((strcmp(cur->name, Next->name) > 0) && (cur != phead) && (Next != phead))
{
strcpy(Name_s, cur->name);
strcpy(cur->name, Next->name);
strcpy(Next->name, Name_s);

age_s = cur->age;
cur->age = Next->age;
Next->age = age_s;

strcpy(sex_s, cur->sex);
strcpy(cur->sex, Next->sex);
strcpy(Next->sex, sex_s);

strcpy(tel_s, cur->tel);
strcpy(cur->tel, Next->tel);
strcpy(Next->tel, tel_s);

strcpy(adr_s, cur->adr);
strcpy(cur->adr, Next->adr);
strcpy(Next->adr, adr_s);
}
cur = cur->next;
Next = Next->next;
}
}
}

void SaveData(PC* phead)
{
FILE* fp = NULL;
PC* cur = phead->next;
assert(phead);
fp = fopen("data.txt", "w");
if (fp == NULL)
{
printf("%s", strerror(errno));
return;
}
while (cur != phead)
{
fprintf(fp, "%s ", cur->name);
fprintf(fp, "%d ", cur->age);
fprintf(fp, "%s ", cur->sex);
fprintf(fp, "%s ", cur->tel);
fprintf(fp, "%s ", cur->adr);
fprintf(fp, "\n");//换行
cur = cur->next;
}
fclose(fp);
fp = NULL;
}

test.c是各个函数之间的调用:

test.c文件

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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#define _CRT_SECURE_NO_WARNINGS 1
#include"contact.h"
enum MyEnum
{
Exit,
Add,
Dele,
Find,
Modify,
Show,
Destroy,
Sort,
Save
};

void Menu()
{
//system("cls");
printf("*******************************\n");
printf("*********1.添加联系人*********\n");
printf("*********2.删除联系人***********\n");
printf("*********3.查找联系人************\n");
printf("*********4.修改联系人*******\n");
printf("*********5.显示所有联系人*******\n");
printf("*********6.清空所有联系人*******\n");
printf("*********7.按名字排序所有联系人*******\n");
printf("*********8.将数据保存在文件中*******\n");
printf("*********0.退出*******\n");
printf("************************\n");
}
int main(void)
{

PC* phead = BuyNewNode("000000", 0, "000000", "00000", "000000");//初始化一个头节点
SaveFileData(phead);
int input = 0;
int sz = 0;//求联系人的个数
char str_name[NAM_MAX] = "0";//用来通过姓名寻找
do
{
Sleep(500);
Menu();
printf("请输入你的选择->");
scanf("%d", &input);
switch (input)
{
case Exit:
printf("即将退出程序\n");
for (int i = 0; i < 10; i++)
{
printf("-");
Sleep(100);
}
break;

case Add:
if (phead == NULL)
{
phead= BuyNewNode("000000", 0, "000000", "00000", "000000");
}
ADDPc(phead);
break;
case Dele:
printf("请输入你要删除的姓名:\n");
scanf("%s", str_name);
PC* pos = Find_by_name(phead, str_name);
if (pos)
{
printf("姓名 年龄 性别 电话 地址\n");
printf("%-15s\t%-5d\t%-5s\t%-15s\t%-10s\n", pos->name, pos->age, pos->sex, pos->tel, pos->adr);
DelPc(pos);
printf("删除成功\n");
}
else
{
printf("没有找到\n");
}
break;

case Show:
sz = Size(phead);
printf("共有%d个联系人\n", sz);
Print(phead);
break;

case Find:
printf("请输入你要寻找的姓名:\n");
scanf("%s", str_name);
PC* pos1=Find_by_name(phead,str_name);
if (pos1)
{
printf("姓名 年龄 性别 电话 地址\n");
printf("%-15s\t%-5d\t%-5s\t%-15s\t%-10s\n", pos1->name, pos1->age, pos1->sex, pos1->tel, pos1->adr);
}
else
{
printf("没有找到\n");
}
break;

case Modify:
printf("请输入你要寻找的姓名:\n");
scanf("%s", str_name);
PC* pos2 = Find_by_name(phead, str_name);
if (pos2)
{
printf("姓名 年龄 性别 电话 地址\n");
printf("%-15s\t%-5d\t%-5s\t%-15s\t%-10s\n", pos2->name, pos2->age, pos2->sex, pos2->tel, pos2->adr);
MoDify(pos2);
}
else
{
printf("没有找到\n");
}
break;

case Destroy:
DesTroy(phead);
printf("销毁成功\n");
break;

case Sort:
sz = Size(phead);
printf("共有%d个联系人\n", sz);
Compare_ByName(phead);
Print(phead);
break;

case Save:
SaveData(phead);
printf("保存成功\n");
break;

default:
printf("选择错误,请重新选择\n");
break;
}
} while (input);
return 0;
}


双向循环链表实现—通讯录(学生管理系统,自行车管理系统,影院管理系统)—C语言课设(万能模板)—数据结构—用文件存储数据
https://6jackjiang.github.io/2022/02/11/categories/C语言/双向循环链表实现—通讯录(学生管理系统,自行车管理系统,影院管理系统)—C语言课设(万能模板)—数据结构—用文件存储数据/
作者
米兰的小铁匠
发布于
2022年2月11日
许可协议