#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct Student
{
char name[10];
struct Student *next;
};
struct Student *create()
{
char str[10];
struct Student *head=NULL;
head=malloc(sizeof(struct Student));
head->next=NULL;
struct Student *current=head;
printf("请输入名字\n");
scanf("%s",str);
strcpy(head->name,str);
char flag=0;
printf("请输入Y或者N\n");
scanf(" %c",&flag);
struct Student *p=NULL;
while(flag=='Y')
{
p=malloc(sizeof(struct Student));
current->next=p;
printf("请输入名字\n");
scanf("%s",str);
strcpy(current->name,str);
current=p;
printf("请输入Y或者N\n");
scanf(" %c",&flag);
}
return head;
}
int main()
{
struct Student *m=NULL;
m=create();
while(1)
{
printf("%s\n",m->name);
if(m->next!=NULL)
{
m=m->next;
}
else break;
}
return 0;
}
构建动态链表记录学生名字
最新推荐文章于 2024-09-03 13:49:41 发布