
两个有序链表序列的合并(c语言编程题)
发布日期:2021-05-06 03:53:28
浏览次数:34
分类:技术文章
本文共 1292 字,大约阅读时间需要 4 分钟。
两个有序链表序列的合并
题目
答案
#include#include struct Array{ int data; struct Array *next;};int main(){ struct Array *head,*head1,*pa,*pb,*head2,*p,*temp; head1=(struct Array *)malloc(sizeof(struct Array)); head1->next=NULL; p=head1; int t; while(1) { scanf("%d",&t); if(t==-1) break; else { temp=(struct Array *)malloc(sizeof(struct Array)); temp->data=t; temp->next=NULL; p->next=temp; p=p->next; } } head2=(struct Array *)malloc(sizeof(struct Array)); head2->next=NULL; p=head2; while(1) { scanf("%d",&t); if(t==-1) break; else { temp=(struct Array *)malloc(sizeof(struct Array)); temp->data=t; temp->next=NULL; p->next=temp; p=p->next; } } head=(struct Array *)malloc(sizeof(struct Array)); head->next=NULL; p=head; pa=head1->next; pb=head2->next; while(pa&&pb) { if(pa->data data) { p->next=pa; p=p->next; pa=pa->next; } else { p->next=pb; p=p->next; pb=pb->next; } } p->next=pa?pa:pb; head1->next=NULL; head2->next=NULL; int flag=0; if(head->next==NULL) { printf("NULL"); return 0; } while(head->next) { if(flag==0) { printf("%d",head->next->data); flag=1; } else printf(" %d",head->next->data); head=head->next; }}
注意
如果malloc报错,就在开头加上这一句
#include
发表评论
最新留言
关注你微信了!
[***.104.42.241]2025年03月12日 22时36分59秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
2021-ICPD昆明站-I Mr. Main and Windmills
2019-03-01
计时器模仿地球绕太阳圆周运动
2019-03-01
1144. The Missing Number (20)
2019-03-01
为什么阿里巴巴不建议在for循环中使用”+”进行字符串拼接
2019-03-01
tp5.1 页面错误!请稍后再试~ 安装好后,提示错误
2019-03-01
删除外键约束
2019-03-01
c++ 预处理命令 #error 用法
2019-03-01
Qt Creator编码
2019-03-01
Linux部署sendmail邮件服务器
2019-03-01
MyBatis5_动态SQL
2019-03-01
《软件方法》第1章 建模和UML
2019-03-01
【今日CV 计算机视觉论文速览 第97期】Tue, 9 Apr 2019
2019-03-01
庄子:谁知南华秋水意?
2019-03-01
Thread.sleep() 和 Thread.yield() 区别
2019-03-01
第1讲 快速入门 《Kotlin 极简教程 》
2019-03-01
官宣:湘江智能“车-站-路-云”一体化协同智慧公交解决方案来啦!
2019-03-01
[OpenGL ES] VBO 顶点缓冲对象
2019-03-01
UltraEdit不产生bak 文件可能不是DOS格式
2019-03-01
云计算-大数据-云安全高等教育改革示范教材
2019-03-01
Web站点安全监控
2019-03-01