#if 1 /* 创建链表后再合并的 */
# include "stdio.h"
#include
#include
typedef struct student {
int num;
float score;
struct student *next;
}LinkList;
LinkList* creat(void);
void MergeList_L(LinkList *La,LinkList *Lb);
void print(LinkList *head)
{ LinkList *ptr;
if(head == NULL){
printf("\n No list\n");
return;
}
printf("\n list\n");
for (ptr = head->next; ptr; ptr = ptr->next)
printf(" %d \n", ptr->num);
}
void main( void )
{
LinkList *La,*Lb;
La=creat();print(La);
Lb=creat();print(Lb);
MergeList_L(La,Lb);
print(La);
}
LinkList *creat(void)
{
LinkList *head, *tail, *p;
int num, size = sizeof(struct student);
head=(LinkList*)malloc(size);
head->next=NULL;
tail =head ;
printf("input num:");
scanf("%d", &num);
while (num != 0)
{
p = (LinkList*)malloc(size);
p->num = num;
p->next = NULL;
tail->next = p;
tail=tail->next;
printf("input num:");
scanf("%d", &num);
}
return head;
}
void MergeList_L(LinkList *La,LinkList *Lb)
{
LinkList *pa,*pb,*pc;
pc = La;
pa=La->next;
pb=Lb->next;
while( (pa != NULL) || (pb != NULL) )
{
if( (pa != NULL) && ( (pa->num<=pb->num) || (pb == NULL) ) )
{
pc->next=pa;
pc=pa;
pa=pa->next;
}
else if(pb != NULL)
{
pc->next=pb;
pc=pb;
pb=pb->next;
}
}
free(Lb);
} //MergeList_L
#endif
输入
input num: 1
input num: 2
input num: 0
输出
1
2
输入
input num: 3
input num: 4
input num: 0
输出
1
2
1
2
3
4