解题思路:先定义一个带指针的结构体,然后定义一个创建链表的函数,一个连接两个链表的函数,一个排序并删除重复的函数,总的来说这就是链表的创建、合并和删除.
注意事项:1.链表创建函数中,链表创建结束的结构体中的结构体指针得赋成NULL,链表结束字符。
2.链表排序和删除时,应该free()掉重复的结构体
参考代码:
#include<stdio.h>
#include<stdlib.h>
//声明模块
typedef struct students{
int Id;
int score;
struct students *next;
}Stu;
Stu *creatl(int n);
void linkS(Stu *x1, Stu *x2);
void sort(Stu *x);
//主函数模块
int main()
{
int n, m;
scanf("%d %d", &n, &m);
Stu *head_a;
Stu *head_b;
head_a = creatl(n);
head_b = creatl(m);
linkS(head_a, head_b);
sort(head_a);
Stu *head;
head = head_a->next;
while (head!=NULL)
{
printf("%d %d\n", head->Id, head->score);
head = head->next;
}
free( head_a);
free( head_b);
free( head);
return 0;
}
//链表创建函数
Stu *creatl(int n)
{
Stu *head, *node, *end;
head = (Stu *)malloc(sizeof(Stu));
end = head;
for (int i = 0; i < n; i++)
{
node = (Stu *)malloc(sizeof(Stu));
scanf("%d %d", &node->Id, &node->score);
end->next = node;
end = node;
}
end->next = NULL; //链表结束指针字符
return head;
}
//链表连接函数
void linkS(Stu *x1,Stu *x2)
{
while (1)
{
if (x1->next == NULL)
{
x1->next = x2->next;
break;
}
x1 = x1->next;
}
}
//链表排序并删除重复序号函数函数
void sort(Stu *x)
{
Stu *pt, *pr;
int a, b;
for (pt = x; pt != NULL; pt = pt->next)
{
for (pr = pt->next; pr != NULL; pr = pr->next)
{
if (pt->Id > pr->Id)
{
a = pt->Id;
b = pt->score;
pt->Id = pr->Id;
pt->score = pr->score;
pr->Id = a;
pr->score = b;
}
}
}
}
实测通过
0.0分
1 人评分
点我有惊喜!你懂得!浏览:1393 |
C语言程序设计教程(第三版)课后习题11.12 (C语言代码)浏览:719 |
A+B for Input-Output Practice (IV) (C语言代码)浏览:509 |
C语言程序设计教程(第三版)课后习题7.1 (C语言代码)浏览:1222 |
WU-输入输出格式练习 (C++代码)浏览:1076 |
母牛的故事 (C语言代码)浏览:549 |
敲七 (C++代码)浏览:1050 |
字符逆序 (C语言代码)浏览:504 |
C语言程序设计教程(第三版)课后习题8.6 (C语言代码)浏览:585 |
买不到的数目 (C语言代码)浏览:3090 |