有序链表的合并出错了,请各位大神提供指教!
程序代码:#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
typedef struct node NODE;
NODE* creat();
void print(NODE *head);
NODE* sort(NODE *heada,NODE *headb);
void main()
{
NODE *heada,*headb,*headc;
headc=sort(heada,headb);
print(headc);
}
NODE *creat()
{
NODE *head,*p,*q;
int n,i;
printf("\ninput n:");
scanf("%d",&n);
q=head=(NODE*)malloc(sizeof(NODE));
q->next=NULL;
printf("\ninput data:");
for(i=0;i<n;i++)
{
p=(NODE*)malloc(sizeof(NODE));
scanf("%d",&p->data);
p->next=NULL;
q->next=p;
q=p;
}
return(head);
}
void print(NODE *head)
{
NODE *a;
head->next=a;
while(a!=NULL)
{
printf("%d ",a->data);
a=a->next;
}
}
NODE *sort(NODE *heada,NODE *headb)
{
NODE *p,*q,*s,*headc,*wei;
heada=creat();
print(heada);
headb=creat();
print(headb);
headc=heada;
s=headc;
headc->next=NULL;
while(p!=NULL&&q!=NULL)
{
if(p->data<q->data)
{
wei=p;
p=p->next;
}
else
{
wei=q;
q=q->next;
}
headc->next=wei;
s=wei;
}
if(q->next==NULL)
{
s->next=p;
}
else
{
s->next=q;
}
return(headc);
}
其中在我输入输出完heada和headb时,合并链表时出现错误,请多多指教!









