基于链表的学生成绩管理系统,包括了学生基本信息的增删改查功能,背后是用链表来实现,也可以理解为带头结点的链表的增删改查。
可以作为实践练手的小项目,也可以当做链表初学者的一个小课程设计。
效果图:
源码如下:
studentSystem.cpp
#include "myList.h"
struct Node* infoNode = createList();
//菜单
void menu()
{
printf("-----------【学生成绩管理系统】-------------\n");
printf("\t\t0.退出系统\n");
printf("\t\t1.录入信息\n");
printf("\t\t2.浏览信息\n");
printf("\t\t3.修改信息\n");
printf("\t\t4.删除信息\n");
printf("\t\t5.查找信息\n");
printf("--------------------------------------------\n");
}
//用户选择事件
void keyDown()
{
int choose;
char name[20] = { 0 };
scanf("%d",&choose);
struct student tmp;
struct Node* tmpNode = NULL;
switch (choose)
{
case 0:
system("pause");
exit(0);
break;
case 1:
printf("------------------【录入信息】---------------\n");
printf("请输入学生姓名,年龄,性别,成绩\n");
scanf("%s%d%s%d",&tmp.name,&tmp.age,&tmp.sex,&tmp.grade);
insertNodeByHead(infoNode,tmp);
break;
case 2:
printf("------------------【浏览信息】---------------\n");
printList(infoNode);
break;
case 3:
printf("------------------【修改信息】---------------\n");
printf("请输入需要修改的学生名字:\n");
scanf("%s",&name);
tmpNode = searchInfoByData(infoNode, name);
if (tmpNode != NULL)
{
printf("请输入学生姓名,年龄,性别,成绩\n");
scanf("%s%d%s%d", &tmp.name, &tmp.age, &tmp.sex, &tmp.grade);
modifyInfoByData(infoNode,name,tmp);
}
break;
case 4:
printf("------------------【删除信息】---------------\n");
printf("请输入需要删除的学生名字:\n");
scanf("%s", &name);
deleteAppoinNode(infoNode,name);
break;
case 5:
printf("------------------【查找信息】---------------\n");
printf("请输入需要查找的学生名字:\n");
scanf("%s", &name);
tmpNode = searchInfoByData(infoNode, name);
if (tmpNode != NULL)
{
printf("name\tage\tsex\tgrade\n");
printf("%s\t%d\t%s\t%d\n", tmpNode->data.name, tmpNode->data.age, tmpNode->data.sex, tmpNode->data.grade);
}
break;
default:
printf("输入错误,请重新输入!\n");
system("pause");
break;
}
}
int main()
{
while (1)
{
menu();
keyDown();
system("pause");
system("cls");
}
system("pause");
return 0;
}
myList.h
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student
{
char name[20];
int age;
char sex[5];
int grade;
};
struct Node
{
student data;
struct Node*next;
};
//创建链表
struct Node* createList()
{
struct Node* headNode = (struct Node*)malloc(sizeof(struct Node));
headNode->next = NULL;
return headNode;
}
//创建节点
struct Node* createNode(student data)
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
//插入链表(头插法)
void insertNodeByHead(struct Node* headNode, student data)
{
struct Node* newNode = createNode(data);
newNode->next = headNode->next;
headNode->next = newNode;
}
//删除结点(根据name进行删除)
void deleteAppoinNode(struct Node* headNode, char* name)
{
struct Node* nowNode = headNode->next;
struct Node* nowNodeFront = headNode;
if (nowNode == NULL)
{
printf("数据为空,无法删除!\n");
return;
}
while (strcmp(nowNode->data.name, name))
{
nowNodeFront = nowNode;
nowNode = nowNode->next;
if (nowNode == NULL)
{
printf("未找到指定数据!\n");
return;
}
}
//找到了指定数据,进行删除
nowNodeFront->next = nowNode->next;
free(nowNode);
printf("删除成功!\n");
}
//查找节点(根据名字查找)
struct Node* searchInfoByData(struct Node* headNode, char *name)
{
struct Node* tmp = headNode->next;
if (tmp ==NULL)
{
printf("无数据!\n");
return NULL;
}
while (strcmp(tmp->data.name, name))
{
tmp = tmp->next;
if (tmp == NULL)
{
printf("无此人!\n");
return NULL;
}
}
//找到,返回当前结点
return tmp;
}
//修改结点
void modifyInfoByData(struct Node* headNode, char* name_before, student data_after)
{
char* name = name_before;
struct Node* tmp = searchInfoByData(headNode, name);
tmp->data = data_after;
}
//打印链表
void printList(struct Node* headNode)
{
struct Node* tmp = headNode->next;
printf("name\tage\tsex\tgrade\n");
while (tmp)
{
printf("%s\t%d\t%s\t%d\n",tmp->data.name,tmp->data.age,tmp->data.sex,tmp->data.grade);
tmp = tmp->next;
}
printf("\n");
}
欢迎建议改进
转载请注明出处,谢谢!
By Lalmon
本文含有隐藏内容,请 开通VIP 后查看