果然是课后题里面最后一道,还是花了一些时间做出来的。其实不是很难,只不过得注意链表细节的地方。
具体参考代码如下:
#include <stdio.h> #include <stdlib.h> #include <string.h> //定义一个学生结构体 typedef struct Stu { int id; int score; struct Stu *pnext; }Stu; //创建一个链表 Stu *create_Stu(int n) { //先定义一个头结点,不存放有效数据 Stu *head = (Stu*)malloc(sizeof(Stu)); if (head == NULL) return NULL; head->id = -1; head->score = -1; head->pnext = NULL; //输入要加入的n个结点的信息,新的结点加到链表最后 Stu *tmp = head; int i; for (i = 0; i < n; i++) { Stu *new_node = (Stu*)malloc(sizeof(Stu)); if (new_node == NULL) return NULL; scanf("%d", &new_node->id); scanf("%d", &new_node->score); new_node->pnext = NULL; //新节点加到链表最后 tmp->pnext = new_node; tmp = new_node; } return head; } //将一个链表与另一个链表合并,返回第一个链表头部 Stu *merge_Stu(Stu *students1, Stu *students2) { if (students1 == NULL || students2 == NULL) return NULL; //先找到第一个链表的尾结点 Stu *tmp1 = students1; while (tmp1->pnext != NULL) { tmp1 = tmp1->pnext; } //出来之后tmp1就为第一个链表的尾结点 Stu *tmp2 = students2; //第一个链表的尾结点连接第二个链表的首结点 tmp1->pnext = tmp2->pnext; //不要忘记释放第二个链表的头结点 free(tmp2); //返回合并之后的头结点 return students1; } //链表结点排序重组 void sort_Stu(Stu *students) { if (students == NULL) return; Stu *pre = NULL; Stu *cur = NULL; Stu tmp; //选择法对结点进行排序 for (pre = students->pnext; pre->pnext != NULL; pre = pre->pnext) { for (cur = pre->pnext; cur != NULL; cur = cur->pnext) { if (pre->id > cur->id) { //数据域和指针域都要进行交换 //数据域交换 tmp = *pre; *pre = *cur; *cur = tmp; //指针域交换 tmp.pnext = pre->pnext; pre->pnext = cur->pnext; cur->pnext = tmp.pnext; } } } } //打印链表信息 void print_Stu(Stu *students) { if (students == NULL || students->pnext == NULL) { printf("invalid list!\n"); return; } Stu *cur = students->pnext; //指向首结点 while (cur != NULL) { printf("%d %d\n", cur->id, cur->score); cur = cur->pnext; } } //释放整个链表 void destory_Stu(Stu *students) { if (students == NULL) return; Stu *s = students; Stu *tmp = NULL; //用来保存当前所释放的结点的下一个结点 while (s != NULL) { tmp = s->pnext; free(s); s = tmp; } } int main() { int N, M; scanf("%d %d", &N, &M); //创建两个链表 Stu *students1 = create_Stu(N); Stu *students2 = create_Stu(M); //合并两个链表 Stu *students = merge_Stu(students1, students2); //对新链表中的内容按学号升序排列并打印 sort_Stu(students); print_Stu(students); destory_Stu(students); return 0; }
欢迎留言讨论~谢谢!
0.0分
41 人评分
Stu *create_Stu 这里为什么返回值类型为Stu啊,大佬们解释下!
maikeis 2021-04-12 14:35:04 |
可以先去了解一下typedef
//第一个链表的尾结点连接第二个链表的首结点 tmp1->pnext = tmp2->pnext; 这一步为啥不是: tmp1->pnext = tmp2啊!
if (head == NULL) return NULL; 请问这两行有什么用啊
弈 2019-03-02 18:48:58 |
#include "stdio.h" #include "malloc.h" #define L sizeof(struct Stu) typedef struct Stu { int id; int score; struct Stu *next; }Stu; struct Stu *creat(int n) { struct Stu *head=(Stu*)malloc(L); struct Stu *tmp; int i; if (head==NULL) return NULL; head->id=-1; head->score=-1; head->next=NULL; tmp=head; for(i=0;i<n;i++) { struct Stu *node=(Stu*)malloc(L); if (node==NULL) return NULL; scanf("%d", &node->id); scanf("%d", &node->score); node->next=NULL; tmp->next=node; tmp=node; } return head; } struct Stu *merge(struct Stu *students1,struct Stu *students2) { struct Stu *tmp1=students1; Stu *tmp2=students2; if (students1==NULL||students2==NULL) return NULL; while (tmp1->next!=NULL) { tmp1=tmp1->next; } tmp1->next=tmp2->next; free(tmp2); return students1; } void sort(Stu *students) { struct Stu *pre=NULL; struct Stu *cur=NULL; struct Stu tmp; if (students==NULL) return; for (pre=students->next;pre->next!=NULL;pre=pre->next) { for (cur=pre->next;cur!=NULL;cur=cur->next) { if (pre->id>cur->id) { tmp=*pre; *pre=*cur; *cur=tmp; tmp.next=pre->next; pre->next=cur->next; cur->next=tmp.next; } } } } void print(Stu *students) { struct Stu *cur=students->next; while (cur!=NULL) { printf("%d %d\n", cur->id, cur->score); cur=cur->next; } } void destory(Stu *students) { struct Stu *s=students; struct Stu *tmp=NULL; if (students==NULL) return; while (s!=NULL) { tmp=s->next; free(s); s=tmp; } } int main() { int N, M; struct Stu *students1; struct Stu *students2; struct Stu *students; scanf("%d%d",&N,&M); students1=creat(N); students2=creat(M); students=merge(students1,students2); sort(students); print(students); destory(students); return 0; }
C杯 2019-08-18 18:51:38 |
head==NULL说明是空链表,就不需要排序,直接退出函数
林致远 2020-11-04 20:02:37 |
增强代码的健壮性
C语言程序设计教程(第三版)课后习题8.3 (C语言代码)浏览:617 |
汽水瓶 (C语言代码)浏览:597 |
C语言程序设计教程(第三版)课后习题5.7 (C++代码)浏览:846 |
C语言程序设计教程(第三版)课后习题10.1 (Java代码)浏览:1436 |
输出正反三角形 (C语言代码)格式错误!!!浏览:1140 |
C语言训练-尼科彻斯定理 (C语言代码)浏览:463 |
WU-格式化数据输出 (C++代码)浏览:1194 |
C语言程序设计教程(第三版)课后习题6.3 (C++代码)浏览:950 |
2005年春浙江省计算机等级考试二级C 编程题(2) (C语言代码)浏览:647 |
1128题解(返回值为数组的情况)浏览:451 |