基于链表的学生管理系统

链表

要是有不懂链表的小伙伴,阔以查看我之前的博客,相关链表的讲
述,在我之前的博客中有详细的介绍。

有关链表详细讲述

在此,我只介绍有关学生管理系统的概述,对于初学者来说,乍一听感觉很难,完全不知头绪,就拿我自己来说,刚开始学链表时,就像在听天书。真不知道啥时候需要传参数,啥时候需要有返回值……

怎么办呢,敲代码说实话,其实真没有什么捷径来投机取巧,唯独熟能生巧、勤加练习,方能精通。话不多说,咱们直奔主题吧!

流程图

在这里插入图片描述

代码演示

student.c文件
/*******************************************************
> Copyright (C) %L ==贵哥== All rights reserved.
> File Name: student.c
> Author: 贵哥
> Website:www.187.qq.com
> Created Time: 2020-09-01
***************************************************/

#include "head.h"

//1.创建链表
LIST *Create_List(void)
{
	//要是一个指针变为变量,则需要动态申请内存
	LIST *head = (LIST *) malloc(sizeof(LIST));
	head->next = NULL;
	return head;
}

//2.创建节点
LIST *Create_Node(STUDENT data)
{
	//要是一个指针变为变量,则需要动态申请内存
	LIST *head = (LIST *) malloc(sizeof(LIST));
	
	head->data = data;
	head->next = NULL;

	return head;
}

//3.添加数据
int Insert_Data_To_List(LIST *head, STUDENT data)
{
	if(head == NULL)
	{
		perror("链表为空");
		return -1;
	}
	else
	{
		LIST *newNode = Create_Node(data);
		newNode->next = head->next;
		head->next    = newNode;
	}
}

//4.删除数据
int Delete_Data_From_List(LIST *head, int num)
{
	if(head == NULL)
	{
		perror("链表为空");
		return -1;
	}
	else{
		LIST *preNode  = head;
		LIST *nextNode = head->next;

		while(nextNode->data.sno != num)
		{

			preNode  = nextNode;
			nextNode = nextNode->next;

			if(nextNode == NULL)
			{
				perror("没有找到要删除的数据");
				return -1;
			}
		}

		preNode->next = nextNode->next;
		printf("要删除的学号为: %d\n", nextNode->data.sno);//此处我删除的是学号,也可以根据自己情况删除其他值
		free(nextNode);
	}
	return 0;
}

//5.遍历链表
void Ergodic_List(LIST *head)
{
	if(head == NULL)
	{
		perror("链表为空");
		return ;
	}
	else{

		LIST *moveNode = head->next;//定义一个节点,用来遍历链表
		printf("姓名\t\t学号\t\t性别\t\t成绩\n");
		while(moveNode != NULL)
		{
			printf(" %s\t\t %d\t\t %s\t\t %d\n", moveNode->data.name,moveNode->data.sno,moveNode->data.sex,moveNode->data.score);
			moveNode = moveNode->next;
		}
	}

	return;
}

//6. 使用学生管理系统提示
void Tip_Student_System(void)
{
	printf("使用学生管理系统说明\n");
	printf("根据输入指令来操作\n");
	printf("1. 打印输出数据 \n");
	printf("2. 插入数据 \n");
	printf("3. 删除数据 \n");
	printf("4. 退出学生管理系统 \n");
}
head.h头文件
/*******************************************************
> Copyright (C) %L ==贵哥== All rights reserved.
> File Name: head.h
> Author: 贵哥
> Website:www.187.qq.com
> Created Time: 2020-09-01
***************************************************/

#ifndef __HEAD_H_
#define __HEAD_H_

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

typedef struct Student
{
	char name[20];	 //姓名
	int sno;		 //学号
	char sex[4];     //性别
	int score;		 //成绩
}STUDENT;

typedef struct List
{
	STUDENT data;	//数据域
	struct List *next;		//指针域
}LIST;

//1.创建链表
LIST *Create_List(void);
//2.创建节点
LIST *Create_Node(STUDENT data);
//3.添加数据
int Insert_Data_To_List(LIST *head, STUDENT num);
//4.删除数据
int Delete_Data_From_List(LIST *head, int num);
//5.遍历链表
void Ergodic_List(LIST *head);
//6. 使用学生管理系统提示
void Tip_Student_System(void);

#endif

main.c主函数文件
/*******************************************************
> Copyright (C) %L ==贵哥== All rights reserved.
> File Name: main.c
> Author: 贵哥
> Website:www.187.qq.com
> Created Time: 2020-09-01
***************************************************/

#include "head.h"

int main(int argc,const char* argv[])
{

	int common = 0;		//指令标志
	//调用创建链表函数
	LIST *head = Create_List();
	STUDENT info;
	//使用学生管理系统提示
	Tip_Student_System();


	while(1)
	{
		setbuf(stdin,NULL);
		printf("请您输入指令\n");
		scanf("%d",&common);
		switch(common)
		{
			case 1:{
				Ergodic_List(head);//遍历链表数据
				break;
			}

			case 2:{
				setbuf(stdin,NULL);//清空缓冲区
				printf("请您输入要插入的数据:  姓名  学号  性别  成绩 \n");
				scanf("%s%d%s%d",info.name,&info.sno,info.sex,&info.score);
				Insert_Data_To_List(head, info);//插入数据
				break;
			}

			case 3:{
				setbuf(stdin,NULL);//清空缓冲区
				printf("请你输入要删除的学号\n");
				scanf("%d",&info.sno);
				Delete_Data_From_List(head, info.sno);//删除数据
				break;
			}

			case 4:{
				printf("欢迎下次使用,再见\n");
				return 0;	//退出学生管理系统
			}
			default :{
				break;
			}
			if(common == 4)
				break;
		}

	}


    return 0;
}


makefile文件
CC=gcc
target=flash_back
obj=main.o level.o game_1.o game_2.o delay.o 

$(target):$(obj)
	$(CC) $(obj) -o $(target)
%.o:%.c
	$(CC) $< -c
clean:
	rm $(target) $(obj)

结果显示

在这里插入图片描述

  • 6
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

weixin_44585751

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

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

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

打赏作者

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

抵扣说明:

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

余额充值