问个程序,在线等
这个程序是按数据大小递增建一个链,并打印出来,测试数据都是对的,
但是总是出现 Null pointer assigment,那位高人指点去掉这个东西,
并麻烦解释原因
程序如下:
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node *next;
};
void creat(struct node* &head)
{
struct node *p;
struct node *pa,*pb;
int s;
printf("please input numbers end of ctrl^z:\n");
while(scanf("%d",&s)!=EOF)
{
p=(struct node *)malloc(sizeof(struct node));
p->data=s;
pa=head->next;
pb=head;
while(pa!=NULL)
if(pa->data<s) { pa=pa->next;
pb=pb->next;
}
else break;
p->next=pa;
pb->next=p;
}
}
void printnode(struct node *head)
{ struct node *p;
p=head->next;
while(p!=NULL)
{ printf("%d ",p->data);
p=p->next;
}
}
int main(void)
{ struct node *head;
head->data=-1;
head->next=NULL;
creat(head);
printnode(head);
return 0;
}