C语言中数据结构之链表归并排序实例代码

所属分类: 软件编程 / C 语言 阅读数: 66
收藏 0 赞 0 分享

C语言中数据结构之链表归并排序实例代码

问题

       设有两个无头结点的单链表,头指针分别为ha,hb,链中有数据域data,链域next,两链表的数据都按递增排序存放,现要求将hb表归到ha表中,且归并后ha仍递增序,归并中ha表中已有的数据若hb中也有,则hb中的数据不归并到ha中,hb的链表在算法中不允许破坏。

源程序

#include <stdio.h> 
#include<stdlib.h> 
#define N1 6 /*链表La的长度*/  
#define N2 6 /*链表Lb的长度*/ 
struct listnode  
{ 
 int data; 
 struct listnode *next; 
}; 
void createlist(struct listnode * *,int);  
void listinsert(struct listnode * *,struct listnode * *); 
void readlist(struct listnode *); 
int main() 
{ 
  
 struct listnode *ha=NULL,*hb=NULL; 
 printf("请按照升序序列输入以下数字以建立链表La\n"); 
 printf("Please Input %d numbers:",N1); 
 createlist(&ha,N1); 
 printf("请按照升序序列输入以下数字以建立链表Lb\n"); 
 printf("Please Input %d numbers:",N2); 
 createlist(&hb,N2); 
 listinsert(&ha,&hb); 
 readlist(ha); 
 printf("\n");  
} 
 
 
void createlist(struct listnode * *p,int n) 
{ /*尾插法建立链表*/ 
 struct listnode *t,*q; 
 int i; 
 t=(struct listnode *)malloc(sizeof(struct listnode)); 
 scanf("%d",&t->data); 
 *p=t; 
 q=t;  
 for(i=n-1;i>0;i--) 
    { 
 t=(struct listnode *)malloc(sizeof(struct listnode)); 
 scanf("%d",&t->data); 
 q->next=t; 
 q=t; 
  } 
 q->next=NULL; 
} 
 
void listinsert(struct listnode * *a,struct listnode * *b)  
{ /*将两个链表按递增序列排序*/ 
 struct listnode *pa,*pb,*other,*la,*pre; 
 la=(struct listnode *)malloc(sizeof(struct listnode)); 
 la->next=*a; 
 pa=*a;    
 pb=*b; 
 pre=la;   
 while(pa&&pb) 
  { 
 if(pa->data<pb->data) 
   {   
  pre=pre->next; 
  pa=pa->next; 
  } 
 else if (pa->data>pb->data) 
 { 
  other=(struct listnode *)malloc(sizeof(struct listnode)); 
  other->data=pb->data; 
  other->next=pa; 
  pre->next=other; 
  pre=other; 
  pb=pb->next; 
   } 
  
 else 
 { 
  pre=pre->next; 
  pa=pa->next; 
  pb=pb->next; 
  } 
 } 
  
 if(!pa) 
 { 
 while(pb) 
 { 
  other=(struct listnode *)malloc(sizeof(struct listnode)); 
  other->data=pb->data;   
        pre->next=other; 
  pre=pre->next; 
  pb=pb->next; 
 } 
 pre->next=NULL; 
 } 
 *a=la->next; 
 free(la); 
} 
 
void readlist(struct listnode *p) 
{ /*遍历链表并输出最终结果*/ 
 struct listnode *q; 
 q=p; 
 printf("链表的排序结果为:\n"); 
 while(q!=NULL) 
   { 
 printf("%3d",q->data); 
 q=q->next; 
 } 
 printf("\n"); 
} 

运行结果

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

更多精彩内容其他人还在看

C++中四种对象生存期和作用域以及static的用法总结分析

以下是对C++中四种对象生存期和作用域以及static的用法进行了详细的介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C++嵌套类与局部类详细解析

从作用域的角度看,嵌套类被隐藏在外围类之中,该类名只能在外围类中使用。如果在外围类之外的作用域使用该类名时,需要加名字限定
收藏 0 赞 0 分享

C++空类详解

以下是对C++中的空类进行了详细的介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C++之友元:友元函数和友元类详解

友元是一种允许非类成员函数访问类的非公有成员的一种机制。可以把一个函数指定为类的友元,也可以把整个类指定为另一个类的友元
收藏 0 赞 0 分享

C++中返回指向函数的指针示例

int (*ff(int)) (int *,int);表示:ff(int)是一个函数,带有一个int型的形参,该函数返回int (*) (int *,int),它是一个指向函数的指针,所指向的函数返回int型并带有两个分别是Int*和int型的形参
收藏 0 赞 0 分享

C数据结构之单链表详细示例分析

以下是对C语言中的单链表进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C数据结构之双链表详细示例分析

以下是对c语言中的双链表进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

浅析如何在c语言中调用Linux脚本

如何在c语言中调用Linux脚本呢?下面小编就为大家详细的介绍一下吧!需要的朋友可以过来参考下
收藏 0 赞 0 分享

深入解析unsigned int 和 int

以下是对unsigned int和int进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

浅谈C++中的string 类型占几个字节

本篇文章小编并不是为大家讲解string类型的用法,而是讲解我个人比较好奇的问题,就是string 类型占几个字节
收藏 0 赞 0 分享
查看更多