C语言入门教程 第13讲 动态内存分配和链表(Lellansin)L2

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;
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值