C语言程序设计教程(第三版)课后习题11.8
来源:互联网 发布:第一军团永远忠诚知乎 编辑:程序博客网 时间:2024/05/16 18:48
题目描述
已有a、b两个链表,每个链表中的结点包括学号、成绩。要求把两个链表合并,按学号升序排列。 使用结构体
输入
第一行,a、b两个链表元素的数量N、M,用空格隔开。接下来N行是a的数据然后M行是b的数据每行数据由学号和成绩两部分组成
输出
按照学号升序排列的数据
样例输入
2 35 1006 893 824 952 10
样例输出
2 103 824 955 1006 89
【代码】
#include <stdio.h>#include <malloc.h>struct stu{ int num; int score; struct stu *next;};struct stu *create(int n) //创建链表 { struct stu *head=NULL,*tail,*p; int i; for(i=1;i<=n;i++) { p=malloc(sizeof(struct stu)); scanf("%d%d",&p->num,&p->score); if(head==NULL) head=p; else tail->next=p; tail=p; } tail->next=NULL; return head;}struct stu *heBin(struct stu *h1,struct stu *h2) //合并链表 { struct stu *p=h1; while(p->next) p=p->next; p->next=h2; return h1;}struct stu *sort(struct stu *head) //链表排序 { struct stu *end,*p; int t; end=head; while(end) end=end->next; while(head->next!=end) { p=head; while(p->next!=end) { if(p->num>p->next->num) { t=p->num; p->num=p->next->num; p->next->num=t; t=p->score; p->score=p->next->score; p->next->score=t; } p=p->next; } end=p; } return head; } void print(struct stu *head) //输出链表 { struct stu *p; while(head) { p=head; head=head->next; printf("%d %d\n",p->num,p->score); free(p); }}main(){ struct stu *h1,*h2,*head; int n,m; scanf("%d%d",&n,&m); h1=create(n); h2=create(m); head=heBin(h1,h2); head=sort(head); print(head); }
阅读全文
0 0
- 1052:C语言程序设计教程(第三版)课后习题11.8
- C语言程序设计教程(第三版)课后习题11.8
- 1052: C语言程序设计教程(第三版)课后习题11.5
- 1052: C语言程序设计教程(第三版)课后习题11.5
- 1001: C语言程序设计教程(第三版)课后习题1.5
- 1003: C语言程序设计教程(第三版)课后习题3.7
- 1005: C语言程序设计教程(第三版)课后习题4.9
- 1007: C语言程序设计教程(第三版)课后习题5.5
- 1009: C语言程序设计教程(第三版)课后习题5.7
- 1010: C语言程序设计教程(第三版)课后习题5.8
- 1051: C语言程序设计教程(第三版)课后习题11.5
- C语言程序设计教程(第三版)课后习题5.7
- C语言程序设计教程(第三版)课后习题10.4
- 1002: C语言程序设计教程(第三版)课后习题1.6
- 1003: C语言程序设计教程(第三版)课后习题3.7
- 1004: C语言程序设计教程(第三版)课后习题4.8
- 1005: C语言程序设计教程(第三版)课后习题4.9
- 1006: C语言程序设计教程(第三版)课后习题5.4
- jenkins体验-maven安装
- 牛顿迭代法求平方根、立方根
- Spring全家桶(二)Bean之间的关系、自动装配、作用域和使用外部文件
- 深度学习之物体检测——YOLO(一)_介绍
- 需求缺陷表缺点及优化建议
- C语言程序设计教程(第三版)课后习题11.8
- log4j.properties配置详解与实例
- 从新闻数据组中提取TF-IDF特征
- 数组Array和集合的区别:
- hadoop2.7.2学习笔记15-HDFS user guide
- 单链表的合并递增输出
- WebSocket与消息推送
- Linux常用命令——网络命令
- ionic3.0.0 跨平台项目创建流程