这段程序错在哪里?我想把一个结构体插入链表
#include<stdio.h>#include<stdlib.h>
struct student
{
int num;
char name[20];
struct student* next;
};
int main()
{
struct student* head,*p,*ptr;
struct student* creat();
struct student m={4,"chenqi"};
struct student* insert(struct student m,struct student* p,int n);
int n=0;
ptr=creat();
insert(m,ptr,n);
ptr=insert(m,ptr,n);
while(ptr!=NULL)
{
printf("%d %s %p\n",ptr->num ,ptr->name ,ptr->next );
ptr=ptr->next;
}
return 0;
}
struct student* creat()
{
struct student *p1,*p2,*head;
head=NULL;
p1=p2=(struct student*)malloc(sizeof(struct student));
int n=0;
scanf("%d %s",&p1->num,&p1->name);
while(p1->num!=0)
{
n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student*)malloc(sizeof(struct student));
scanf("%d %s",&p1->num,&p1->name);
}
p2->next=NULL;
return head;
}
struct student *insert(struct student m,struct student *p,int n)
{
int i;
struct student *p1,*p2,*p3;
p3=p;
p1=&m;
for(i=0;i<n;i++)
p3=p3->next ;
p2=p3->next ;
p3->next =p1;
p1->next=p2;
return p;
}