該樓層疑似違規已被系統折疊 隐藏此樓檢視此樓
#include
#include
typedef struct LNode
{
int data;
struct LNode *next;
}LNode,*LinkList;
//正序輸出單連結清單
void OutputList(LinkList L)
{
while(L->next)
{
L=L->next;
printf("%d ",L->data);
}
printf("\n\n");
}
//尾插法建立連結清單
LinkList List_TailInsert(LinkList &L)
{
L=(LinkList)malloc(sizeof(LNode));
L->next=NULL;
LNode *p,*r=L;
int x;
scanf("%d",&x);
while(x!=9999)
{
p=(LNode*)malloc(sizeof(LNode));
p->data=x;
r->next=p;
r=p;
scanf("%d",&x);
}
r->next=NULL;
return L;
}
//合并
LinkList MergeLinkList_Increase(LinkList A,LinkList B,LinkList &C)
{
LNode *p=A->next;
LNode *q=B->next;
LNode *r;
C=(LinkList)malloc(sizeof(LNode));
//C=A;//用A的頭結點來做C的頭結點,其實用B的也可以,甚至自己建立一個結點也可以。
C->next=NULL;//将A的頭結點取下,因為這時C隻有頭結點,沒有這句就相當于C=A了。
r=C;//r指向C(指向C的尾結點),因為現在隻有一個結點,頭結點即尾結點。
free(B);//因為p指向了B中有資料的第一個結點,是以B頭結點沒用了,釋放。
while(p&&q)
{
if(p->data<=q->data)
{
r->next=p;
p=p->next;
r=r->next;
}else{
r->next=q;
q=q->next;
r=r->next;
}
}
r->next=NULL;
if(p)
r->next=p;
if(q)
r->next=q;
return C;
}
int main()
{
LinkList A,B,C;
printf("請輸入單連結清單A元素(輸入9999結束接受):");
List_TailInsert(A);
printf("請輸入單連結清單B元素(輸入9999結束接受):");
List_TailInsert(B);
printf("A:");
OutputList(A);
printf("B:");
OutputList(B);
printf("C:");
OutputList(MergeLinkList_Increase(A,B,C));
printf("A:");
OutputList(A);
}