博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
数据结构实验之链表四:有序链表的归并
阅读量:4308 次
发布时间:2019-06-06

本文共 1674 字,大约阅读时间需要 5 分钟。


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

 

Time Limit: 1000MS Memory limit: 65536K

题目描述

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

输入

第一行输入M与N的值;
 
第二行依次输入M个有序的整数;
第三行依次输入N个有序的整数。

输出

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

示例输入

6 51 23 26 45 66 9914 21 28 50 100

示例输出

1 14 21 23 26 28 45 50 66 99 100

提示

不得使用数组!

来源

 

示例程序

 
#include 
#include
struct node{ int data; struct node *next;}*head1,*head2;struct node *creat(int n){ struct node *head,*tail,*p; head=(struct node *)malloc(sizeof(struct node)); head->next=NULL; p=head; while(n--) { tail=(struct node *)malloc(sizeof(struct node)); scanf("%d",&tail->data); tail->next=p->next; p->next=tail; p=tail; } return(head);};struct node * merge(struct node *head1,struct node *head2){ struct node *p1,*p2,*tail; p1=head1->next; p2=head2->next; tail=head1; free(head2); while(p1 && p2) if(p1->data
data) { tail->next=p1; tail=p1; p1=p1->next; } else { tail->next=p2; tail=p2; p2=p2->next; } if(p1) tail->next=p1; else tail->next=p2; return (head1);}int main(){ int n,m; struct node *head1,*head2,*p; scanf("%d%d",&n,&m); head1=creat(n); head2=creat(m); p=merge(head1,head2); while(p->next !=NULL) { if(p->next->next!=NULL) printf("%d ",p->next->data); else printf("%d\n",p->next->data); p=p->next; } return 0;}

 

转载于:https://www.cnblogs.com/Misty5/articles/4108909.html

你可能感兴趣的文章
java String 的比较
查看>>
将String数字字符转为整型
查看>>
【转】 Java中equals和==的区别
查看>>
idea导入maven项目时需要注意
查看>>
nginx部署前端项目的一些配置【刚入门】
查看>>
java 日期格式化 将String日期重新格式化成String型【转】
查看>>
Linux下python安装升级详细步骤 | Python2 升级 Python3
查看>>
阿里云CentOS安装图形化界面
查看>>
SpringBoot nohup启动
查看>>
PHP pclzip.php 解压中文乱码
查看>>
Jenkins安装 maven插件
查看>>
数学好玩 沛沛猜想
查看>>
银联号
查看>>
银行开发平台
查看>>
Linux网络配置
查看>>
Linux开机、重启、和用户登录注销
查看>>
Linux运行级别
查看>>
MySQL逻辑架构简介
查看>>
Linux帮助指令
查看>>
vi和vim编辑器
查看>>