数据结构--学生管理系统

一、底层数据结构选型

  1. 数组

◦ 优点:随机访问效率高,代码实现简单

◦ 缺点:固定长度,扩容需重新申请空间;增删元素要移动大量数据

◦ 适用场景:学生数量固定、查询操作频繁的场景

  1. 单链表

◦ 优点:动态扩容,增删操作无需移动大量元素,灵活性强

◦ 缺点:查询需遍历链表,效率较低;需额外存储节点指针

◦ 适用场景:学生数量动态变化、增删操作频繁的场景

二、核心功能模块

  1. 学生信息实体

包含学号(唯一标识)、姓名、年龄、专业等核心字段

  1. 核心操作

◦ 增加:添加前需校验学号唯一性,避免重复

◦ 删除:通过学号定位目标元素/节点,执行删除操作

◦ 修改:先按学号查询到目标,再更新对应信息

◦ 查询:支持按学号精准查询、按姓名模糊查询,以及遍历展示所有学生

三、Java 链表实现核心代码

// 学生实体类

class Student {

String id;

String name;

int age;

Student next;

public Student(String id, String name, int age) {

this.id = id;

this.name = name;

this.age = age;

}

}

// 学生管理系统类

public class StudentManager {

private Student head = new Student(null, null, 0); // 头结点

// 添加学生

public void addStudent(Student stu) {

Student temp = head;

while (temp.next != null) {

if (temp.next.id.equals(stu.id)) {

System.out.println("学号重复,添加失败");

return;

}

temp = temp.next;

}

temp.next = stu;

}

// 按学号删除学生

public void deleteStudent(String id) {

Student temp = head;

while (temp.next != null) {

if (temp.next.id.equals(id)) {

temp.next = temp.next.next;

System.out.println("删除成功");

return;

}

temp = temp.next;

}

System.out.println("未找到该学生");

}

// 遍历所有学生信息

public void showAll() {

Student temp = head.next;

if (temp == null) {

System.out.println("暂无学生信息");

return;

}

while (temp != null) {

System.out.println("学号:" + temp.id + " 姓名:" + temp.name + " 年龄:" + temp.age);

temp = temp.next;

}

}

// 主函数测试

public static void main(String\[\] args) {

StudentManager manager = new StudentManager();

manager.addStudent(new Student("001", "张三", 20));

manager.showAll();

manager.deleteStudent("001");

manager.showAll();

}

}

相关推荐
aprilaaaaa1 小时前
(TC397 学习笔记)二、ICU和TIM的时钟计算
笔记·学习·fpga开发
wuyk5551 小时前
107.FreeRTOS 链表深度解析:从原理到面试满分答案
c语言·开发语言·数据结构·stm32·单片机·链表·面试
-dzk-1 小时前
【链表】LC 138.随机链表的复制
数据结构·链表
hongmai6668881 小时前
ESP32-WROVER-IE-N4R8:外接天线+8MB内存,专治信号焦虑
笔记·单片机·嵌入式硬件·microsoft·risc-v
Persistent的粽子!1 小时前
C++:类与对象(一)
开发语言·c++·经验分享·笔记
xian_wwq2 小时前
【学习笔记】-深度认知系列-第8讲主流AI模型横向评测——GPT、Claude、Gemini、盘古、文心、通义
人工智能·笔记·学习
洋不写bug2 小时前
链表补充练习,双链表的模拟实现
java·数据结构·链表·双链表·底层实现
白狐_7983 小时前
408 数据结构|外部排序:流程与 k 路归并
数据结构·算法
晨欣3 小时前
NVIDIA GPU 架构演进学习笔记(GPT-5.6 Terra 生成)
笔记·学习·gpu·显卡·nvidia·英伟达
一条破秋裤3 小时前
17_ADC原理与转换模式
笔记·stm32·学习