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
#include
#include
#include
typedef struct stu
{
int num;
char name[21];
struct stu *next;
}stu;

stu* creat(int id,char str[]);
void add(stu *p,int id,char str[]);
int query_value(stu *p,char name[]);
stu* query_id(stu *p,int id);
int del_list(stu *p,int id);
void menu(stu *p);
void menu_add(stu *p);
void menu_edit(stu *p);

int main(){
stu *p=NULL;
p=creat(0,"NULL");
menu(p);
while(p!=NULL){
printf("name=%s,id=%d\n",p->name,p->num);
p=p->next;
}
}

stu* creat(int id,char str[]){//创建链表节点
stu *new;
if((new=(stu *)malloc(sizeof(stu)))==NULL){
printf("不能分配内存空间!");
exit(0);
}
new->num=id;
strcpy(new->name,str);
new->next=NULL;
return new;
}

void add(stu *p,int id,char str[]){//链表尾插入新节点
stu *new=NULL;
new=creat(id,str);
while(p->next!=NULL){
p=p->next;
}
p->next=new;
}
int query_value(stu *p,char name[]){//按照值查找节点,并返回节点序号
int i=0;
while((p!=NULL)){
if (strcmp(p->name,name)==0){
return i;
}
p=p->next;
i=i+1;
}
return 0;
}

stu* query_id(stu *p,int id){//传入指针节点序号,并返回节点指针
for (int i = 0; i < id; ++i){ p=p->next;
}
return p;
}

int del_list(stu *p,int id){//传入节点序号,删除节点
stu *p1,*s;
p1=query_id(p,id-1);
if (p1==NULL){//前一个节点不存在
return -1;
}
else{
if (p->next==NULL){//被删除节点不存在
return 0;
}
else{
s=p->next;
p->next=s->next;
free(s);
return 1;
}
}

}
void menu(stu *p){
int input;
printf("欢迎使用学生学籍管理系统\n");
printf("1.插入学生\n");
printf("2.修改学生\n");
printf("3.输出列表\n");
printf("请输入选项:");
scanf("%d",&input);
switch(input){
case 1:{
menu_add(p);
menu(p);
break;
}
case 2:{
menu_edit(p);
menu(p);
break;
}
case 3:break;
}
}
void menu_add(stu *p){
int id;
char name[21];
printf("请输入要添加的姓名:\n");
scanf("%s",name);
printf("请输入学号:\n");
scanf("%d",&id);
add(p,id,name);
}

void menu_edit(stu *p){
int i;
stu *p1;
char name[21];
printf("请输入要修改的姓名:\n");
scanf("%s",name);
i=query_value(p,name);
p1=query_id(p,i);
printf("请输入新的姓名:\n");
scanf("%s",p1->name);

}