Chinaunix首页 | 论坛 | 博客
  • 博客访问: 347180
  • 博文数量: 78
  • 博客积分: 3380
  • 博客等级: 中校
  • 技术积分: 857
  • 用 户 组: 普通用户
  • 注册时间: 2010-06-16 19:39
文章分类

全部博文(78)

文章存档

2011年(31)

2010年(47)

分类: C/C++

2010-08-31 21:34:00

/* ************************************************************************
 *       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;
}
 
阅读(489) | 评论(0) | 转发(0) |
0

上一篇:循环链表

下一篇:队列

给主人留下些什么吧!~~