顺序表应用5:有序顺序表归并

Time Limit: 100 ms Memory Limit: 880 KiB

Problem Description

已知顺序表A与B是两个有序的顺序表,其中存放的数据元素皆为普通整型,将A与B表归并为C表,要求C表包含了A、B表里所有元素,并且C表仍然保持有序。

Input

输入分为三行:
第一行输入m、n(1<=m,n<=10000)的值,即为表A、B的元素个数;
第二行输入m个有序的整数,即为表A的每一个元素;
第三行输入n个有序的整数,即为表B的每一个元素;

Output

输出为一行,即将表A、B合并为表C后,依次输出表C所存放的元素。

Sample Input

5 3
1 3 5 6 9
2 4 10

Sample Output

1 2 3 4 5 6 9 10

用链表和数组实现都一个原理;;

#include <stdio.h>
#include <stdlib.h> struct node
{
int data;
struct node *next;
}; int main()
{
struct node *head, *tail, *p, *q, *head2;
head = (struct node *)malloc(sizeof(struct node));
head->next = NULL;
head2 = (struct node *)malloc(sizeof(struct node));
head2->next = NULL;
int i, m, n;
scanf("%d %d",&m,&n); tail = head;
for(i=0; i<m; i++){
p = (struct node *)malloc(sizeof(struct node));
scanf("%d",&p->data);
p->next = NULL;
tail->next = p;
tail = p;
} tail = head2;
for(i=0; i<n; i++){
p = (struct node *)malloc(sizeof(struct node));
scanf("%d",&p->data);
p->next = NULL;
tail->next = p;
tail = p;
} p = head->next;
q = head2->next;
tail = head;
while(p&&q){
if(p->data<q->data){
tail->next = p;
tail = p;
p = p->next;
}
else{
tail->next = q;
tail = q;
q = q->next;
}
}
while(p){
tail->next = p;
tail = p;
p = p->next;
}
while(q){
tail->next = q;
tail = q;
q = q->next;
} p = head->next;
while(p->next){
printf("%d ",p->data);
p = p->next;
} printf("%d\n",p->data); return 0;
}

最新文章

  1. 基于 fuzz 技术验证移动端 app 的健壮性
  2. replicate复制函数
  3. Jqgrid 数据格式化配置
  4. java中IO流操作的标准异常类
  5. javascript针对DOM的应用
  6. uva 11324 The Largest Clique(强连通分量缩点+DAG动态规划)
  7. HDU 5573 Binary Tree 构造
  8. 从根源上解析 Java volatile 关键字的实现
  9. Git 忽略已经提交的文件
  10. 【开源java游戏框架libgdx专题】-05-模块描述与上下文
  11. Sql Server插入随机数
  12. Python3.4 + Django1.7.7 搭建简单的表单并提交
  13. luoguP4231_三步必杀_差分
  14. SpringMVC 接受请求参数、作用域传值
  15. 怎么添加在安装好的nvidia-docker上面根据Dockerfile构建自己所需要的运行环境
  16. react - web + webpack4 从0构建
  17. flask No such command &quot;init-db&quot;.
  18. ntp测试
  19. EF6 Code First &amp; Auto Migration on Appharbor
  20. [扩展推荐] Laravel 中利用 GeoIP 获取用户地理位置信息

热门文章

  1. python发送包含html、图片、附件和链接的邮件
  2. CXF动态客户端如何优化JaxWsDynamicClientFactory.createClient -- 慢
  3. 斯坦福CS229机器学习课程笔记 Part1:线性回归 Linear Regression
  4. java基础篇之HashMap
  5. JSSDK用法
  6. 10-python中的requests应用
  7. HDU 5293 Tree chain problem
  8. Django rest_framework----序列化组件
  9. 【linux命令】setterm控制终端属性命令(中英文)
  10. 解析json的方法