数据结构实验之链表四:有序链表的归并
来源:互联网 发布:软件体系结构评估报告 编辑:程序博客网 时间:2024/05/18 14:15
题目描述
分别输入两个有序的整数序列(分别包含M和N个数据),建立两个有序的单链表,将这两个有序单链表合并成为一个大的有序单链表,并依次输出合并后的单链表数据。
输入
第一行输入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
建立两个链表
释放一个表头,用另一个作为新表头
源代码C#
<span style="font-size:14px;">#include <stdio.h>#include <stdlib.h>struct node{ int data; struct node *next;};struct node *create(int n){ struct node*head,*p,*tail; int i; head=(struct node*)malloc(sizeof(struct node)); head->next=NULL; tail=head; for(i=0;i<n;i++) { p=(struct node*)malloc(sizeof(struct node)); p->next=NULL; scanf("%d",&p->data); tail->next=p; tail=p; } return (head);};void print(struct node *h){ struct node *p=h->next; while(p!=NULL) { if(p->next==NULL) printf("%d",p->data); else printf("%d ",p->data); p=p->next; }}struct node *pai(struct node *h1,struct node *h2){ struct node *p=h1->next; struct node *q=h2->next; struct node *tail; h1->next=NULL; tail=h1; free(h2); while(p&&q) { if(p->data<q->data) { tail->next=p; tail=p; p=p->next; tail->next=NULL; } else { tail->next=q; tail=q; q=q->next; tail->next=NULL; } } while(p) { tail->next=p; tail=p; p=p->next; tail->next=NULL; } while(q) { tail->next=q; tail=q; q=q->next; tail->next=NULL; } return (h1);}int main(){ int m,n; char a[10]; scanf("%d %d",&m,&n); struct node *p,*q,*w; p=create(m); gets(a); q=create(n); w=pai(p,q); print(w); return 0;}</span>
0 0
- 2119 数据结构实验之链表四:有序链表的归并
- 数据结构实验之链表四:有序链表的归并
- 数据结构实验之链表四:有序链表的归并
- 数据结构实验之链表四:有序链表的归并
- 数据结构实验之链表四:有序链表的归并
- 数据结构实验之链表四:有序链表的归并
- 数据结构实验之链表四:有序链表的归并
- 【2119】数据结构实验之链表四:有序链表的归并
- 数据结构实验之链表四:有序链表的归并
- 数据结构实验之链表四:有序链表的归并
- 数据结构实验之链表四:有序链表的归并
- 数据结构实验之链表四:有序链表的归并
- 数据结构实验之链表四:有序链表的归并
- 数据结构实验之链表四:有序链表的归并
- 数据结构实验之链表四:有序链表的归并
- 数据结构实验之链表四:有序链表的归并
- 数据结构实验之链表四:有序链表的归并
- 数据结构实验之链表四:有序链表的归并
- HDU-3709-Balanced Number(数位DP+记忆化DFS)
- MySQL5.7.14的免安装配置
- git介绍及使用教程
- Dataset.Tables[0].Select从一个Dataset中提取符合要求的数据
- arm 上电过程以及uboot
- 数据结构实验之链表四:有序链表的归并
- 2021 发工资咯:)
- 快让人崩溃的error C2236: 意外的“class”“DiagramWindow”。是否忘记了“;”?
- go database/sql 源码分析 -题外篇
- 面向对象的三大特点
- cocos lua遇到的一些问题
- 初中oj1338 泽泽在埃及
- spark 的HA模式
- rsyslog 走tcp通讯配置