两个无序单链表,排序后合并成一个有序链表

算法思想:用冒泡法,对链表1和2进行排序,对排序后的两个链表,从小到大进行循环,装入链表3中。

#include<stdio.h>
#include<stdlib.h>
struct stud/*定义链表*/
{
int data;
struct stud *next;
};
void pai_xue(struct stud *head1,struct stud *head2,int count1,int count2)/*冒泡排序法*/
{
int i,j,temp=0;
struct stud *p;
for(i=0;i<count1-1;++i)
for(p=head1->next;p->next!=NULL;p=p->next)/*对链表1进行排序*/
{
if(p->data>p->next->data)
{
temp=p->data;
p->data=p->next->data;
p->next->data=temp;
}
}
for(i=0;i<count2-1;++i)
for(p=head2->next;p->next!=NULL;p=p->next)/*对链表2进行排序*/
{
if(p->data>p->next->data)
{
temp=p->data;
p->data=p->next->data;
p->next->data=temp;
}
}
printf("\n链表1排完序后\n");/*输出链表1和2*/
p=head1->next;
while(p)
{
printf("%d",p->data);
p=p->next;
}
printf("\n");
printf("\n链表2排完序后\n");
p=head2->next;
while(p)
{
printf("%d",p->data);
p=p->next;
}
printf("\n");
}
void main()
{
struct stud *head1,*head2,*p,*q,*head3;
int count1=1,count2=1;
head3=(struct stud *)malloc(sizeof(struct stud *));/*定义链表头结点,并分配空间*/
head3->next=NULL;
head1=(struct stud *)malloc(sizeof(struct stud *));
head2=(struct stud *)malloc(sizeof(struct stud *));
p=(struct stud *)malloc(sizeof(struct stud *));
q=(struct stud *)malloc(sizeof(struct stud *));
head1->next=NULL;
head2->next=NULL;
printf("输入一个数据以999结束\n");
scanf("%d",&p->data);
while(p->data!=999)/*链表1输入数据*/
{
count1++;
p->next=head1->next;
head1->next=p;
printf("输入一个数据以999结束\n");
p=(struct stud *)malloc(sizeof(struct stud *));
scanf("%d",&p->data);
}
printf("现在开始给第二个链表输入数据\n");
printf("输入一个数据以999结束\n");
scanf("%d",&q->data);
while(q->data!=999)/*链表2输入数据*/
{
count2++;
q->next=head2->next;
head2->next=q;
printf("输入一个数据以999结束\n");
q=(struct stud *)malloc(sizeof(struct stud *));
scanf("%d",&q->data);
}
pai_xue(head1,head2,count1,count2);
head1=head1->next;
head2=head2->next;
while(head1!=NULL&&head2!=NULL)/*将排序好的链表1和2 的数据导入链表3*/
{
if(head1->data<=head2->data)
{
p=head1->next;
head1->next=head3->next;
head3->next=head1;
head1=p;
}
else
{
q=head2->next;
head2->next=head3->next;
head3->next=head2;
head2=q;
}
}
if(head1!=NULL)/*如果有链表1或2的数据不为空,将剩下的数据导入链表3中*/
{
p=head1;
while(p!=NULL)
{
q=p->next;
p->next=head3->next;
head3->next=p;
p=q;
}
}
if(head2!=NULL)
{
q=head2;
while(q!=NULL)
{
p=q->next;
q->next=head3->next;
head3->next=q;
q=p;
}
}
q=head3->next;/*将链表倒置,原来是由大到小,倒置成由小到大*/
head3->next=NULL;
while(q!=NULL)
{
p=q->next;
q->next=head3->next;
head3->next=q;
q=p;
}
printf("两个链表合并后由小到大为\n");
p=head3->next;
while(p)
{
printf("%d",p->data);
p=p->next;
}
}

两个无序单链表,排序后合并成一个有序链表相关推荐

  1. 将k个有序链表合并成一个有序链表

    将k个有序链表合并成一个有序链表 这里以从小到大排序为例, 时间复杂度为O(nlgk). 特点:利用最小堆来完成k路归并 思路:取每个列表中的第一个元素,并将其放在最小堆中.与每个元素一起,必须跟踪从 ...

  2. 两个有序链表排序C语言,K个有序链表的归并排序(C语言)

    归并算法时间复杂度:O(NlogN) 注意:断链和合并的思想 两个链表的归并: #include #include typedef struct listNode{ int val; struct l ...

  3. 两个有序链表合并成一个有序的单链表

    将这两个有序链表合并成一个有序的单链表 要求结果链表仍使用原来两个链表的存储空间,不另外占用其它的存储空间 表中允许有重复数据 算法描述 (1)定义一个合并后的指针pc指向La表的头结点.由于要求不占 ...

  4. 将两个整形数组排序后合并成一个数组

    将两个整形数组排序后合并成一个数组, 数组排序采用快速排序法,快速法定义了三个参数,(数组首地址*a,要排序数组起始元素下标i,要排序数组结束元素下标j). 它首先选一个数组元素(一般为a[(i+j) ...

  5. LeetCode 之 JavaScript 解答第23题 —— 合并K个有序链表(Merge K Sorted Lists)

    Time:2019/4/10 Title: Merge K Sorted Lists Difficulty: Difficulty Author: 小鹿 题目:Merge K Sorted Lists ...

  6. 合并k个有序链表 python_[LeetCode] 23. Merge k Sorted Lists 合并k个有序链表

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. E ...

  7. 合并k个有序链表 python_Leetcode打卡 | No.23 合并 k 个有序链表

    写在前边: 欢迎和小詹一起定期刷leetcode,每周一和周五更新一题,每一题都吃透,欢迎一题多解,寻找最优解!这个记录帖哪怕只有一个读者,小詹也会坚持刷下去的! PS:从第10开始,代码以图片形式给 ...

  8. [LeetCode] Merge k Sorted Lists 合并k个有序链表

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. E ...

  9. 合并N个有序链表与FQ公平调度

    2018/05/09 下大雨了,于是就想表达一些只有下雨才能表达的东西.夜半酒酣惊觉起,使我流泪忆江南-前天晚上下班带着小小在暴雨中狂奔,非常舒服,其实也算是流言终结者吧.反驳一下几千年来在我国北方通 ...

最新文章

  1. 基于微信的图片放大预览
  2. python中使用 protocol buffer(Protobuf)
  3. Storm 1.1.0 集群安装
  4. antdesignvue upload vue3个人笔记待更新
  5. 耦合层:撮合物联网的理论与实践牵手的“月老”
  6. yum客户端的配置文件的格式
  7. Mat 创建图像的理解
  8. ISA2006的部署和无人职守
  9. linux 下xhprof的安装和使用
  10. 飞秋不能传递文件的两种情况
  11. 浅谈C10K问题 与 解决方案
  12. 从《征途》看互联网盈利模式的设计
  13. 报错:【pip Error】ERROR: Cannot determine archive format of C:\Users\YDD\AppData\Local\Temp\pip-req-buil
  14. 穷爸爸富爸爸的作者破产了,这本书是垃圾还是宝藏?
  15. 《隐私保护周三见》精彩50问 | 交流群互动合集
  16. java 6面骰子_java计算掷6面骰子6000次每个点数出现的概率代码实例
  17. 刺激战场灵敏度怎么调压枪最稳:花了4个小时调的灵敏度,适合所有段位
  18. 如何在微信H5页面链接跳转到第三方小程序的任意页面?
  19. 游戏编程模式之组件模式
  20. 原生JS-实现无限图片轮播

热门文章

  1. python输入输出流详解_输入输出流的概念
  2. 点云标注工具:1.PCAT
  3. C++:while(getline())函数
  4. ROS系统中实现点云聚类(realsense数据源)
  5. Linux那些事儿 之 戏说USB(3)我是一棵树
  6. 判断文件或文件夹(目录)是否存在 C/C++ win/linux通用
  7. 兰州大学C语言程序设计课程作业,【兰州大学|兰州大学C语言程序设计课程作业( 五 )】语言程序设计|课程|作业-傻大方...
  8. php slaveok_ZipArchive::open
  9. ios xib 四等分_ios Xib的几种用法[转]
  10. Error:CPACK_PACKAGE_VERSION does not match version provided by version.hpp header!