单向链表的创建及简单使用(以学生管理为例,持续更新)

功能

1.创建链表

2.查找

3.遍历

4.删除

5.修改

6.保存和文件读取

代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NMBSCORE 3
struct StNode
{
	int num;        //学号
	char name[20];  //姓名 
	char sex;       //性别
	int age;        //年龄
	int grade;      //班级
	int score[NMBSCORE]; //成绩
	StNode* next;
};


//读取文件
struct StNode* ReadFile(const char* filename,struct StNode* head)
{
	struct StNode* node,*tmp;
	FILE* fp;
	int i;
	if ((fp = fopen(filename,"r")) == 0)
	{
		printf("文件打开失败\n");
		return 0;
	}
	while(!feof(fp))
	{
		node = (struct StNode*)malloc(sizeof(struct StNode));
		fscanf(fp,"%d%20s %c%4d%4d",&node->num,node->name,&node->sex,&node->age,&node->grade);
		for(i=0;i<NMBSCORE;i++)
			fscanf(fp,"%4d",&node->score[i]);
		fscanf(fp,"\n");
		node->next = 0;
		if (head == 0)
		{
			head = node;
			tmp = head;
		}else
		{
			tmp->next = node;
			tmp = node;
		}
	}
	fclose(fp);
	return head;
}

//录入信息
struct StNode* Input(struct StNode* head)
{
	struct StNode* p,*node;
	int i;
	system("cls");
	node = (struct StNode*)malloc(sizeof(struct StNode));
	//录入信息
	printf("请输入学号:");
	scanf("%d",&node->num);
	printf("请输入姓名:"); 
	scanf("%s",node->name);
	printf("请输入性别:");
	scanf(" %c",&node->sex);
	printf("请输入年龄:");
	scanf(" %d",&node->age);
	printf("请输入班级:");
	scanf(" %d",&node->grade);
	printf("请输入%d门课程的成绩:",NMBSCORE);
	for(i=0;i<NMBSCORE;i++)
		scanf("%d",&node->score[i]);
	node->next = 0;

	if (head == 0)
	{
		head = node;
		//
	}else
	{
		p = head;
		while(p->next)
			p = p->next;
		//新建节点
		p->next = node;
	}
	system("pause");
	return head;
}
//显示所有学生信息
void showAll(struct StNode* head)
{
	struct StNode* p;
	int i=1,j;
	system("cls");
	p = head;
	while(p)
	{
		printf("学生%d学号:%d\n",i,p->num);
		printf("学生%d姓名:%s\n",i,p->name); 
		printf("学生%d性别:%c\n",i,p->sex);
		printf("学生%d年龄:%d\n",i,p->age);
		printf("学生%d班级:%d\n",i,p->grade);
		printf("学生%d的成绩:",i);
		for(j=0;j<NMBSCORE;j++)
			printf("%d ",p->score[j]);
		printf("\n");
		i++;
		p = p->next;
	}
	system("pause");
}

//查询信息
void Find(struct StNode* head)
{
	int opt;
	struct StNode* p = head;
	int id,j;
	char name[20]={0};
	system("cls");
	printf("1.按学号查询\n");
	printf("2.按姓名查询\n"); 
	scanf("%d",&opt);
	switch(opt)
	{
	case 1:
		printf("请输入学号:");
		scanf("%d",&id);
		while(p)
		{
			if(p->num == id)
			{
				printf("学生学号:%d\n",p->num);
				printf("学生姓名:%s\n",p->name); 
				printf("学生性别:%c\n",p->sex);
				printf("学生年龄:%d\n",p->age);
				printf("学生班级:%d\n",p->grade);
				printf("学生成绩:");
				for(j=0;j<NMBSCORE;j++)
					printf("%d ",p->score[j]);
				printf("\n");
				break;
			}
			p = p->next;
		}
		if(p==0)
			printf("查无此人\n");
		break;
	case 2:
		printf("请输入姓名:");
		scanf("%s",name);
		while(p)
		{
			if(strcmp(p->name,name) ==0)
			{
				printf("学生学号:%d\n",p->num);
				printf("学生姓名:%s\n",p->name); 
				printf("学生性别:%c\n",p->sex);
				printf("学生年龄:%d\n",p->age);
				printf("学生班级:%d\n",p->grade);
				printf("学生成绩:");
				for(j=0;j<NMBSCORE;j++)
					printf("%d ",p->score[j]);
				printf("\n");
				break;
			}
			p = p->next;
		}
		if(p==0)
			printf("查无此人\n");
		break;
	}
	system("pause");
}




//修改学生信息
void ModInfo(struct StNode* head)
{
	struct StNode* node = 0;
	int id,i;
	system("cls");
	printf("请输入需要修改信息的学生学号:");
	scanf("%d",&id);
	node = head;
	while(node)
	{
		if(node->num == id)
			break;
		else
			node = node->next;
	}
	if(node == 0)
		printf("查无此人\n");
	else
	{
		memset(node->name,0,20);
		printf("请输入姓名:"); 
		scanf("%s",node->name);
		printf("请输入性别:");
		scanf(" %c",&node->sex);
		printf("请输入年龄:");
		scanf(" %d",&node->age);
		printf("请输入班级:");
		scanf(" %d",&node->grade);
		printf("请输入%d门课程的成绩:",NMBSCORE);
		for(i=0;i<NMBSCORE;i++)
			scanf("%d",&node->score[i]);
	}
	system("pause");
}

//删除学生信息
void DeleteInfo(struct StNode* head)
{
	struct StNode* node,*pre;
	int id,i;
	system("cls");
	printf("请输入需要删除信息的学生学号:");
	scanf("%d",&id);
	
	if (head->num == id)
	{
		node = head->next;
		free(head);
		head = node;
		printf("该学生信息已经删除\n");
		system("cls");
		return;
	}else
	{
		pre = head;
		node = head->next;
		while(node)
		{
			if (node->num == id)
			{
				pre->next = node->next;
				free(node);
				node = 0;
				printf("该学生信息已经删除\n");
				system("pause");
				return;
			}else
			{
				pre = node;
				node = node->next;
			}
		}
		printf("查无此人\n");
		system("pause");
	}
}

//学生信息统计
void Tongji(struct StNode* head)
{
	struct StNode* node;
	int opt = 0;
	int count = 0;
	char ch;
	int grade;
	system("cls");
	printf("1.按照性别统计\n");
	printf("2.按照年级统计\n");
	scanf("%d",&opt);
	switch(opt)
	{
	case 1:
		printf("请输入性别:");
		scanf(" %c",&ch);
		node = head;
		count = 0;
		while(node)
		{
			if(node->sex == ch)
				count++;
			node = node->next;
		}
		printf("数量:%d\n",count);
		break;
	case 2:
		printf("请输入年级:");
		scanf("%d",&grade);
		node = head;
		count = 0;
		while(node)
		{
			if(node->grade == grade)
				count++;
			node = node->next;
		}
		printf("数量:%d\n",count);
		break;
	}
	system("pause");
}

//学生信息保存
void Save(const char* name,struct StNode* head)
{
	struct StNode* node;
	FILE* fp;
	int i;
	system("cls");
	if ((fp = fopen(name,"w")) == 0)
	{
		printf("文件打开失败\n");
		system("pause");
		return;
	}
	node = head;
	while(node)
	{
		fprintf(fp,"%d%20s %c%4d%4d",node->num,node->name,node->sex,node->age,node->grade);
		for(i=0;i<NMBSCORE;i++)
			fprintf(fp,"%4d",node->score[i]);
		fprintf(fp,"\n");
		node = node->next;
	}
	fclose(fp);
	printf("文件保存成功!\n");
	system("pause");
}

int main()
{
	struct StNode* head = 0;
	int opt = 0;
	int bgo = 1;
	char* filename = "students.txt";

	head = ReadFile(filename,head); //读取文件
	while(bgo)
	{
		system("cls");
		printf("--------------学生信息管理系统---------------|\n");
		printf("|   1.录入学生信息                           |\n");
		printf("|   2.显示所有学生信息                       |\n");
		printf("|   3.学生信息查询                           |\n");
		printf("|   4.学生信息修改                           |\n");
		printf("|   5.学生信息删除                           |\n");
		printf("|   6.学生信息统计                           |\n");
		printf("|   7.学生信息保存                           |\n");
		printf("|   0.退出系统                               |\n");
		printf("----------------------------------------------\n");
		scanf("%d",&opt);
		switch(opt)
		{
		case 0:
			bgo = 0;
			break;
		case 1:
			head = Input(head);
			break;
		case 2:
			showAll(head);
			break;
		case 3:
			Find(head);
			break;
		case 4:
			ModInfo(head);
			break;
		case 5:
			DeleteInfo(head);
			break;
		case 6:
			Tongji(head);
			break;
		case 7:
			Save(filename,head);
			break;
		}
	}
	return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qfl_sdu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值