C++基于链表的学生信息管理系统代码实现

拾柒
拾柒
发布时间:2026-10-14| 4人浏览过
原创

本文分享一套基于C++单向链表实现的简易学生信息管理系统源码。系统通过结构体存储学生姓名、电话、地址等字段,支持数据的循环录入、排序插入、按姓名查找修改与删除,并利用文件流实现内存数据到磁盘文件的持久化存储。以下代码展示了核心逻辑与完整主流程。

系统设计思路

该管理系统采用控制台交互界面,核心数据结构为带头结点的单向链表。每个节点存储一名学生的详细信息(`students` 结构体),包括姓名、电话、街道、城市和邮编。程序主要分为三个模块:

  • 数据存储层:定义学生结构体和链表节点,实现链表的创建、插入(按姓名字典序排序)、删除、查找和销毁。
  • 文件持久层:使用 fwrite 和 fread 将链表中的节点数据批量写入/读取二进制文件,避免频繁I/O操作。
  • 业务逻辑层:主控菜单处理用户输入,协调各功能函数的调用,提供录入、查询、修改、删除和保存退出等操作。

核心数据结构与工具函数

首先定义学生实体和链表节点。为了简化演示,所有字符串字段均设定为固定长度数组。

#ifndef GOODS_H_INCLUDED
#define GOODS_H_INCLUDED

typedef struct students /*定义学生信息*/
{
 char name[100] ;
 char phone[100] ;
 char street[100] ;
 char city[100] ;
 char youb[100] ; // 此处youb意为Zip Code
 
}students;

#endif // GOODS_H_INCLUDED
#ifndef LIST_H_INCLUDED
#define LIST_H_INCLUDED
#include "goods.h"

typedef struct Node /**链表结构体*/
{
 students data ;
 struct Node *next ;
}Node ;

// 声明主要操作函数
void creatList(Node *head,int n);
void insertList(Node *head,students e) ;
int delList(Node *head,char e[]) ;
Node *searchList(Node *head,char e[]) ;
void disputList(Node *head); // 拼写原代码为disputList
void changeList(Node *head ,char e[]) ;
void destroy(Node *head) ;

#endif // LIST_H_INCLUDED

链表基础操作实现

链表的操作是系统的基石。插入操作 `insertList` 实现了按姓名字符串比较进行升序排列,便于后续管理。删除操作 `delList` 通过遍历找到目标前驱节点并调整指针。注意在释放内存时需使用 free()。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "list.h"
#include "goods.h"

void creatList(Node *head,int n)/**创建一个长度为n的链表*/
{
 int i ;
 students p ;
 for(i=1; i<=n ; i++)
 {
  p=lurushuju() ; // 假设此函数在外部或头文件中定义
  insertList(head,p) ;
 }
}

void insertList(Node *head,students e) /**按姓名顺序插入*/
{
 Node *p;
 Node *q;
 q=(Node*)malloc(sizeof(Node));
 q->data=e;
 // 寻找插入位置:保持姓名字典序递增
 for(p=head; p->next && strcmp( (p->next)->data.name,e.name)<0 ;p=p->next ) ;
 q->next=p->next;
 p->next=q;
}

int delList(Node *head,char e[])/**删除姓名为e的项*/
{
 Node *p;
 for(p=head; p->next && strcmp(e,p->next->data.name) ;p=p->next) ;
 if(p->next ==NULL) // 修正原代码中指向null的错误写法
 {
  return 0 ;
 }
 else
 {
  Node *t;
  t=p->next;
  p->next=t->next;
  free(t);
  return 1 ;
 }
}

Node *searchList(Node *head,char e[])/**查找名字,返回节点指针或NULL*/
{
 Node *p;
 for(p=head; p && strcmp(e,p->data.name) ; p=p->next ) ;
 return p ;
}

void destroy(Node *head)
{
 Node *p;
 Node *temp;
 for(p=head;p;){
    temp=p;
    p=p->next;
    free(temp);
 }
}

数据输入与修改逻辑

为了方便演示,这里合并了部分输入验证逻辑。实际项目中建议增加输入合法性检查(如电话号码格式)。

students lurushuju()
{
 students e ;
 printf("请输入学生的姓名: ") ;
 scanf("%s",e.name);
 printf("请输入学生的电话: ") ;
 scanf("%s",e.phone) ;
 printf("请输入学生的街道: ") ;
 scanf("%s",e.street) ;
 printf("请输入学生的城市: ") ;
 scanf("%s",e.city) ;
 printf("请输入学生的邮编: ") ;
 scanf("%s",e.youb) ;
 return e ;
}

void xiugaishuju(students *e)/**修改单个学生对象的字段*/
{
 int score ;
 do
 {
  printf("\n请选择要修改的信息类型:\n");
  printf("1.姓名 2.电话 3.街道 4.城市 5.邮编 6.退出修改\n");
  scanf("%d",&score) ;
  switch(score)
  {
  case 1:
   scanf("%s",e->name);
   break ;
  case 2:
   scanf("%s",e->phone) ;
   break;
  case 3:
   scanf("%s",e->street) ;
   break ;
  case 4:
   scanf("%s",e->city) ;
   break ;
  case 5:
   scanf("%s",e->youb) ;
   break ;
  default:
   return ;
  }
 }while(1);
}

文件读写与主程序流程

系统通过 savaList (保存) 和 duquLisr (读取) 实现数据持久化。注意路径分隔符在不同操作系统上的差异,Windows通常使用双斜杠 \\。

#include "file.h"

void savaList(Node *head)/*保存数据到文件*/
{
 FILE *fp=fopen("data\\data.txt" ,"w") ;
 if(fp==NULL) return;
 Node *p ;
 for(p=head->next;p;p=p->next)
 {
   fwrite(&(p->data),sizeof(students),1,fp) ;
 }
 fclose(fp) ;
}

void duquLisr(Node *head)/*从文件读取数据到链表*/
{
 FILE *fp=fopen("data\\data.txt" ,"r") ;
 if(fp==NULL) return;
 students e ;
 while( fread(&e,sizeof(students) ,1,fp ) )
 {
   insertList(head,e) ;
 }
 fclose(fp) ;
}

以下是整合了菜单控制和文件操作的主函数入口。原代码中的 system("cls") 和 system("pause") 依赖平台特定的命令,建议在跨平台编译时移除或替换为跨平台清屏方案。

#include <stdio.h>
#include <stdlib.h>
#include "list.h"
#include "goods.h"
#include "file.h"

void mainmenu(Node *head)
{
 int scored ;
 int count=1 ;
 char e[100] ;
 int n;
 students p;
 
 // 尝试先加载已有数据
 duquLisr(head);

 do
 {
  system("cls"); // Windows专用清屏
  printf("================ 学生信息管理系统 ===================\n") ;
  printf("1.录入数据 2.修改数据 3.显示数据 4.删除数据 5.插入数据\n") ;
  printf("7.读取最新数据 6.存盘退出       8.直接退出\n") ;
  printf("====================================================\n") ;
  printf("请输入选项: ") ;
  scanf("%d",&scored);
  
  switch(scored)
  {
  case 1:
   printf("请输入要录入的学生数量: ");
   scanf("%d",&n);
   creatList(head,n);
   printf("录入完成,按任意键继续...\n");
   system("pause") ;
   break ;
  case 2:
   printf("请输入待改学生的姓名: ") ;
   scanf("%s",e);
   changeList(head , e) ;
   system("pause") ;
   break ;
  case 3:
   printf("\n%-10s %-10s %-15s %-10s %-10s\n","姓名","电话","街道","城市","邮编");
   disputList(head);
   system("pause") ;
   break ;
  case 4:
   printf("请输入待删学生的姓名: ");
   scanf("%s",e);
   n=delList(head, e) ;
   if(n){
    printf("删除成功\n");
   }else{
    printf("未找到该学生\n");
   }
   system("pause") ;
   break ;
  case 5:
   printf("请输入想插入的学生信息:\n");
   p=lurushuju();
   insertList(head, p);
   printf("插入成功\n");
   system("pause") ;
   break ;
  case 6:
   savaList(head);
   printf("已保存至文件,即将退出。\n");
   count=0;
   break;
  case 7:
   // 清除当前内存重新读取,防止重复累加
   destroy(head);
   head = (Node*)malloc(sizeof(Node));
   head->next=NULL;
   duquLisr(head);
   printf("数据已刷新\n");
   system("pause") ;
   break;
  case 8:
   count=0;
   break;
  default :
   printf("无效输入\n");
   system("pause") ;
 }
 
 }while(count);
 
 // 清理资源
 destroy(head);
 free(head);
}
 
int main()
{
 Node *head=(Node*)malloc(sizeof(Node));
 head->next=NULL ;
 mainmenu(head) ;
 return 0;
}

以上就是C++实现简单学生信息管理系统的详细内容,更多关于C++链表应用与管理开发实践的资料请关注本站其它相关文章!

相关标签:

免责声明:本文内容仅供参考,不代表本站观点。转载或引用请注明出处。

Python 相关产品和应用

全部下载
Selenium

Selenium

4.51.0 · Windows

用于浏览器自动化和 Web 测试的 Python 工具。

scikit-learn

scikit-learn

1.9.1 · Windows

常用 Python 机器学习算法与模型评估工具库。

Black

Black

26.10.1 · Windows

强调一致性的 Python 自动代码格式化工具。

Ruff

Ruff

0.17.1 · macOS

由 Rust 编写的高速 Python 代码检查和格式化工具。

pytest

pytest

9.1.1 · Windows

简洁而强大的 Python 自动化测试框架。

FastAPI

FastAPI

0.146.0 · Windows

高性能 Python API 框架,支持类型提示和自动接口文档。

Flask

Flask

3.1.3 · Windows

轻量灵活的 Python Web 应用框架。

Django

Django

6.1.2 · Windows

成熟的 Python Web 框架,适合构建内容站点和业务系统。

Spyder

Spyder

6.1.7 · Windows

面向科学计算与数据分析的 Python 集成开发环境。

SciPy

SciPy

1.18.1 · Windows

面向科学、工程和技术计算的 Python 算法库。