Chinaunix首页 | 论坛 | 博客
  • 博客访问: 26748
  • 博文数量: 41
  • 博客积分: 185
  • 博客等级: 入伍新兵
  • 技术积分: 260
  • 用 户 组: 普通用户
  • 注册时间: 2012-12-20 13:48
文章分类

全部博文(41)

文章存档

2013年(20)

2012年(21)

我的朋友
最近访客

分类: C/C++

2013-01-04 22:00:24


点击(此处)折叠或打开

  1. //合并排序函数1
  2. #include"stdlib.h"
  3. typedef struct code
  4. {
  5.     int data;
  6.     struct code *next;
  7. }Code;

  8. Code *Insert(Code *head1,Code *head2)
  9. {
  10.     Code *head,*cur;
  11.     if(head1==NULL)
  12.         return head2;
  13.     if(head2==NULL)
  14.         return head1;
  15.     if(head1->data>head2->data)//找出最小的data;
  16.     {
  17.         head=head2;
  18.         head2=head2->next;
  19.     }
  20.     else
  21.     {
  22.         head=head1;
  23.         head1=head1->next;
  24.     }
  25.     for(cur=head;head1!=NULL&&head2!=NULL;)
  26.     {
  27.         if(head1->data<head2->data)
  28.         {
  29.             cur->next=head1;
  30.             cur=head1;
  31.             head1=head1->next;
  32.         }
  33.         else
  34.         {
  35.             cur->next=head2;
  36.             cur=head2;
  37.             head2=head2->next;
  38.         }
  39.     }
  40.     cur->next=(head1==NULL?head2:head1);//head1和head2至少有一个为空时,剩下的那个链表直接放在cur->next.
  41.     return head;
  42. }


点击(此处)折叠或打开

  1. //合并排序函数2
  2. #include"stdlib.h"
  3. typedef struct code
  4. {
  5.     int data;
  6.     struct code *next;
  7. }Code;

  8. Code *Insert(Code *ahead,Code *bhead)
  9. {
  10.     Code *pa1,*pa2,*pb1,*pb2;
  11.     pa1=pa2=ahead;
  12.     pb1=pb2=bhead;
  13.     do //让b链表中的结点依次和a链表的结点比较并插入;
  14.     {
  15.         while((pb1->data>pa1->data)&&(pa1->next!=NULL))
  16.         {
  17.             pa2=pa1;
  18.             pa1=pa1->next;
  19.         }
  20.         if(pb1->data<=pa1->data)
  21.         {
  22.             if(pa1==ahead)
  23.                 ahead=pb1;
  24.             else
  25.                 pa2->next=pb1;
  26.             pb1=pb1->next;
  27.             pb2->next=pa1;
  28.             pa2=pb2;
  29.             pb2=pb1;
  30.         }
  31.     }while(pa1->next!=NULL);
  32.     if(pa1==NULL)
  33.         pa1->next=pb1;
  34.     return ahead;
  35. }

阅读(252) | 评论(0) | 转发(0) |
0

上一篇:链表排序

下一篇:直接插入排序原理

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