OJ--链表的归并
2016-04-03 00:00
218 查看
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *creat(int n)
{int i;
struct node *head,*p,*tail;
head=(struct node *)malloc(sizeof(struct node));
head->next=NULL;
tail=head;
for(i=1;i<=n;i++)
{
p=(struct node *)malloc(sizeof(struct node));
p->next=NULL;
scanf("%d",&p->data);
tail->next=p;
tail=p;
}
return head;
};
struct node * merge(struct node *head1,struct node *head2)
{struct node *p1,*p2,*tail;
p1=head1->next;
p2=head2->next;
free(head2);
tail=head1;
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;
if(p2) tail->next=p2;
return head1;
};
int main()
{struct node *p,*head1,*head2;
int n,m;
scanf("%d %d",&n,&m);
head1=creat(n);
head2=creat(m);
merge(head1,head2);
p=head1;
while(p->next!=NULL)
{
printf("%d ",p->next->data);
p=p->next;
}
return 0;
}
相关文章推荐
- android ViewPager 轮播图的实现
- java和mysql存储过程
- zlib.h:no such file or directory
- CentOS 7 更换 163网易yum源
- linux安装GCC详解
- CentOS 7安装配置Samba
- “Linux内核分析”实验报告(六)分析Linux内核创建一个新进程的过程
- VC6如何让Dialog响应WM_CHAR消息
- eclipse导入tomcat源码
- python魔法 交换两个数字
- ZendFramework流程
- python 中斐波那契数列的那些事
- python快速排序
- Android 心得
- 【转载】iostat和iowait详细解说
- 【转发】在 Windows 上使用 Chrome 运行 Android 应用
- 【转发】如何使用Android Studio开发/调试Android源码
- 创建动态二维数组
- HTTP状态码
- Head First C 第九章 进程与系统调用 exec函数