#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
struct student
{
int num;
int score;
struct student *link;
};
struct student *createlist(int n)//创建链表
{
int i;
struct student *dshead, *dslink, *dsnew;
dshead = (struct student*)malloc(sizeof(struct student));
dsnew = dslink = dshead;
dshead->num = 0;
dshead->score = 0;
dshead->link = NULL;
for (i = 0; i < n; i++)
{
dsnew = (struct student*)malloc(sizeof(struct student));
dslink->link = dsnew;
scanf("%d %d", &dsnew->num, &dsnew->score);
dsnew->link = NULL;
dslink = dsnew;
}
return dshead;
}
struct student *merge(struct student *dshead1, struct student *dshead2)//合并链条
{
struct student *dslink = dshead1;
while (dslink->link != NULL)
{
dslink = dslink->link;
}
dslink->link = dshead2->link;
return dshead1;
}
void output(struct student *dshead2)//输出链条
{
struct student *dsnow;
dsnow = dshead2->link;
do
{
printf("%d %d\n", dsnow->num, dsnow->score);
dsnow = dsnow->link;
} while (dsnow != NULL);
}
int main()
{
int n, m;
scanf("%d %d",&n,&m);
struct student *dshead1 = createlist(n);
struct student *dshead2 = createlist(m);
struct student *p0,*p1;//p1记录当前位置,p0前一位置
dshead1 = merge(dshead1, dshead2);//合并后的链条头
p0 = dshead1;
p1 = dshead1->link;
int min,k = 0;
struct student *dslink,*dsnew;
for (int i = 1; i <= m + n; i++)//每次选出最小的序号
{
p1 = dshead1->link;
min = p1->num;
while (p1 != NULL)//找出剩下的最小值
{
if (p1->num < min)
{
min = p1->num;
}
p1 = p1->link;
}
k++;//用来区分是否是第一次,只有第一次需要对链头连接第二链
p0 = dshead1;
p1 = dshead1->link;
while (p1->num != min)//找到最小值对应的位置,拼接在新的链条后面
{
p0 = p1;
p1 = p1->link;
}
p0->link = p1->link;
if (k == 1)
{
dshead2->link = p1;
dslink = p1;
dslink->link = NULL;
}
else
{
dslink->link = p1;
dslink = dslink->link;
dslink->link = NULL;
}
}
output(dshead2);
system("pause");
return 0;
}
0.0分
0 人评分
C语言训练-求函数值 (C语言代码)浏览:944 |
C语言程序设计教程(第三版)课后习题8.8 (C语言代码)浏览:610 |
C语言程序设计教程(第三版)课后习题12.2 (C语言代码)浏览:855 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:633 |
WU-小九九 (C++代码)浏览:1713 |
1014题解浏览:524 |
简单的a+b (C语言代码)浏览:1024 |
字符串比较 (C语言代码)浏览:770 |
链表数据求和操作 (C语言代码)浏览:1035 |
字符逆序 (C语言代码)浏览:541 |