用动态链表存放学生信息
建立动态链表,想让输入号码时可以输出对应的数据#include <stdio.h>
#include <stdlib.h>
#define LEN sizeof(struct student)
struct student
{
long num;
char name;
float score[3];
struct student *next;
};
int n;
struct student *creat()
{
struct student *p1,*p2,*head;
void put(struct student *t)
p1=p2=(struct student *)malloc(LEN);
put(p1);
head=NULL;
n=0;
while(p1->num!=0)
{n++;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
put(p1);
}
p2->next=NULL;
return head;
}
void put(struct student *t)
{
printf("please enter num:\n");
scanf("%ld",&t->num);
printf("please enter name:\n");
scanf("%s",&t->name);
printf("please enter score:\n");
scanf("%f %f %f",&t>score[0],&t->score[1],&t->score[2]);
}
int i,j;
void print(struct student *head)
{
struct student *p;
printf("your number:\n");
scanf("%ld",&i);
p=head;
do
{if(p->num==i)
{printf("%ld,%s,%f,%f,%f",p->num,p->name,p->score[0],p->score[1],p->score[2]);
break;
}
else p->next;
}while(p!=NULL);
}
void main()
{
struct student *head;
head=creat();
print(head);
}
不知道是什么问题,只要用到结构体变量,程序运行的时候就会出现assess violation
就算是按着书上动态链表的编程打也会出现这样的情况