系统报错说print函数有一个致命错误,但是我实在找不到
#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct su)
struct su//定义结构体
{
int num;
struct su *next;
};
struct su *create(int n);
struct su *inset(struct su *head,struct su *cha,int j,int n);
void print(struct su *head);
int i=0;
int main()
{
struct su *a,*cha;
int n,j;
scanf("%d",&n);//输入要存多少数
a=create(n);//创建链表
scanf("%d",&j);//输入在那个结点后插数
scanf("%d",&cha->num);//输入想插的数
a=inset(a,cha,j,n);
print(a);//打印链表
printf("\n");
return 0;
}
struct su *create(int n)//传入要输出多少个数
{
struct su *head;
struct su *p1,*p2;//定义3个结构体指针
p1=p2=(struct su *)malloc(LEN);//开辟内存
scanf("%d",&p1->num);//输入元素
head=NULL;//初始化指针
while(i<n)
{
i++;//计算结点
if(i==1)
{
head=p1;//第一个结点当头指针
}
else
{
p2->next=p1;//不是第一个就将p2指向新开辟的结点
}
p2=p1;//再将p2拉到与p1同起跑线
p1=(struct su *)malloc(LEN);//用p1开辟新结点
if(i==n)//当达到所要的数,就停止开辟新结点
break;
scanf("%d",&p1->num);//输入新结点的数据域的值
}
p2->next=NULL;//将最后的结点指向null
return head;//返回头指针
}
struct su *inset(struct su *head,struct su *cha,int j,int n)
{
struct su *p1,*p2,*p0;
int count=0;
p1=head;p0=cha;
if(head==NULL)
{
head=p0;p0->next=NULL;
}
else
{
while(count<=j)//计数到想插结点
{
count++;
p2=p1;p1=p1->next;
}
if(head==p1)
{
p0->next=head;head=p0;//想插结点为首指针时
}
else
{
p2->next=p0;p0->next=p1;//非首非尾时
}
return head;//返回头指针
}
void print(struct su *head)//传入头指针
{
struct su *p=head;//定义指针p
if(head!=0)//当传入的头指针不是0
{
do{
printf("%d ",p->num);//打印
p=p->next;//指下一个
}while(p!=0);//循环到尾指针
}
}