帮我看看哪儿出错了~~
输入两个链表a,b,包括学号、成绩。要求把两个链表合并,按学号升序排列;现象是,输入a,b链表正常,但是合并链表(链表c)却是NULL,错误初步认定在insert()函数里面,帮忙看看~~谢谢啦o(∩_∩)o #include<stdio.h>
#include<malloc.h>
typedef struct student
{
int code,score;
struct student* next;
} ST;
main()
{
ST *creat();
ST *insert();
ST *ahead,*bhead,*chead;
void pr();
printf("输入A链表数据:\n");
ahead=creat();
printf("输入B链表数据:\n");
bhead=creat();
printf("\n");
printf("A链表:\n");
pr(ahead);
printf("---------------------------------\n");
printf("B链表:\n");
pr(bhead);
chead=insert(ahead,bhead);
printf("---------------------------------\n");
printf("C链表:\n");
pr(chead);
}
ST *creat(void)
{
ST *p1,*p2,*head;
head=(ST *)malloc(sizeof (ST));
p1=head;
p1->next=NULL;
p2=(ST *)malloc(sizeof (ST));
printf("输入学号,成绩:\n");
scanf("%d,%d",&p2->code,&p2->score);
while(p2->code!=0)
{
p1->next=p2;
p1=p2;
p2=(ST *)malloc(sizeof (ST));
printf("输入学号,成绩:\n");
scanf("%d,%d",&p2->code,&p2->score);
p1->next=NULL;
}
return (head);
}
void pr(ST *head)
{
ST *p;
p=head;
p=p->next;
if (p)
do
{
printf("\ncode:%d\nscore:%d\n",p->code,p->score);
p=p->next;
}while (p);
}
ST *insert(ST *ahead,ST *bhead)
{
ST *p1,*p2,*p,*chead;
p1=ahead->next;
p2=bhead->next;
p=chead=(ST *)malloc(sizeof (ST));
p->next=NULL;
while(p1 && p2)
{
if(p1->code>p2->code)
{
p->next=p2;
p2=p2->next;
p->next=NULL;
}
else
{
p->next=p1;
p1=p1->next;
p->next=NULL;
}
}
if(p1=NULL) p->next=p2;
else p->next=p1;
return (chead);
}