#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 人评分
Biggest Number (错误代码时间超限制)浏览:2249 |
2004年秋浙江省计算机等级考试二级C 编程题(2) (C语言代码)浏览:687 |
C二级辅导-阶乘数列 (C语言代码)浏览:637 |
数列 (C++代码)浏览:704 |
C语言程序设计教程(第三版)课后习题5.4 (C语言代码)浏览:695 |
【回文数(二)】 (C++代码)浏览:922 |
C语言程序设计教程(第三版)课后习题11.1 (C语言代码)浏览:816 |
数组输出 (C语言代码)--此题的题目描述有问题浏览:1829 |
【亲和数】 (C语言代码)浏览:538 |
wu-理财计划 (C++代码)浏览:890 |