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

一、底层数据结构选型

  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();

}

}

相关推荐
hanlin0324 分钟前
刷题笔记:力扣第17题-电话号码的字母组合
笔记·算法·leetcode
لا معنى له25 分钟前
JEPA:联合嵌入预测架构介绍 ——学习笔记
笔记·学习
sprite_雪碧27 分钟前
考研机试笔记-1输入输出
笔记·考研·华为od
不是株35 分钟前
算 法
数据结构·python·算法
自信1504130575937 分钟前
插入排序算法
c语言·数据结构·算法·排序算法
阿Y加油吧42 分钟前
力扣打卡day09——缺失的第一个正数、矩阵置零
数据结构·算法·leetcode
仰泳的熊猫42 分钟前
题目2576:蓝桥杯2020年第十一届省赛真题-解码
数据结构·c++·算法·蓝桥杯
灰色小旋风1 小时前
力扣16 最接近的三数之和(C++)
数据结构·c++·算法·leetcode
前端达人1 小时前
第 4 篇:内容即数据——frontmatter 规范、数据结构与构建链路的工程化设计
大数据·数据结构
噜啦噜啦嘞好1 小时前
算法篇:滑动窗口
数据结构·算法