数据结构实验之链表四:有序链表的归并

Time Limit: 1000 ms Memory Limit: 65536 KiB

Problem Description

分别输入两个有序的整数序列(分别包含M和N个数据),建立两个有序的单链表,将这两个有序单链表合并成为一个大的有序单链表,并依次输出合并后的单链表数据。

Input

第一行输入M与N的值;

第二行依次输入M个有序的整数;

第三行依次输入N个有序的整数。

Output

输出合并后的单链表所包含的M+N个有序的整数。

Sample Input

6 5

1 23 26 45 66 99

14 21 28 50 100

Sample Output

1 14 21 23 26 28 45 50 66 99 100

Hint

不得使用数组!

考链表的插入操作,比较之后将比较小的节点插入到新的节点后面,最后将剩余节点插入。

#include <stdio.h>
#include <string.h>
#include <stdlib.h> typedef struct node
{
int data;
struct node *next;
}link; link *newlink()
{
link *t;
t = (link*)malloc(sizeof(link));
t->next = NULL;
return t;
} link *create(int n)
{
link *head,*p,*q;
int i;
head = newlink();
p = head;
for(i=0;i<n;i++)
{
q = newlink();
scanf("%d",&q->data);
q->next = NULL;
p->next = q;
p = q;
}
return head;
} link *Guibing(link *head1,link *head2)
{
link *r,*p,*q;
p = head1->next;
q = head2->next;
head1->next = NULL;
r = head1;
while(p&&q)
{
if(p->data<q->data)
{
r->next = p;
r = r->next;
p = p->next;
}
else
{
r->next = q;
r = r->next;
q = q->next;
}
}
if(p)
r->next = p; if(q)
r->next = q;
return head1;
} void show(link *head)
{
link *p;
p = head->next;
while(p)
{
if(p->next==NULL)
printf("%d\n",p->data);
else
printf("%d ",p->data);
p = p->next;
}
} int main()
{
link *head1;
link *head2;
int m,n;
scanf("%d%d",&m,&n);
head1 = create(m);
head2 = create(n);
head1 = Guibing(head1,head2);
show(head1);
return 0;
}

SDUT-2119_数据结构实验之链表四:有序链表的归并的更多相关文章

  1. SDUT 3401 数据结构实验之排序四:寻找大富翁.!

    数据结构实验之排序四:寻找大富翁 Time Limit: 150MS Memory Limit: 512KB Submit Statistic Problem Description 2015胡润全球 ...

  2. SDUT OJ 数据结构实验之图论四:迷宫探索

    数据结构实验之图论四:迷宫探索 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...

  3. SDUT OJ 数据结构实验之二叉树四:(先序中序)还原二叉树

    数据结构实验之二叉树四:(先序中序)还原二叉树 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem ...

  4. SDUT OJ 数据结构实验之排序四:寻找大富翁

    数据结构实验之排序四:寻找大富翁 Time Limit: 200 ms Memory Limit: 512 KiB Submit Statistic Discuss Problem Descripti ...

  5. SDUT 3376 数据结构实验之查找四:二分查找

    数据结构实验之查找四:二分查找 Time Limit: 20MS Memory Limit: 65536KB Submit Statistic Problem Description 在一个给定的无重 ...

  6. SDUT 3361 数据结构实验之图论四:迷宫探索

    数据结构实验之图论四:迷宫探索 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 有一个地下迷 ...

  7. SDUT 3343 数据结构实验之二叉树四:还原二叉树

    数据结构实验之二叉树四:还原二叉树 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 给定一棵 ...

  8. SDUT-3376_数据结构实验之查找四:二分查找

    数据结构实验之查找四:二分查找 Time Limit: 30 ms Memory Limit: 65536 KiB Problem Description 在一个给定的无重复元素的递增序列里,查找与给 ...

  9. SDUT-3343_数据结构实验之二叉树四:(先序中序)还原二叉树

    数据结构实验之二叉树四:(先序中序)还原二叉树 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 给定一棵二叉树的先序遍历 ...

  10. SDUT OJ 数据结构实验之链表四:有序链表的归并

    数据结构实验之链表四:有序链表的归并 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Desc ...

随机推荐

  1. 前端(jQuery)(6)-- jQuery的扩展与noConflict

    1.jQuery的扩展 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...

  2. fidder下载及使用

    fidder 下载地址:https://www.telerik.com/download/fiddler 安装采用默认方式安装即可.

  3. ACMer之歌

    <<死了都要编>> 死了都要编 不动态规划不痛快 算法多深只有这样 才足够表白 死了都要编 不A星算法不痛快 宇宙毁灭星还在 把每天当成是比赛来编程 一分一秒都编到汗水掉下来 ...

  4. mysql sum() 求和函数的用法

    查询在record表中 name=? 的 money 加起来的值使用聚和函数 sum() 求和select sum(money) from record t where t.name = ?另外:co ...

  5. Django 创建web项目之HelloWorld

    Django.Flask.Tornado并称为python WEB三大框架.Diango是一个开源的web应用框架,具有开发速度快的特点.同时因为过度封装,具有性能低的特点. 创建Django项目,启 ...

  6. unity限帧的正确姿势

    首先 unity上面要做一下手脚 打开后如下 接着.... 在Inspector面板 把V Sync Count 设置为不限制(Don`t Sync)(我们用脚本限制,不然unity自己控制不了它自己 ...

  7. vue-eslint配置文件

    做项目的时候,我把eslint设置为了false,可想而知提交会产生的冲突 让我一个一个解决肯定不可能的,eslint的rule很多 在vue的配置文件.eslintrc.js中配置以下选项 这样只需 ...

  8. 【react】react-bookManager

    作者可能是本意想要做一个图书管理系统,不过添加书籍的时候报错,所以简单的页面我们简单的看看 先上github地址:https://github.com/hesisi/react-bookManager ...

  9. 安装tengine及淘宝会话保持模块

    安装tengine及淘宝会话保持模块 下载http://tengine.taobao.org/ 解压tar -zxvf tengine-2.3.0.tar.gz 安装GCC: yum -y insta ...

  10. Sass @at-root (1)

    在SassConf大会上,给我们传递了Sass3.3的新特性.这些新特性有很多意义,特别是@at-root指令,这让你的代码会得更佳清洁. 今天我们主要一起来了解Sass中的@at-root特性的使用 ...