原题链接:[编程入门]链表合并
果然是课后题里面最后一道,还是花了一些时间做出来的。其实不是很难,只不过得注意链表细节的地方。
具体参考代码如下:
#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分
30 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
//第一个链表的尾结点连接第二个链表的首结点 tmp1->pnext = tmp2->pnext; 这一步为啥不是: tmp1->pnext = tmp2啊!@弈 #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; }if (head == NULL) return NULL; 请问这两行有什么用啊