<p>struct Info</p><p>{</p><p> unsigned long identifier;</p><p> char name[20];</p><p> struct Date date;</p><p> unsigned int years;</p><p> struct Info* next;</p><p>};</p>
收藏0 举报
void addInfo(struct Info** students)//students是头指针 { struct Info* info, *temp; info = (struct Info*)malloc(sizeof(struct Info)); if (info == NULL) { printf("内存分配失败!\n"); exit(1); } getInput(info); if (*students != NULL) { temp = *students; *students = info; info->next = temp; } else { *students = info; info->next = NULL; } } ❝ 由于students存放的是头指针,因此我们需要传入它的地址传递给函数,才能够改变它本身的值。而students本身又是一个指向Info结构体的指针,所以参数的类型应该就是struct Info**。 ❞ 往单链表里面添加一个结点,也就是先申请一个结点,然后判断链表是否为空。如果为空,那么直接将头指针指向它,然后next成员指向NULL。若不为空,那么先将next指向头指针原本指向的结点,然后将头指针指向新结点即可。 那么,打印链表也变得很简单: 复制void printStu(struct Info* students) { struct Info* info; int count = 1; info = students; while (book != NULL) { printf("Student%d:\n", count); printf("姓名:%s\n", info->name); printf("学号:%d\n", info->identifier); info = info->next; count++; } } 想要读取单链表里面的数据,只需要迭代单链表中的每一个结点,直到next成员为NULL,即表示单链表的结束。 最后,当然还是别忘了释放空间: 复制void releaseStu(struct Info** students) { struct Info* temp; while (*students != NULL) { temp = *students; *students = (*students)->next; free(temp); } } 尾插法 与头插法类似,尾插法就是把每一个数据都插入到链表的末尾。 复制void addInfo(struct Info** students) { struct Info* info, *temp; info = (struct Info*)malloc(sizeof(struct Info)); if (info == NULL) { printf("内存分配失败!\n"); exit(1); } getInput(info); if (*students != NULL) { temp = *students; *students = info; //定位到链表的末尾的位置 while (temp->next != NULL) { temp = temp->next; } //插入数据 temp->next = info; info->next = temp; } else { *students = info; info->next = NULL; } } 这么一来,程序执行的效率难免要降低很多,因为每次插入数据,都要先遍历一次链表。如果链表很长,那么对于插入数据来说就是一次灾难。不过,我们可以给程序添加一个指针,让它***都指向链表的尾部,这样一来,就可以用很少的空间换取很高的程序执行效率。 代码更改如下: 复制void addInfo(struct Info** students) { struct Info* info, *temp; static struct Info* tail;//设置静态指针 info = (struct Info*)malloc(sizeof(struct Info)); if (info == NULL) { printf("内存分配失败!\n"); exit(1); } getInput(info); if (*students != NULL) { tail->next = info; info->next = NULL; } else { *students = info; info->next = NULL; } }
void printStu(struct Info* students) { struct Info* info; int count = 1; info = students; while (book != NULL) { printf("Student%d:\n", count); printf("姓名:%s\n", info->name); printf("学号:%d\n", info->identifier); info = info->next; count++; } } 想要读取单链表里面的数据,只需要迭代单链表中的每一个结点,直到next成员为NULL,即表示单链表的结束。 最后,当然还是别忘了释放空间: 复制void releaseStu(struct Info** students) { struct Info* temp; while (*students != NULL) { temp = *students; *students = (*students)->next; free(temp); } } 尾插法 与头插法类似,尾插法就是把每一个数据都插入到链表的末尾。 复制void addInfo(struct Info** students) { struct Info* info, *temp; info = (struct Info*)malloc(sizeof(struct Info)); if (info == NULL) { printf("内存分配失败!\n"); exit(1); } getInput(info); if (*students != NULL) { temp = *students; *students = info; //定位到链表的末尾的位置 while (temp->next != NULL) { temp = temp->next; } //插入数据 temp->next = info; info->next = temp; } else { *students = info; info->next = NULL; } } 这么一来,程序执行的效率难免要降低很多,因为每次插入数据,都要先遍历一次链表。如果链表很长,那么对于插入数据来说就是一次灾难。不过,我们可以给程序添加一个指针,让它***都指向链表的尾部,这样一来,就可以用很少的空间换取很高的程序执行效率。 代码更改如下: 复制void addInfo(struct Info** students) { struct Info* info, *temp; static struct Info* tail;//设置静态指针 info = (struct Info*)malloc(sizeof(struct Info)); if (info == NULL) { printf("内存分配失败!\n"); exit(1); } getInput(info); if (*students != NULL) { tail->next = info; info->next = NULL; } else { *students = info; info->next = NULL; } }
void releaseStu(struct Info** students) { struct Info* temp; while (*students != NULL) { temp = *students; *students = (*students)->next; free(temp); } } 尾插法 与头插法类似,尾插法就是把每一个数据都插入到链表的末尾。 复制void addInfo(struct Info** students) { struct Info* info, *temp; info = (struct Info*)malloc(sizeof(struct Info)); if (info == NULL) { printf("内存分配失败!\n"); exit(1); } getInput(info); if (*students != NULL) { temp = *students; *students = info; //定位到链表的末尾的位置 while (temp->next != NULL) { temp = temp->next; } //插入数据 temp->next = info; info->next = temp; } else { *students = info; info->next = NULL; } } 这么一来,程序执行的效率难免要降低很多,因为每次插入数据,都要先遍历一次链表。如果链表很长,那么对于插入数据来说就是一次灾难。不过,我们可以给程序添加一个指针,让它***都指向链表的尾部,这样一来,就可以用很少的空间换取很高的程序执行效率。 代码更改如下: 复制void addInfo(struct Info** students) { struct Info* info, *temp; static struct Info* tail;//设置静态指针 info = (struct Info*)malloc(sizeof(struct Info)); if (info == NULL) { printf("内存分配失败!\n"); exit(1); } getInput(info); if (*students != NULL) { tail->next = info; info->next = NULL; } else { *students = info; info->next = NULL; } }
void addInfo(struct Info** students) { struct Info* info, *temp; info = (struct Info*)malloc(sizeof(struct Info)); if (info == NULL) { printf("内存分配失败!\n"); exit(1); } getInput(info); if (*students != NULL) { temp = *students; *students = info; //定位到链表的末尾的位置 while (temp->next != NULL) { temp = temp->next; } //插入数据 temp->next = info; info->next = temp; } else { *students = info; info->next = NULL; } } 这么一来,程序执行的效率难免要降低很多,因为每次插入数据,都要先遍历一次链表。如果链表很长,那么对于插入数据来说就是一次灾难。不过,我们可以给程序添加一个指针,让它***都指向链表的尾部,这样一来,就可以用很少的空间换取很高的程序执行效率。 代码更改如下: 复制void addInfo(struct Info** students) { struct Info* info, *temp; static struct Info* tail;//设置静态指针 info = (struct Info*)malloc(sizeof(struct Info)); if (info == NULL) { printf("内存分配失败!\n"); exit(1); } getInput(info); if (*students != NULL) { tail->next = info; info->next = NULL; } else { *students = info; info->next = NULL; } }
void addInfo(struct Info** students) { struct Info* info, *temp; static struct Info* tail;//设置静态指针 info = (struct Info*)malloc(sizeof(struct Info)); if (info == NULL) { printf("内存分配失败!\n"); exit(1); } getInput(info); if (*students != NULL) { tail->next = info; info->next = NULL; } else { *students = info; info->next = NULL; } }
struct Info *searchInfo(struct Info* students, long* target) { struct Info* info; info = students; while (info != NULL) { if (info->identifier == target) { break; } info = info->next; } return book; }; void printInfo(struct Info* info) { ... } ... int main(void) { ... printf("\n请输入学生学号:"); scanf("%d", input); info = searchInfo(students, input); if (info == NULL) { printf("抱歉,未找到相关结果!\n"); } else { do { printf("相关结果如下:\n"); printInfo(book); } while ((info = searchInfo(info->next, input)) != NULL); } releaseInfo(...); return 0; }
//Example 03 #include <stdio.h> #include <stdlib.h> struct Node { int value; struct Node* next; }; void insNode(struct Node** head, int value) { struct Node* pre; struct Node* cur; struct Node* New; cur = *head; pre = NULL; while (cur != NULL && cur->value < value) { pre = cur; cur = cur->next; } New = (struct Node*)malloc(sizeof(struct Node)); if (New == NULL) { printf("内存分配失败!\n"); exit(1); } New->value = value; New->next = cur; if (pre == NULL) { *head = New; } else { pre->next = New; } } void printNode(struct Node* head) { struct Node* cur; cur = head; while (cur != NULL) { printf("%d ", cur->value); cur = cur->next; } putchar('\n'); } int main(void) { struct Node* head = NULL; int input; printf("开始插入整数...\n"); while (1) { printf("请输入一个整数,输入-1表示结束:"); scanf("%d", &input); if (input == -1) { break; } insNode(&head, input); printNode(head); } return 0; } 运行结果如下: 复制//Consequence 03 开始插入整数... 请输入一个整数,输入-1表示结束:4 4 请输入一个整数,输入-1表示结束:5 4 5 请输入一个整数,输入-1表示结束:3 3 4 5 请输入一个整数,输入-1表示结束:6 3 4 5 6 请输入一个整数,输入-1表示结束:2 2 3 4 5 6 请输入一个整数,输入-1表示结束:5 2 3 4 5 5 6 请输入一个整数,输入-1表示结束:1 1 2 3 4 5 5 6 请输入一个整数,输入-1表示结束:7 1 2 3 4 5 5 6 7 请输入一个整数,输入-1表示结束:-1
//Consequence 03 开始插入整数... 请输入一个整数,输入-1表示结束:4 4 请输入一个整数,输入-1表示结束:5 4 5 请输入一个整数,输入-1表示结束:3 3 4 5 请输入一个整数,输入-1表示结束:6 3 4 5 6 请输入一个整数,输入-1表示结束:2 2 3 4 5 6 请输入一个整数,输入-1表示结束:5 2 3 4 5 5 6 请输入一个整数,输入-1表示结束:1 1 2 3 4 5 5 6 请输入一个整数,输入-1表示结束:7 1 2 3 4 5 5 6 7 请输入一个整数,输入-1表示结束:-1
... void delNode(struct Node** head, int value) { struct Node* pre; struct Node* cur; cur = *head; pre = NULL; while (cur != NULL && cur->value != value) { pre = cur; cur = cur->next; } if (cur == NULL) { printf("未找到匹配项!\n"); return ; } else { if (pre == NULL) { *head = cur->next; } else { pre->next = cur->next; } free(cur); } }
中国龙芯CDX 发表于 2024-4-10 15:21 链表的增删改查都是基本操作,还需要多加熟练
本版积分规则 发表回复 回帖并转播 回帖后跳转到最后一页
262
1929
3
扫码关注 21ic 官方微信
扫码关注嵌入式微处理器
扫码关注电源系统设计
扫码关注21ic项目外包
扫码浏览21ic手机版
本站介绍 | 申请友情链接 | 欢迎投稿 | 隐私声明 | 广告业务 | 网站地图 | 联系我们 | 诚聘英才
京公网安备 11010802024343号