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

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 <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;
}

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

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

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

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

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

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

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

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

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

  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 OJ 数据结构实验之链表六:有序链表的建立

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

  9. SDUT OJ 数据结构实验之链表九:双向链表

    数据结构实验之链表九:双向链表 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...

随机推荐

  1. OSGI 模块化

    推荐教程:https://course.tianmaying.com/osgi-toturial+osgi-concept#15

  2. jqurey datatable tableTools 自定义button元素 以及按钮定义事件

    版本 1.10.4 "dom": 'T<"clear">lfrtip', "tableTools": { //"sSw ...

  3. Leetcode:Regular Expression Matching分析和实现

    题目大意是要求我们实现一个简单的正则表达式全匹配判断.其中正则表达式中只包含一般字符,以及全匹配字符.和变长字符*.其中.可以匹配一个字符,而*与前一个字符相关联,x*可以被看作任意多个x(0到正无穷 ...

  4. git配置多用户多平台

    在Git使用中经常会碰到多用户问题,例如:你在公司里有一个git账户,在github上有一个账户,并且你想在一台电脑上同时对这两个git账户进行操作,此时就需要进行git多用户配置. 首先配置不同的S ...

  5. 【HDU4303】Hourai Jeweled

    题意 有一棵n个结点的树,每个结点都有一个值,没一条边都有一个颜色.如果某条路径上,相邻的边颜色不同,那么把这路径上所有的点的值加起来. 输出所有符合条件的路径上值的和. n<=300000. ...

  6. maven安装错误履历

    1\:maven cannot find entry:"/src/main/java" 先删除source下的文件夹 再新建文件夹

  7. 10.IN 操作符

    IN 操作符 IN 操作符允许我们在 WHERE 子句中规定多个值. SQL IN 语法 SELECT column_name(s) FROM table_name WHERE column_name ...

  8. PHP加密与解密

    password_hash ( string $password , integer $algo [, array $options ] ) 加密,生成60位得字符串 $algo:一个用来在散列密码时 ...

  9. SQLite在php中的接口

    sqlite是一种比较轻型的嵌入式数据库,它与 SQL 之间的不同,为什么需要它,以及它的应用程序数据库处理方式.SQLite是一个软件库,实现了自给自足的.无服务器的.零配置的.事务性的 SQL 数 ...

  10. c# 常规验证基类

    using System;using System.Collections.Generic;using System.Linq;using System.Text.RegularExpressions ...