两个有序链表序列的合并(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
上一篇:海盗分赃(8行代码搞定!)
下一篇:装箱问题(c语言)

发表评论

最新留言

关注你微信了!
[***.104.42.241]2025年03月12日 22时36分59秒