源代码
class Stu {
private final int serialNumber; // 序号
private final String classInfo; // 班级
private final String studentId; // 学号
private final String name; // 姓名
public Stu(int serialNumber, String classInfo, String studentId, String name) {
this.serialNumber = serialNumber;
this.classInfo = classInfo;
this.studentId = studentId;
this.name = name;
}
public void printStudentInfo() {
System.out.println("序号: " + serialNumber +
", 班级: " + classInfo +
", 学号: " + studentId +
", 姓名: " + name);
}
}
// 单链表节点类
class StuNode {
Stu data;
StuNode next;
public StuNode(Stu data) {
this.data = data;
this.next = null;
}
}
class blb {
public static void main(String[] args) {
// 创建四个学生对象
Stu s1 = new Stu(2, "24软件技术2班(3+2)", "24001633", "林春雨");
Stu s2 = new Stu(39, "24软件技术2班(3+2)", "24001672", "欧玉雅");
Stu s3 = new Stu(5, "24软件技术2班(3+2)", "24001636", "张柏静");
Stu s4 = new Stu(33, "24软件技术2班(3+2)", "24001666", "陈小芬");
// 构建单链表
StuNode head = new StuNode(s1);
head.next = new StuNode(s2);
head.next.next = new StuNode(s3);
head.next.next.next = new StuNode(s4);
// 遍历链表并输出学生信息
StuNode current = head;
while (current != null) {
current.data.printStudentInfo();
current = current.next;
}
}
}
运行结果
序号: 2, 班级: 24软件技术2班(3+2), 学号: 24001633, 姓名: 林春雨
序号: 39, 班级: 24软件技术2班(3+2), 学号: 24001672, 姓名: 欧玉雅
序号: 5, 班级: 24软件技术2班(3+2), 学号: 24001636, 姓名: 张柏静
序号: 33, 班级: 24软件技术2班(3+2), 学号: 24001666, 姓名: 陈小芬