Two known non-descending chain sequences S1 and S2, the construction of new design function list in descending order of the non-merged S1 and S3 after S2.
Input format:
Enter two lines, are given the non-descending sequence composed of a number of positive integers each row, the end of the sequence represented by -1 (-1 does not belong to this sequence). Digital intervals by a space.
Output format:
the combined output of the new non-descending list, separated by spaces between numbers in a row, can not have the extra space at the end; if the new list is empty, output NULL.
Sample input:
. 1. 3. 5 -1
2. 8. 6. 4 10 -1
Output Sample:
123,456,810
#include<stdio.h>
typedef struct LNode{
int data;
struct LNode *next;
}LNode,*LinkList;
LinkList creat_list();
LinkList hb(LinkList L1,LinkList L2);
void print(LinkList L);
int main()
{
LinkList L1=creat_list();
LinkList L2=creat_list();
LinkList L;
L=hb(L1,L2);
print(L);
return 0;
}
LinkList creat_list()//尾插法建表,带有头结点
{
LinkList L=new struct LNode;
L->next=NULL;
int e;
scanf("%d",&e);
LinkList r=L;
while(e!=-1)
{
LinkList s=new struct LNode;
s->next=NULL;
s->data=e;
s->next=r->next;
r->next=s;
r=s;
scanf("%d",&e);
}
return L;
}
LinkList hb(LinkList L1,LinkList L2)
{
LinkList r,L;
r=L=L1;直接利用L1的头结点,不用另开空间了,最后释放L2的头结点,这里没有释放也对了
LinkList p1=L1->next,p2=L2->next;
while(p1&&p2)
{
if(p1->data<=p2->data)
{
r->next=p1;
r=p1;
p1=p1->next;
}
else
{
r->next=p2;
r=p2;
p2=p2->next;
}
}
r->next=p1?p1:p2;//剩余的不用动,直接接上。
;
return L->next;返回首元,直接打印
}
void print(LinkList L)//注意打印时的格式,设置一个标识符
{
if(!L) printf("NULL");
int flag=0;
LinkList p=L;
while(p)
{
if(flag!=0) printf(" ");
printf("%d",p->data);
p=p->next;
flag=1;
}
}