主题:把两个链表合并为一个
#include <stdio.h>
#include <stdlib.h>
#define ElemType int
typedef struct LNode
{ElemType data;
struct LNode *next;
}LNode,*LinkList;
void createlist(LinkList La,int n)
{int i;
LinkList p;
La=(LinkList)malloc(sizeof(LNode));
for(i=n;i>0;i--)
{p=(LinkList)malloc(sizeof(LNode));
scanf("%d",&p->data);
p->next=La->next;La->next=p;}}
void createlist1(LinkList Lb,int n)
{int i;
LinkList p;
Lb=(LinkList)malloc(sizeof(LNode));
Lb->next=NULL;
for(i=n;i>0;i--)
{p=(LinkList)malloc(sizeof(LNode));
scanf("%d",&p->data);
p->next=Lb->next;Lb->next=p;
}
}
void merglist(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;}
if(pa)
{pc->next=pa;pc=pa;pa=pa->next;}
if(pb)
{pc->next=pb;pc=pb;pb=pb->next;}
}
while(Lc->next)
{printf("%d ",Lc->data);
Lc=Lc->next;
}
}
main()
{int n;
LinkList La,Lb,Lc;
printf("please input n");
scanf("%d",&n);
createlist(La,n)
;
printf("lease input the n again");
scanf("%d",n);
createlist1(Lb,n);
merglist(La,Lb,Lc);
}
把两个链表合并为一个,不知那里错,得不到预想的结果
#include <stdlib.h>
#define ElemType int
typedef struct LNode
{ElemType data;
struct LNode *next;
}LNode,*LinkList;
void createlist(LinkList La,int n)
{int i;
LinkList p;
La=(LinkList)malloc(sizeof(LNode));
for(i=n;i>0;i--)
{p=(LinkList)malloc(sizeof(LNode));
scanf("%d",&p->data);
p->next=La->next;La->next=p;}}
void createlist1(LinkList Lb,int n)
{int i;
LinkList p;
Lb=(LinkList)malloc(sizeof(LNode));
Lb->next=NULL;
for(i=n;i>0;i--)
{p=(LinkList)malloc(sizeof(LNode));
scanf("%d",&p->data);
p->next=Lb->next;Lb->next=p;
}
}
void merglist(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;}
if(pa)
{pc->next=pa;pc=pa;pa=pa->next;}
if(pb)
{pc->next=pb;pc=pb;pb=pb->next;}
}
while(Lc->next)
{printf("%d ",Lc->data);
Lc=Lc->next;
}
}
main()
{int n;
LinkList La,Lb,Lc;
printf("please input n");
scanf("%d",&n);
createlist(La,n)
;
printf("lease input the n again");
scanf("%d",n);
createlist1(Lb,n);
merglist(La,Lb,Lc);
}
把两个链表合并为一个,不知那里错,得不到预想的结果