本文实例为大家分享了C语言单链表实现通讯录管理系统的具体代码,供大家参考,具体内容如下
本人前几天刚刚自学了单链表,趁热打铁,赶紧写一个小小的项目练练手。
单链表的实现在本人之前的博客中有:C语言编写一个链表
通讯录管理系统
保存人的信息有:
名字 name
电话 telephone
性别 sex
年龄 age
用一个结构体来装这些信息:
1
2
3
4
5
6
|
struct infor{
char name[20];
int age;
char sex[8];
char telephone[16];
};
|
实现功能有:
增加联系人
删除联系人
修改联系人
寻找联系人
显示联系人
首先建立链表的基本功能创建头链表,创建节点,插入节点
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
|
struct addre* Creathead(){ //创建头链表
struct addre *headnode = ( struct addre*) malloc ( sizeof ( struct addre));
if (headnode == NULL){
printf ( "malloc error\n" );
}
headnode->next = NULL;
return headnode;
}
struct addre* Creatlist( struct infor *list){ //创建节点
struct addre *node = ( struct addre*) malloc ( sizeof ( struct addre));
if (node == NULL){
printf ( "malloc error\n" );
}
node->next = NULL;
node->people.age = list->age;
strcpy (node->people.name, list->name);
strcpy (node->people.sex, list->sex);
strcpy (node->people.telephone, list->telephone);
return node;
}
void Addlist( struct addre *headnode, struct infor *list){ //插入节点
struct addre *t = headnode;
while (t->next != NULL){
t = t->next;
}
struct addre *nodelist = Creatlist(list);
t->next = nodelist;
nodelist->next = NULL;
}
|
然后在实现通讯录的功能
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
|
void Addpeople( struct addre* node){ //添加人的信息
struct infor *a= malloc ( sizeof ( struct infor)); // 创建动态信息结构体指针
if (a == NULL){
printf ( "malloc error\n" );
}
printf ( "请输入名字\n" );
scanf ( "%s" , a->name);
printf ( "请输入年龄\n" );
scanf ( "%d" , &a->age);
printf ( "请输入性别\n" );
scanf ( "%s" , a->sex);
printf ( "请输入电话号码\n" );
scanf ( "%s" , a->telephone);
Addlist(node, a); //用尾插法插入该人信息
printf ( "添加成功!\n" );
}
void Deletepeople( struct addre *node){ //删除人的信息
char *str = malloc ( sizeof ( char )* 10);
if (str == NULL){ //通过名字寻找
printf ( "malloc error\n" );
}
printf ( "请输入要删除人的姓名\n" );
scanf ( "%s" , str);
struct addre *strat = node;
struct addre *end = node->next;
int flag = 0; //判断是否找到 0为未找到,1 找到
while (end){ //判断end的 不然会越界
if ( strcmp (end->people.name, str) == 0){
flag = 1;
break ;
}
node = node->next; //到下一个链表
strat = node; //一个指向前面 一个指向后面,删除将end删除,前面那个直接指向end的指向
end = node->next;
}
if (flag){
strat->next = end->next;
printf ( "删除成功\n" );
free (end);
}
else {
printf ( "没找到!\n" );
}
}
void Modifyinfor( struct addre *node){ //修改人的信息
char *str = malloc ( sizeof ( char )* 10); //通过名字寻找
if (str == NULL){
printf ( "malloc error\n" );
}
printf ( "请输入要修改人的姓名\n" );
scanf ( "%s" , str);
int flag = 0;
while (node){
if ( strcmp (node->people.name, str) == 0){
flag = 1;
break ;
}
node = node->next;
}
if (flag){
printf ( "请重新输入该人信息\n" );
printf ( "请输入名字\n" );
scanf ( "%s" , node->people.name);
printf ( "请输入年龄\n" );
scanf ( "%d" , &node->people.age);
printf ( "请输入性别\n" );
scanf ( "%s" , node->people.sex);
printf ( "请输入电话号码\n" );
scanf ( "%s" , node->people.telephone);
printf ( "修改成功\n" );
}
else {
printf ( "没找到\n" );
}
}
void Foundpeople( struct addre *node){ //找到某人的信息并打印出来
char *str = malloc ( sizeof ( char )* 10); //通过名字寻找
if (str == NULL){
printf ( "malloc error\n" );
}
printf ( "请输入要查找人的姓名\n" );
scanf ( "%s" , str);
int flag = 0;
while (node){
if ( strcmp (node->people.name, str) == 0){
flag = 1;
break ;
}
node = node->next;
}
if (flag){
printf ( "name\tage\tsex\ttelephone\n" );
printf ( "%s\t" , node->people.name);
printf ( "%d\t" , node->people.age);
printf ( "%s\t" , node->people.sex);
printf ( "%s\t" , node->people.telephone);
}
else {
printf ( "没找到!\n" );
}
}
void Display( struct addre *node){
struct addre *pmove = node->next; //要从头节点的下一个节点显示信息
printf ( "name\tage\tsex\ttelephone\n" );
while (pmove){
printf ( "%s\t%d\t%s\t%s\n" , pmove->people.name, pmove->people.age, pmove->people.sex, pmove->people.telephone);
pmove = pmove->next;
}
}
|
其它代码
菜单:
1
2
3
4
5
6
7
8
|
void Menu(){
printf ( "+-----------------+\n" );
printf ( "+-1.add 2.delet-+\n" );
printf ( "+-3.modify 4.seek-+\n" );
printf ( "+-5.display6.exit-+\n" );
printf ( "+-----------------+\n" );
printf ( "Please Enter Your Select\n" );
}
|
本人使用的时多文件的方式上面的代码都在函数定义的源文件里实现
main函数的源文件代码:
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
|
#include"addressbank.h"
/***********************
信息:
名字 name
年龄 age
电话 telephone
性别 sex
功能:
增加
删除
修改
寻找
打印
显示
***********************/
int main(){
struct addre* node = Creathead();
int quit = 0;
while (!quit){
Menu();
int select = 0;
scanf ( "%d" , &select);
switch (select){
case 1:
Addpeople(node);
break ;
case 2:
Deletepeople(node);
break ;
case 3:
Modifyinfor(node);
break ;
case 4:
Foundpeople(node);
break ;
case 5:
Display(node);
break ;
case 6:
quit = 1;
break ;
default :
printf ( "Enter Error!\n" );
break ;
}
}
system ( "pause" );
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
|
#ifndef __ADDRESSBANK_H__
#define __ADDRESSBANK_H__
#include<stdio.h>
#include<string.h>
struct infor{ //信息结构体
char name[20];
int age;
char sex[8];
char telephone[16];
};
struct addre{ //链表
struct infor people;
struct addre *next;
};
//功能函数
extern void Menu();
extern void Addpeople( struct addre *node);
extern void Deletepeople( struct addre *node);
extern void Modifyinfor( struct addre *node);
extern void Foundpeople( struct addre *node);
extern void Display( struct addre *node);
//下面未链表函数
extern struct addre* Creatlist( struct infor *list);
extern struct addre* Creathead();
extern void Addlist( struct addre *headnode, struct infor *list);
#endif
|
代码可直接复制使用,如有不足请提出,后续修改谢谢
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/weixin_57023347/article/details/117364918