链表合并
时间:2010-08-31 来源:chengbin_liu
/* ************************************************************************
* Filename: unionLink.c
* Description:
* Version: 1.0
* Created: 08/31/2010 08:06:57 PM
* Revision: none
* Compiler: gcc
* Author: chengbin_liu,
* Company:
* ************************************************************************/ #include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
typedef struct node
{
int data;
struct node *next;
}node;
//==============================================================================
node *create()
{
int i=0;
node *head,*p,*q;
int x=0;
head=(node *)malloc(sizeof(node));
while(1)
{
printf("input the data:");
scanf("%d",&x);
if(x==0)
break;
p=(node *)malloc(sizeof(node));
p->data=x;
if(++i==1)
{
head->next=p;
}
else
{
q->next=p;
}
q=p;
}
q->next=NULL;
return head;
}
//=================================================================================
void print(node *head)
{
node *p;
int index=0;
if(head->next==NULL)
return;
p=head->next;
while(p!=NULL)
{
printf("the %d node is %d\n",++index,p->data);
p=p->next;
}
}
//==================================================================================
node *unionLink(node *head1, node *head2)
{
node *head=NULL;
if(head1==NULL)
{
return head2;
}
if(head2==NULL)
{
return head1;
}
if(head1->data < head2->data)
{
head=head1;
head->next=unionLink(head1->next,head2);
}
else
{
head=head2;
head->next=unionLink(head1,head2->next);
}
return head;
}
//===================================================================================
int main()
{
node *head1=create();
node *head2=create();
node *head=unionLink(head1,head2);
print(head);
return 0;
}
* Filename: unionLink.c
* Description:
* Version: 1.0
* Created: 08/31/2010 08:06:57 PM
* Revision: none
* Compiler: gcc
* Author: chengbin_liu,
* Company:
* ************************************************************************/ #include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
typedef struct node
{
int data;
struct node *next;
}node;
//==============================================================================
node *create()
{
int i=0;
node *head,*p,*q;
int x=0;
head=(node *)malloc(sizeof(node));
while(1)
{
printf("input the data:");
scanf("%d",&x);
if(x==0)
break;
p=(node *)malloc(sizeof(node));
p->data=x;
if(++i==1)
{
head->next=p;
}
else
{
q->next=p;
}
q=p;
}
q->next=NULL;
return head;
}
//=================================================================================
void print(node *head)
{
node *p;
int index=0;
if(head->next==NULL)
return;
p=head->next;
while(p!=NULL)
{
printf("the %d node is %d\n",++index,p->data);
p=p->next;
}
}
//==================================================================================
node *unionLink(node *head1, node *head2)
{
node *head=NULL;
if(head1==NULL)
{
return head2;
}
if(head2==NULL)
{
return head1;
}
if(head1->data < head2->data)
{
head=head1;
head->next=unionLink(head1->next,head2);
}
else
{
head=head2;
head->next=unionLink(head1,head2->next);
}
return head;
}
//===================================================================================
int main()
{
node *head1=create();
node *head2=create();
node *head=unionLink(head1,head2);
print(head);
return 0;
}
相关阅读 更多 +