数据结构实验之链表四:有序链表的归并
来源:互联网 发布:网络角色去衣图 编辑:程序博客网 时间:2024/04/28 14:54
Problem Description
分别输入两个有序的整数序列(分别包含M和N个数据),建立两个有序的单链表,将这两个有序单链表合并成为一个大的有序单链表,并依次输出合并后的单链表数据。
Input
第一行输入M与N的值;
第二行依次输入M个有序的整数;
第三行依次输入N个有序的整数。
Output
输出合并后的单链表所包含的M+N个有序的整数。
Example Input
6 51 23 26 45 66 9914 21 28 50 100
Example Output
1 14 21 23 26 28 45 50 66 99 100
Hint
不得使用数组!
Author
#include <stdio.h>#include <stdlib.h>#include <string.h>#include<bits/stdc++.h>#include <iostream>#include <algorithm>#define maxn 10100using namespace std;typedef struct list{ int data; struct list *next;}sqlist;sqlist *newlist(int n){ sqlist *head; head=(sqlist *)malloc(sizeof(sqlist)); head->next=NULL; scanf("%d", &head->data); n--; sqlist *mail=head; while(n--) { sqlist *p; p=(sqlist *)malloc(sizeof(sqlist)); p->next=NULL; scanf("%d", &p->data); mail->next=p; mail=p; } return head;}sqlist *add(sqlist *head1, sqlist *head2, sqlist *head3){ sqlist *mail=head3; while(head1&&head2) { if(head1->data<head2->data) { mail->next=head1; mail=mail->next; sqlist *p; p=head1; head1=head1->next; p->next=NULL; } else { mail->next=head2; mail=mail->next; sqlist *p; p=head2; head2=head2->next; p->next=NULL; } } if(head1) { while(head1) { mail->next=head1; mail=mail->next; sqlist *p=head1; head1=head1->next; p->next=NULL; } } else if(head2) { while(head2) { mail->next=head2; mail=mail->next; sqlist *p=head2; head2=head2->next; p->next=NULL; } } return head3;}void print(sqlist *head3){ int top=1; while(head3->next) { if(top)top=0; else printf(" "); printf("%d", head3->next->data); head3=head3->next; }}int main(){ int m, n; scanf("%d%d", &m, &n); sqlist *head1, *head2, *head3; head1=newlist(m); head2=newlist(n); head3=(sqlist *)malloc(sizeof(sqlist)); head3->next=NULL; head3=add(head1, head2, head3); print(head3); return 0;}
0 0
- 2119 数据结构实验之链表四:有序链表的归并
- 数据结构实验之链表四:有序链表的归并
- 数据结构实验之链表四:有序链表的归并
- 数据结构实验之链表四:有序链表的归并
- 数据结构实验之链表四:有序链表的归并
- 数据结构实验之链表四:有序链表的归并
- 数据结构实验之链表四:有序链表的归并
- 【2119】数据结构实验之链表四:有序链表的归并
- 数据结构实验之链表四:有序链表的归并
- 数据结构实验之链表四:有序链表的归并
- 数据结构实验之链表四:有序链表的归并
- 数据结构实验之链表四:有序链表的归并
- 数据结构实验之链表四:有序链表的归并
- 数据结构实验之链表四:有序链表的归并
- 数据结构实验之链表四:有序链表的归并
- 数据结构实验之链表四:有序链表的归并
- 数据结构实验之链表四:有序链表的归并
- 数据结构实验之链表四:有序链表的归并
- 简化程序并实现数据连接
- softmax手写数字识别 python代码
- CentOS7.3安装Bumblebee驱动
- 【matlab】GUI的handlevisibility属性
- 欢迎使用CSDN-markdown编辑器
- 数据结构实验之链表四:有序链表的归并
- 简单的模拟——多项式输出
- Resmgr:Cpu Quantum 等待事件
- 花生采摘
- C和C++互相调用 以及 extern "C"的用法
- 根据手机号查询归属地
- android 中singletask启动模式的 activity的刷新
- 字符串的压缩
- springMVC下的转发与重定向