链表合并问题,请教,谢谢。
题目:有连个链表,需要把它们合并,下面是合并部分的程序,执行后总会出现内存错误,请帮忙找出哪块出了问题。谢谢struct student
{
int num;
int score;
struct student *next;
};
typedef struct student nod;
nod *insert(nod *head1,nod *head2)
{
nod *insert_nod(nod *,nod *);
nod *head;
nod *p;
nod *nextp;
if(head1==NULL)
return(head2);
else if(head2==NULL)
return(head1);
else
{
head=head1;
p=head2;
while(p!=NULL)
{
nextp=p->next;
head=insert_nod(head,p);
p=nextp;
}
return(head);
}
}
nod *insert_nod(nod *head,nod *t)
{
nod *p=head;
nod *q=NULL;
while(p->num<t->num&&p!=NULL)
{
q=p;
p=p->next;
}
if(p==head)
{
t->next=p;
return(t);
}
q->next=t;
t->next=p;
return(head);
}
[ 本帖最后由 飘在深圳 于 2010-3-11 17:19 编辑 ]