下面是小编为大家整理的将两个递增链表合并成一个递减链表,供大家参考。
#include "stdio. h" #include "stdlib. h" struct Node {int date;
struct Node *next;
} Node;
goujian(struct Node *L)
{
struct Node *s, *p;
int e; p=L;
printf("输入数据:
") ;
scanf("%d", &e) ;
while(e!=-999)
{s=(struct Node *) malloc(sizeof(struct Node) ) ;
s->date=e;
s->next=p->next;
p->next=s;
scanf("%d", &e) ;
p=s; }
}
outline(struct Node *L)
{ struct Node *p;
p=L->next;
printf("输出数据:
") ;
while(p!=NULL)
{printf("%d ", p->date) ;
p=p->next;
}
}
hebing(struct Node *h1, struct Node *h2)
{
struct Node *q, *p, *r, *s;
p=h1->next;
q=h2->next;
r=h1;
r->next=NULL;
while(p!=NULL&&q!=NULL)
{if(p->date<=q->date)
{
s=p;
p=p->next;
s->next=r->next;
r->next=s;
}
else
{
s=q;
q=q->next;
s->next=r->next;
r->next=s;
}
if(q==NULL)
{while(p!=NULL)
{s=p;
p=p->next;
s->next=r->next;
r->next=s; }
}
if(p==NULL)
{ while(q!=NULL)
{ s=q;
q=q->next;
s->next=r->next;
r->next=s; }
}
}
free(h2) ;
}
void main()
{ struct Node *h1, *h2;
h1=(struct Node
*) malloc(sizeof(struct Node) ) ;
h2=(struct Node *) malloc(sizeof(struct Node) ) ;
h1->next=NULL;
h2->next=NULL;
goujian(h1) ;
goujian(h2) ;
hebing(h1, h2) ;
outline(h1) ;
}
推荐访问:将两个递增链表合并成一个递减链表 链表 递减 递增