数据结构实验之链表四:有序链表的归并(SDUT 2119)
2024-09-05 08:09:19
#include <bits/stdc++.h>
using namespace std;
struct node
{
int data;
struct node *next;
};
struct node *creat(int n)
{
struct node *head,*tail,*p;
head=(struct node*)malloc(sizeof(struct node));
head->next=NULL;
tail=head;
int i;
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;
}
int main()
{
struct node *head,*head1,*head2,*p,*p1,*p2,*tail;
head=(struct node*)malloc(sizeof(struct node));
head->next=NULL;
tail=head;
int n,m;
scanf("%d %d",&n,&m);
head1=creat(n);
head2=creat(m);
p1=head1->next;
p2=head2->next;
free(head1);
free(head2);
while(p1&&p2)
{
if(p1->data<p2->data)
{
tail->next=p1;
tail=p1;
p1=p1->next;
tail->next=NULL;
}
else
{
tail->next=p2;
tail=p2;
p2=p2->next;
tail->next=NULL;
}
}
if(p1) tail->next=p1;
else tail->next=p2;
for(p=head->next;p!=NULL;p=p->next)
{
if(p==head->next) printf("%d",p->data);
else printf(" %d",p->data);
}
return 0;
}
最新文章
- cf Round 613
- Python学习笔记(三)——类型与变量
- 理解innodb buffer pool
- fork Bomb
- PostgreSQL Replication之第十二章 与Postgres-XC一起工作(3)
- Edit Distance编辑距离(NM tag)- sam/bam格式解读进阶
- Linux常用文件管理命令
- Sciter/HTMLayout内存占用评测
- 整理:Google jQuery 引用地址大全和方法(转)
- 转:MongoDB调查总结
- SQL从入门到基础 - 05 数据分组、Having语句
- HDU 3478 Play with Chain (Splay树)
- 关于sqlserver还原不了数据库的原因
- China-global view
- 键盘控制div移动并且解决停顿问题(原生js)
- Oracle 11g常用管理命令(用户、表空间、权限)
- Redis之数据类型大全
- TeamWork#3,Week5,Scrum Meeting 11.13
- Protobuf底层存储原理
- 【Oracle123】向OracleDB导入*.csv文件数据