C语言 day21 | 我的日常分享

C语言 day21

链表

链表的查询

main.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
... ...
else if (strcmp(cmd, "search") == 0) {
//printf("-------search--------\n");
STU *res=NULL;
char name[64]="";
//memset(&res, 0, sizeof(STU));
printf("请输入要查询的姓名:\n");
scanf("%s", name);
res = search_link(head,name);
if (res == NULL)
{
printf("没有查到%s的有关信息\n",name);
}
else
{
printf("%s的信息:\n",name);
printf("%d %s %f\n", res->num, res->name, res->score);
}
}
... ...

link.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
//查询节点
STU* search_link(STU* head, char* name) {
STU* pb = head;
//判断链表是否存在
if (pb == NULL) {
printf("链表不存在\n");
return head;
}
else
{
//寻找对应节点
while (strcmp(pb->name, name) != 0 && pb->next != NULL) {//pb->next != NULL防止越界 出现段错误
pb = pb->next;
}

//判断是否找到
if (strcmp(pb->name, name) == 0)
{
return pb;
}
else
{
return NULL;
}
}

//return pb;
}


链表节点的删除

main.c

1
2
3
4
5
6
7
8
9
10
... ...
else if (strcmp(cmd, "delete") == 0) {
//printf("-------delete--------\n");
char name[64] = "";
printf("请输入要删除的姓名:\n");
scanf("%s", name);
head = delete_link(head, name);//要更新链表头 所以要返回值 因为如果删除的是头节点链表头就更新了
}

... ...

link.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
STU* delete_link(STU *head, char *name) {
STU* pb = head,*pf=head;//pf跟在pb后面 用于后面删除中部节点
//判断链表是否存在
if (pb == NULL)
{
printf("链表不存在\n");
return head;
}
else//链表存在
{
//寻找要删除节点
while (strcmp(pb->name, name) != 0 && pb->next != NULL) //pb->next != NULL防止越界 出现段错误
{
pf = pb;
pb = pb->next;
}
//判断删除节点是否存在
if (strcmp(pb->name, name) == 0)//存在
{
//判断节点的位置
if (pb == head)//头部
{
head = pb->next;
free(pb);
return head;
}
else if (pb->next!=NULL && pb!=head)//中部
{
pf->next = pb->next;
free(pb);
}
else if (pb->next==NULL)//尾部
{
pf->next = NULL;
free(pb);
}
}
else//不存在
{
printf("%s节点不存在\n",name);
}
}
return head;
}


释放链表

main.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
··· ···
else if (strcmp(cmd, "free") == 0) {
//printf("-------free--------\n");
char sure[8] = "";
printf("确定要释放链表?(Y/N)");
scanf("%s", sure);
if(strcmp(sure,"Y")==0)
{
head = free_link(head);//别忘了更新链表头 链表释放完成后,链表头指向的还是原来的地址,所以要更新
}
else
{
continue;
}
}
··· ···

link.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//释放链表
STU* free_link(STU* head) {
STU* pb = head,*head_tmp=head;
//判断链表是否存在
if (pb == NULL) //不存在
{
printf("链表不存在\n");
return;
}
else//存在
{
while (pb!=NULL)
{
head_tmp = pb->next;
free(pb);
pb = head_tmp;
}
printf("链表释放完成!\n");
return NULL;
}
return NULL;
}


链表的逆序

main.c

1
2
3
4
5
6
7
··· ···
else if (strcmp(cmd, "reserve") == 0)
{
//printf("-------reserve----------");
head = reserve_link(head);
}
··· ···

link.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
//链表逆序
STU* reserve_link(STU* head) {
//1、判断链表是否存在
if (head == NULL)
{
printf("链表不存在!\n");
return head;
}
else
{
STU* pb = head, * pr = head;
pb = pb->next;
head->next = NULL;

while (pb != NULL)
{
pr = pb->next;//记录pb->next
pb->next = head;
head = pb;
pb = pr;

}
return head;
}
}

链表的整理

按学号进行排序
main.c

1
2
3
4
5
6
7
··· ···
else if (strcmp(cmd, "sort") == 0)
{
//printf("-------sort----------");
head = sort_link(head);
}
··· ···

link.c

1
2
3
4
5
6
//链表的整理
STU* sort_link(STU* head) {

//还没写呢~
return head;
}

学生信息系统完整代码(未写完) 下载