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

一、底层数据结构选型

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

}

}

相关推荐
risc12345615 分钟前
如何认识结构?结构 = 要素 + 关系 + 动态
笔记
历程里程碑26 分钟前
Linux 库
java·linux·运维·服务器·数据结构·c++·算法
Sheep Shaun28 分钟前
如何让一个进程诞生、工作、终止并等待回收?——探索Linux进程控制与Shell的诞生
linux·服务器·数据结构·c++·算法·shell·进程控制
Pluchon29 分钟前
硅基计划4.0 简单模拟实现AVL树&红黑树
java·数据结构·算法
風清掦42 分钟前
【江科大STM32学习笔记-05】EXTI外部中断11
笔记·stm32·学习
小龙报44 分钟前
【51单片机】从 0 到 1 玩转 51 蜂鸣器:分清有源无源,轻松驱动它奏响新年旋律
c语言·数据结构·c++·stm32·单片机·嵌入式硬件·51单片机
dllxhcjla1 小时前
数据结构和算法
数据结构
wdfk_prog1 小时前
[Linux]学习笔记系列 -- [drivers][tty]sysrq
linux·笔记·学习
QT.qtqtqtqtqt2 小时前
uni-app小程序前端开发笔记(更新中)
前端·笔记·小程序·uni-app
EmbedLinX2 小时前
嵌入式之协议解析
linux·网络·c++·笔记·学习