B站链接
C语言入门教程 第13讲 动态内存分配和链表.
大佬发代码的链接我打不开了,自己跟着视频抄了一份,保存一下。
L2
#include<stdio.h>
#include<string.h>
#include<malloc.h>
struct Student
{
char name[10];
struct Student * next;
};
struct Student * create()
{
struct Student *head=NULL,*current=NULL,*next=NULL;
char str[10];
char flag;
printf("输入姓名:\n");
scanf("%s",str);
fflush(stdin);
head=(struct Student*)malloc(sizeof(struct Student));
strcpy(head->name,str);
current=head;
printf("是否继续输入:Y/N ");
scanf("%c",&flag);
fflush(stdin);
while(flag!='N')
{
printf("输入姓名:\n");
scanf("%s",str);
fflush(stdin);
next=(struct Student*)malloc(sizeof(struct Student));
strcpy(next->name,str);
current->next=next;
current=next;
printf("是否继续输入:Y/N ");
scanf("%c",&flag);
fflush(stdin);
}
current->next=NULL;
return head;
}
void list(struct Student *p)
{
printf("\n已经存入的姓名:\n");
while(1)
{
printf("%s\n",p->name);
if(p->next!=NULL)
{
p=p->next;
}
else
{
break;
}
}
}
int main()
{
struct Student *p;
p=create();
list(p);
return 0;
}