#include <stdio.h>
#include <stdlib.h>
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define ok 1
#define overflow -2
typedef struct LNode
{
int data;
struct LNode *next;
}LNode,*LinkList;
LNode *create_LinkList(void)
{
int data;
LNode *head,*p,*q;
head=p=(LNode *)malloc(sizeof(LNode));
p->next=NULL;
while(1)
{
scanf("%d",&data);
if(data==0)
break;
q=(LNode *)malloc(sizeof(LNode));
q->data=data;
q->next=p->next;
p->next=q;
p=q;
}
return(head);
}
void PrintLinkList(LNode *head)
{
LNode *p;
p=head->next;
if(p!=NULL)
do
{
printf("%d",p->data);
p=p->next;
}
while(p!=NULL);
}
LinkList MergeList_L(LinkList &la,LinkList &lb,LinkList &lc)
{
LinkList pa,pb,pc;
pa=la->next;
pb=lb->next;
lc=pc=la;
while(pa&&pb)
{
if(pa->data<=pb->data)
{pc->next=pa;pc=pa;pa=pa->next;}
else {pc->next=pb;pc=pb;pb=pb->next;}
}
pc->next=pa?pa:pb;
free(lb);
return lc;
}
void main()
{
LinkList la,lb,lc;
la=create_LinkList();
lb=create_LinkList();
MergeList_L(la,lb,lc);
PrintLinkList(lc);
}
這些合併都是需要認爲的輸入2個遞增的鏈表,如果沒有,就用冒泡什麼的再排序。頭插入和尾插入不一樣的!