/* ************************************************************************
* Filename: unionLink.c
* Description:
* Version: 1.0
* Created: 08/31/2010 08:06:57 PM
* Revision: none
* Compiler: gcc
* Author: chengbin_liu,
* Company:
* ************************************************************************/
#include
#include
#include
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;
}
阅读(520) | 评论(0) | 转发(0) |