为什么空链表不能插入
#include<stdio.h>#include<malloc.h>
void *insret(struct aaa **head,struct aaa *p0);
void print(struct aaa *p);
#define LEN sizeof(struct aaa)
#define NULL 0
struct aaa
{
int num;
struct aaa *next;
};
void main()
{
struct aaa *p1,*p2,*head,*p0;
int n=0;
p1=p2=(struct aaa *)malloc(LEN);
head=NULL;
scanf("%d",&p1->num);
while(p1->num!=0)
{
n++;
if(n==1)
head=p1;
else p2->next=p1;
p2=p1;
p1=(struct aaa *)malloc(LEN);
scanf("%d",&p1->num);
}
p2->next=NULL;
p0=(struct aaa *)malloc(LEN);
printf("please p0");
scanf("%d",&p0->num);
insret(&head,p0);
print(head);
}
void *insret(struct aaa **head,struct aaa *p0)
{
struct aaa *p1,*p2;
p1=*head;
if(head==NULL)
{
*head=p0;
p0->next=NULL;
}
else
while((p0->num > p1->num) && (p1->next!=NULL))
{
p2=p1;
p1=p1->next;
}
if(p1->num >= p0->num)
{
if(*head==p1)
*head=p0;
else p2->next=p0;
p0->next=p1;
}
else
{
p1->next=p0;
p0->next=NULL;
}
return head;
}
void print(struct aaa *p)
{
struct aaa *r;
int k=0;
r=p;
while(r!=NULL)
{
k++;
printf("%d %d\n",k,r->num);
r=r->next;
}
}