写了一个单向数据链代码,运行老出错!但总找不出问题所在,求助!
#include <stdio.h>#include <malloc.h>
#define size sizeof(struct student)
#define N 5
struct student
{
int shengao;
float tizhong;
struct student *next;
};
struct student *create(void) /*建立链表*/
{
struct student *p1;
struct student *p2;
struct student *head;
int i;
for(i=1;i<=N;i++)
{
p1=(struct student*) malloc(size);
printf("输入身高和体重\n");
scanf("%d %f",&p1->shengao,&p1->tizhong);
if(i==1) /*第一个结点*/
{
head=p2=p1;
}
else if(i<N) /*中间结点*/
{
p2->next=p1;
p2=p1;
}
else if(i=N) /*最后一个结点*/
{
p2->next=p1;
p1->next=NULL;
}
}
return head;
}
void NextPrint(struct student *head) /*输出链表的函数*/
{
struct student *p;
p=head;
printf("输出链表");
while(p->next!=NULL)
{
printf("身高:%d\体重:%f\n",&p->shengao,&p->tizhong);
p=p->next;
}
}
void main()
{
struct student *head=create();
NextPrint(head);
}