#include<stdio.h>
#include<stdlib.h>
struct school
{
int date;
struct school *next;
};
void scan(struct school *student)
{
printf("请输入数据");
scanf("%d",&student->date); // 关键错误 &
student->next = NULL;
}
void addstudent(struct school **head)
{
struct school *student=NULL,*temp=NULL;
student=(struct school *)malloc(sizeof(struct school));
if(student==NULL)
{
printf("内存分配失败");
exit(1);
}
scan(student);
if(*head!=NULL)
{
temp=*head;
*head=student;
(*head)->next=temp;
}
else
{
*head=student;
}
}
void printstudent(struct school *head)
{
struct school *student;
student=head;
while(student!=NULL)
{
printf("%d\n",student->date);
student=student->next;
}
}
int main()
{
struct school *head=NULL;
char ch;
while(1)
{ printf("是否输入y/n?");
scanf("%c",&ch);
if(ch=='y')
{
addstudent(&head);
}
if(ch=='n')
{
break;
}
while(getchar() != '\n') continue;
}
while(getchar() != '\n') continue;
printf("是否打印y/n?");
scanf("%c",&ch);
while(ch!='y'||ch!='n')
{
if(ch=='y')
{
printstudent(head);
break;
}
while(getchar() != '\n') continue;
printf("是否打印y/n?");
scanf("%c",&ch);
}
return 0;
}
|