#include
#include
//创建结构体
struct school
{
char name[20];
int age;
char gender;
int number;
struct school *next;
};
int count ;
//创建链表
struct school *creat()
{
count=1;
struct school *head=NULL;
struct school *end,*news;
end=news=(struct school*)malloc(sizeof(struct school));
printf("现在请输入第%d位学生的资料\n",count);
printf("请输入学生的学号,没有学生后按0即可:");
scanf("%d",&news->number);
if(news->number==0)
{
goto s;
}
printf("请输入学生的姓名:");
scanf("%s",&news->name);
printf("请输入学生的年龄:");
scanf("%d",&news->age);
printf("请输入学生的性别(M或W):");
scanf("%s",&news->gender);
printf("\n \n");
s: while(news->number!=0)
{
count++;
if(count==1)
{
news->next=head;
head=news;
end=news;
}
else
{
news->next=NULL;
end->next=news;
end=news;
}
news=(struct school*)malloc(sizeof(struct school));
printf("现在请输入第%d位学生的资料\n",count);
printf("请输入学生的学号:");
scanf("%d",&news->number);
if(news->number==0)
{
goto s;
}
printf("请输入学生的姓名:");
scanf("%s",&news->name);
printf("请输入学生的年龄:");
scanf("%d",&news->age);
printf("请输入学生的性别:");
scanf("%s",&news->gender);
printf("\n \n");
}
free(news);
return head;
}
void print(struct school *head)
{
count=1;
struct school *itemp;
itemp=head;
while(itemp==NULL)
{
printf("这是第%d位同学的资料\n",count);
printf("姓名:%s\n",itemp->name);
printf("姓别:%c\n",itemp->gender);
printf("年龄:%d\n",itemp->age);
printf("学号:%d\n",itemp->number);
printf("\n \n \n");
itemp=itemp->next;
}
}
//主函数
int main()
{
printf("-------XX学院学生录入系统-------\n");
printf("请按照要求填写学生姓名,性别,年龄以及学号,\n若输入完成后学号填零即可!\n");
printf("\n \n");
struct school *head;
head=creat();
print(head);
return 0;
}
为什么我的输出是NULL?
修改如下,供参考:
#include <stdio.h>
#include <stdlib.h>
//创建结构体
struct school{
char name[20];
int age;
char gender;
int number;
struct school* next;
};
int count;
//创建链表
struct school* creat()
{
count = 0; //修改
struct school* head = NULL;
struct school* end, * news;
// end = news = (struct school*)malloc(sizeof(struct school));
// printf("现在请输入第%d位学生的资料\n", count);
// printf("请输入学生的学号,没有学生后按0即可:");
// scanf("%d", &news->number);
// if (news->number == 0)
// {
// goto s;
// }
// printf("请输入学生的姓名:");
// scanf("%s", &news->name);
// printf("请输入学生的年龄:");
// scanf("%d", &news->age);
// printf("请输入学生的性别(M或W):");
// scanf("%s", &news->gender);
// printf("\n \n");
while (1) //s: while (news->number != 0) 修改
{
news = (struct school*)malloc(sizeof(struct school));
printf("现在请输入第%d位学生的资料\n", count+1); //修改
printf("请输入学生的学号:");
scanf("%d", &news->number);
if (news->number == 0){
free(news);
break;
}
printf("请输入学生的姓名:");
scanf("%s", news->name); //scanf("%s", &news->name);
printf("请输入学生的年龄:");
scanf("%d", &news->age);
printf("请输入学生的性别(男:M 女:F):");
scanf(" %c", &news->gender); //scanf("%s", &news->gender);
printf("\n \n");
count++;
if (count == 1)
{
news->next = head;
head = news;
end = news;
}
else
{
news->next = NULL;
end->next = news;
end = news;
}
}
//free(news);
return head;
}
void print(struct school* head)
{
int cnt = 0;
struct school* itemp;
itemp = head;
while (itemp != NULL) //(itemp == NULL)
{
cnt++;
printf("这是第%d位同学的资料\n", cnt);
printf("姓名:%s\n", itemp->name);
printf("姓别:%c\n", itemp->gender);
printf("年龄:%d\n", itemp->age);
printf("学号:%d\n", itemp->number);
printf("\n");
itemp = itemp->next;
}
}
//主函数
int main()
{
printf("-------XX学院学生录入系统-------\n");
printf("请按照要求填写学生姓名,性别,年龄以及学号,\n若输入完成后学号填零即可!\n");
printf("\n \n");
struct school* head;
head = creat();
print(head);
return 0;
}
为什么我看不到你的头文件啊,编译器默认会加上去吗?
另外这个问题我不会,也是初学者,但是我对这个头文件好好奇