【数据结构】4线性表综合实验

0

章节

巩固2.1到2.3小节。

理解与表达线性表的逻辑结构;

线性表的结构、结构与操作;

顺序表的表示与实现;

顺序表应用;

完成实验1线性表的综合应用

重点

线性表实现;

难点

链表的相关功能实现;

作业或思考题

实验1:线性表的综合实验

内容达成以下标准(考核点):

成绩处理的顺序表实现;

成绩处理的链表实现,分析总结到位。

实验1:线性的综合应用

一、题目及分析

1.1 题目

新学年一年一度的奖学金评定也开始了,请设计上学年你班和兄弟班级同学成绩综合测评情况,

奖学金是同一个班为单位进行评比的。为了横向比较便于管理,请将你班和兄弟班级学生成绩做一个综合排名。班级人数、课程情况、奖学金比例等细节,请你自己根据情况来自定。

请按下面步骤来完成:

1)先定义成绩管理的ADT

2)使用顺序表来实现

3)使用链表来实现

1.2 问题的分析与ADT定义

根据题目要求,我们需要设计一个成绩管理的抽象数据类型(ADT),并使用顺序表和链表来实现。同时,我们需要考虑班级人数、课程情况和奖学金比例等细节。

根据问题描述,可以分析出以下关键点:

  • 需要记录学生的成绩信息,包括学生的姓名、学号、所选修的课程以及对应的成绩。
  • 需要能够记录多个班级的学生信息,并能够进行综合排名。
  • 需要支持增加、删除和查询学生的成绩信息。
  • 需要进行奖学金评定,按照一定比例给予奖励。

基于以上分析,定义成绩管理的ADT:

抽象数据类型:课程(Course)

{

数据对象:课程名称

数据关系:无

基本操作:

获取课程名称

}

抽象数据类型:学生(Student)

{

数据对象:学生姓名、学生学号、学生所选课程、学生课程成绩

数据关系:学生与课程之间存在多对多的关系,一个学生可以选修多门课程,一门课程可以被多个学生选修

基本操作:

设置学生姓名和学号

记录学生的课程成绩

获取指定课程的分数

获取学生所选修的所有课程列表

获取学生姓名

获取学生的平均绩点

}

抽象数据类型:成绩链表(ScoreLink)

{

数据对象:学生信息、课程信息、成绩表

数据关系:成绩链表中存储了多个学生的信息和他们的课程成绩,每个学生都有自己选修的多门课程

基本操作:

在链表末尾追加一个学生对象

输出成绩表

合并两个成绩链表

按照平均绩点对学生进行排序

按照平均绩点排序输出成绩表

获取所有课程的集合

获取课程名称的最大长度

}

二、顺序表的实现

2.1 工程中类结构图

2.2各个类的实现

2.2.1 Course类代码

java 复制代码
package experience1.ArrayList;
public class Course {
   String courseName;//课程昵称
   String courseID;//课程编号
   float credit;// 学分
   float score;//分数
   public Course() {
   }
   public Course(String cn, String ci, int cr) {
       courseName = cn;
       courseID = ci;
       credit = cr;
   }
   public void setData(Course c) {
       this.courseName=c.courseName;
       this.courseID=c.courseID;
       this.credit=c.credit;
       this.score=c.score;
   }
   public String getCourseName() {
       return courseName;
   }
   public void setCourseName(String courseName) {
       this.courseName=courseName;
   }
   public String getCourseID() {
       return courseID;
   }
   public void setCourseID(String courseID) {
       this.courseID = courseID;
   }
   public float getCredit() {
       return credit;
   }
   public void setCredit(float credit) {
       this.credit = credit;
   }
   public float getScore() {
       return score;
   }
   public void setScore(float score) {
       this.score = score;
   }
}

2.2.2 Main类代码

java 复制代码
package experience1.ArrayList;
import java.util.ArrayList;
public class Main {
   public static void main(String[] args) {
/*
必修 116701 5
体育 116702 3
外语拓展 116703 2
艺术类 116704 2
创新教育 115605 2
科技文明 116706 2
海洋科学 116707 1
农业发展 116708 1
生态文明 116709 1
道德法律 116710 1
经济管理116711 1
       */
       ScoreLink class1 = new ScoreLink();
       ScoreLink class2 = new ScoreLink();
       // 班级1的学生和成绩
       experience1.ArrayList.Student s1 = new experience1.ArrayList.Student("何一", "2023092901");
       ArrayList<Course> c1 = new ArrayList<>();
       c1.add(new Course("必修", "116701", 5));
       c1.add(new Course("体育", "116702", 3));
       c1.add(new Course("外语拓展", "116703", 2));
       float[] score1 = new float[] { 90, 80, 70 };
       s1.recordScores(c1, score1);
       class1.append(s1);
       experience1.ArrayList.Student s2 = new experience1.ArrayList.Student();
       s2.setNameAndNumber("何二", "2023092902");
       ArrayList<Course> c2 = new ArrayList<>();
       c2.add(new Course("必修", "116701", 5));
       c2.add(new Course("体育", "116702", 3));
       float[] score2 = new float[] { 80, 60 };
       s2.recordScores(c2, score2);
       class1.append(s2);
       experience1.ArrayList.Student s3 = new experience1.ArrayList.Student();
       s3.setNameAndNumber("何三", "2023092903");
       ArrayList<Course> c3 = new ArrayList<>();
       c3.add(new Course("必修", "116701", 5));
       c3.add(new Course("外语拓展", "116703", 2));
       c3.add(new Course("艺术类", "116704", 2));
       float[] score3 = new float[] { 80, 80, 90 };
       s3.recordScores(c3, score3);
       class1.append(s3);
       experience1.ArrayList.Student s4 = new experience1.ArrayList.Student();
       s4.setNameAndNumber("何四", "2023092904");
       ArrayList<Course> c4 = new ArrayList<>();
       c4.add(new Course("必修", "116701", 5));
       c4.add(new Course("体育", "116702", 3));
       c4.add(new Course("外语拓展", "116703", 2));
       c4.add(new Course("艺术类", "116704", 2));
       c4.add(new Course("经济管理", "116711", 1));
       float[] score4 = new float[] { 93, 85, 95 , 92, 98};
       s4.recordScores(c4, score4);
       class1.append(s4);
       // 班级2的学生和成绩
       experience1.ArrayList.Student s5 = new experience1.ArrayList.Student("张一", "2023092905");
       ArrayList<Course> c5 = new ArrayList<>();
       c5.add(new Course("必修", "116701", 5));
       c5.add(new Course("海洋科学", "116707", 1));
       c5.add(new Course("农业发展", "116708", 1));
       float[] score5 = new float[] { 90, 90, 90 };
       s5.recordScores(c5, score5);
       class2.append(s5);
       experience1.ArrayList.Student s6 = new experience1.ArrayList.Student("张二", "2023092906");
       ArrayList<Course> c6 = new ArrayList<>();
       c6.add(new Course("必修", "116701", 5));
       c6.add(new Course("体育", "116702", 3));
       c6.add(new Course("海洋科学", "116707", 1));
       c6.add(new Course("农业发展", "116708", 1));
       float[] score6 = new float[] { 80, 60, 100, 100};
       s6.recordScores(c6, score6);
       class2.append(s6);
       experience1.ArrayList.Student s7 = new experience1.ArrayList.Student("张三", "2023092907");
       ArrayList<Course> c7 = new ArrayList<>();
       c7.add(new Course("必修", "116701", 5));
       c7.add(new Course("外语拓展", "116703", 2));
       c7.add(new Course("艺术类", "116704", 2));
       float[] score7 = new float[] { 80, 80, 90 };
       s7.recordScores(c7, score7);
       class2.append(s7);
       experience1.ArrayList.Student s8 = new Student("张四", "2023092908");
       ArrayList<Course> c8 = new ArrayList<>();
       c8.add(new Course("必修", "116701", 5));
       c8.add(new Course("体育", "116702", 3));
       c8.add(new Course("外语拓展", "116703", 2));
       c8.add(new Course("艺术类", "116704", 2));
       c8.add(new Course("海洋科学", "116707", 1));
       float[] score8 = new float[] { 95, 95, 95 , 95, 95};
       s8.recordScores(c8, score8);
       class2.append(s8);
       // 输出班级1的成绩表
       System.out.println("班级1成绩表:");
       class1.outs();
       // 输出班级2的成绩表
       System.out.println("班级2成绩表:");
       class2.outs();
       // 合并两个班级的数据
       class1.merge(class2);
       // 输出合并后的班级成绩表
       System.out.println("两个班排序后的班级成绩表:");
       class1.outsByPoint();
   }
}

2.2.3 Node类代码

java 复制代码
package experience1.ArrayList;
public class Node {
   Student data;
   Node next;

   public Node(Student data) {
       this.data = new Student(data.getName(), data.getNumber());
       for (Course course : data.getCourses()) {
           Course newCourse = new Course();
           newCourse.setCourseName(course.getCourseName());
           newCourse.setCourseID(course.getCourseID());
           newCourse.setScore(course.getScore());
           newCourse.setCredit(course.getCredit());
           this.data.getCourses().add(newCourse);
       }
       this.next = null;
   }
}
java 复制代码
package experience1.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;
public class ScoreLink {
   private experience1.ArrayList.Node head;
   private int studentCounts;
   public ScoreLink() {
       head = null;
       studentCounts = 0;
   }
   public void append(experience1.ArrayList.Student s) {
       experience1.ArrayList.Node newNode = new experience1.ArrayList.Node(s);

       if (head == null) {
           head = newNode;
       } else {
           experience1.ArrayList.Node current = head;
           while (current.next != null) {
               current = current.next;
           }
           current.next = newNode;
       }
       studentCounts++;
   }
   public void outs() {
       System.out.println("学生人数为:" + studentCounts);
       Map<String, experience1.ArrayList.Course> courseMap = getAllCourses();
       // 设置固定分隔符长度
       int maxCourseNameLength = getMaxCourseNameLength(courseMap);
       int separate = maxCourseNameLength + 5;
       // 输出表头
       System.out.printf("%-10s", "姓名");
       for (experience1.ArrayList.Course course : courseMap.values()) {
           System.out.printf("%-" + separate + "s", course.getCourseName());
       }
       System.out.println("平均绩点");
       separate +=3;
       experience1.ArrayList.Node current = head;
       while (current != null) {
           System.out.printf("%-10s", current.data.getName());
           for (experience1.ArrayList.Course course : courseMap.values()) {
               float score = current.data.getScore(course);
               if (score != -1) {
                   System.out.printf("%-" + separate + ".2f", score);
               } else {
                   System.out.printf("%-" + separate + "s", "非选 ");
               }
           }
           System.out.printf("%8.2f%n", current.data.getAveragePoint());
           current = current.next;
       }
       System.out.println();
   }
   private Map<String, experience1.ArrayList.Course> getAllCourses() {
       Map<String, experience1.ArrayList.Course> courseMap = new LinkedHashMap<>();
       experience1.ArrayList.Node current = head;
       while (current != null) {
           for (experience1.ArrayList.Course course : current.data.getCourses()) {
               if (!courseMap.containsKey(course.getCourseName())) {
                   courseMap.put(course.getCourseName(), course);
               }
           }
           current = current.next;
       }
       return courseMap;
   }
   private int getMaxCourseNameLength(Map<String, experience1.ArrayList.Course> courseMap) {
       int maxLength = 0;
       for (experience1.ArrayList.Course course : courseMap.values()) {
           int length = course.getCourseName().length();
           if (length > maxLength) {
               maxLength = length;
           }
       }
       return maxLength;
   }
   public void merge(ScoreLink scoreLink) {
       experience1.ArrayList.Node current = head;
       while (current.next != null) {
           current = current.next;
       }
       current.next = scoreLink.head;
       studentCounts += scoreLink.studentCounts;
   }
   public void sort() {
       boolean swapped;
       experience1.ArrayList.Node current;
       experience1.ArrayList.Node last = null;
       if (head == null) {
           return;
       }
       do {
           swapped = false;
           current = head;

           while (current.next != last) {
               if (current.data.getAveragePoint() < current.next.data.getAveragePoint()) {
                   Student temp = current.data;
                   current.data = current.next.data;
                   current.next.data = temp;
                   swapped = true;
               }
               current = current.next;
           }
           last = current;
       } while (swapped);
   }
   public void outsByPoint() {
       sort();
       // 输出表头
       System.out.format("%-8s%-15s", "排名", "姓名");
       Map<String, experience1.ArrayList.Course> courseMap = getAllCourses();
       for (experience1.ArrayList.Course course : courseMap.values()) {
           System.out.format("%-15s", course.getCourseName());
       }
       System.out.println("平均绩点");
       // 输出学生信息和成绩
       int rank = 1;
       Node current = head;
       while (current != null) {
           System.out.format("%-10d%-15s", rank++, current.data.getName());
           for (Course course : courseMap.values()) {
               float score = current.data.getScore(course);
               if (score != -1) {
                   System.out.format("%-20.2f", score);
               } else {
                   System.out.format("%-20s", "非选");
               }
           }
           System.out.format("%.2f%n", current.data.getAveragePoint());
           current = current.next;
       }
   }
}

2.2.5 Student类代码

java 复制代码
package experience1.ArrayList;
import java.util.ArrayList;
public class Student {
   ArrayList<Course> courses;
   String name;
   String number;
   float averagePoint;
   public Student() {
       courses = new ArrayList<>();
       averagePoint = 0;
   }
   public Student(String name, String number) {
       courses = new ArrayList<>();
       this.name = name;
       this.number = number;
       averagePoint = 0;
   }
   public void setNameAndNumber(String name, String number) {
       this.name = name;
       this.number = number;
   }
   public void recordScores(ArrayList<Course> courses, float[] scores) {
       for (int i = 0; i < courses.size(); i++) {
           Course course = new Course();
           course.courseName = courses.get(i).courseName;
           course.courseID = courses.get(i).courseID;
           course.score = scores[i];
           course.credit = courses.get(i).credit;
           this.courses.add(course);
       }
       this.setPoint();
   }
   public void setPoint() {
       float getPoints = 0;// 个人绩点和
       float allPoints = 0; // 课程总绩点
       for (Course c : courses) {
           getPoints += (c.score - 50) / 10 * c.credit;
           allPoints += c.credit;
       }
       averagePoint = getPoints / allPoints;
   }
   public float getScore(Course course) {
       for (Course c : courses) {
           if (c.courseID.equals(course.courseID)) {
               return c.score;
           }
       }
       return -1; // 如果找不到指定课程的分数,则返回-1表示未选修该课程
   }
   public ArrayList<Course> getCourses() {
       return courses;
   }
   public String getName() {
       return name;
   }
   public float getAveragePoint() {
       return averagePoint;
   }

package experience1.LinkedList;

import java.util.LinkedList;

public class Student {
   LinkedList<Course> courses;
   String name;
   String number;
   float averagePoint;

   public Student() {
       courses = new LinkedList<>();
       averagePoint = 0;
   }

   public Student(String name, String number) {
       courses = new LinkedList<>();
       this.name = name;
       this.number = number;
       averagePoint = 0;
   }

   public void setNameAndNumber(String name, String number) {
       this.name = name;
       this.number = number;
   }

   public void recordScores(LinkedList<Course> courses, float[] scores) {
       for (int i = 0; i < courses.size(); i++) {
           Course course = new Course();
           course.courseName = courses.get(i).courseName;
           course.courseID = courses.get(i).courseID;
           course.score = scores[i];
           course.credit = courses.get(i).credit;
           this.courses.add(course);
       }

       this.setPoint();
   }

   public void setPoint() {
       float getPoints = 0;// 个人绩点和
       float allPoints = 0; // 课程总绩点
       for (Course c : courses) {
           getPoints += (c.score - 50) / 10 * c.credit;
           allPoints += c.credit;
       }
       averagePoint = getPoints / allPoints;
   }

   public float getScore(Course course) {
       for (Course c : courses) {
           if (c.courseID.equals(course.courseID)) {
               return c.score;
           }
       }
       return -1; // 如果找不到指定课程的分数,则返回-1表示未选修该课程
   }

   public LinkedList<Course> getCourses() {
       return courses;
   }

   public String getName() {
       return name;
   }

   public float getAveragePoint() {
       return averagePoint;
   }
   public String getNumber() {
       return number;
   }
}
}

2.3程序运行结果

三、链表的实现

3.1 工程中类结构图

3.2各个类的实现

3.2.1 Course类代码

java 复制代码
package experience1.LinkedList;
public class Course {
   String courseName;//课程昵称
   String courseID;//课程编号
   float credit;// 学分
   float score;//分数
   public Course() {
   }
   public Course( String cn, String ci,int cr) {
       courseName = cn;
       courseID = ci;
       credit = cr;
   }
   public void setData(Course c) {
       this.courseName=c.courseName;
       this.courseID=c.courseID;
       this.credit=c.credit;
       this.score=c.score;
   }
   public String getCourseName() {
       return courseName;
   }
   public void setCourseName(String courseName) {
       this.courseName=courseName;
   }
   public String getCourseID() {
       return courseID;
   }
   public void setCourseID(String courseID) {
       this.courseID = courseID;
   }
   public float getCredit() {
       return credit;
   }
   public void setCredit(float credit) {
       this.credit = credit;
   }
   public float getScore() {
       return score;
   }
   public void setScore(float score) {
       this.score = score;
   }
}

3.2.2 Main类代码

java 复制代码
package experience1.LinkedList;
import java.util.LinkedList;
public class Main {
   public static void main(String[] args) {
/*
必修 116701 5
体育 116702 3
外语拓展 116703 2
艺术类 116704 2
创新教育 115605 2
科技文明 116706 2
海洋科学 116707 1
农业发展 116708 1
生态文明 116709 1
道德法律 116710 1
经济管理116711 1
       */
       ScoreLink class1 = new ScoreLink();
       ScoreLink class2 = new ScoreLink();
       // 班级1的学生和成绩
       Student s1 = new Student("何一", "2023092901");
       LinkedList<Course> c1 = new LinkedList<>();
       c1.add(new Course("必修", "116701", 5));
       c1.add(new Course("体育", "116702", 3));
       c1.add(new Course("外语拓展", "116703", 2));
       float[] score1 = new float[] { 90, 80, 70 };
       s1.recordScores(c1, score1);
       class1.append(s1);
       Student s2 = new Student();
       s2.setNameAndNumber("何二", "2023092902");
       LinkedList<Course> c2 = new LinkedList<>();
       c2.add(new Course("必修", "116701", 5));
       c2.add(new Course("体育", "116702", 3));
       float[] score2 = new float[] { 80, 60 };
       s2.recordScores(c2, score2);
       class1.append(s2);

       Student s3 = new Student();
       s3.setNameAndNumber("何三", "2023092903");
       LinkedList<Course> c3 = new LinkedList<>();
       c3.add(new Course("必修", "116701", 5));
       c3.add(new Course("外语拓展", "116703", 2));
       c3.add(new Course("艺术类", "116704", 2));
       float[] score3 = new float[] { 80, 80, 90 };
       s3.recordScores(c3, score3);
       class1.append(s3);

       Student s4 = new Student();
       s4.setNameAndNumber("何四", "2023092904");
       LinkedList<Course> c4 = new LinkedList<>();
       c4.add(new Course("必修", "116701", 5));
       c4.add(new Course("体育", "116702", 3));
       c4.add(new Course("外语拓展", "116703", 2));
       c4.add(new Course("艺术类", "116704", 2));
       c4.add(new Course("经济管理", "116711", 1));
       float[] score4 = new float[] { 93, 85, 95 , 92, 98};
       s4.recordScores(c4, score4);
       class1.append(s4);

       // 班级2的学生和成绩
       Student s5 = new Student("张一", "2023092905");
       LinkedList<Course> c5 = new LinkedList<>();
       c5.add(new Course("必修", "116701", 5));
       c5.add(new Course("海洋科学", "116707", 1));
       c5.add(new Course("农业发展", "116708", 1));
       float[] score5 = new float[] { 90, 90, 90 };
       s5.recordScores(c5, score5);
       class2.append(s5);

       Student s6 = new Student("张二", "2023092906");
       LinkedList<Course> c6 = new LinkedList<>();
       c6.add(new Course("必修", "116701", 5));
       c6.add(new Course("体育", "116702", 3));
       c6.add(new Course("海洋科学", "116707", 1));
       c6.add(new Course("农业发展", "116708", 1));
       float[] score6 = new float[] { 80, 60, 100, 100};
       s6.recordScores(c6, score6);
       class2.append(s6);

       Student s7 = new Student("张三", "2023092907");
       LinkedList<Course> c7 = new LinkedList<>();
       c7.add(new Course("必修", "116701", 5));
       c7.add(new Course("外语拓展", "116703", 2));
       c7.add(new Course("艺术类", "116704", 2));
       float[] score7 = new float[] { 80, 80, 90 };
       s7.recordScores(c7, score7);
       class2.append(s7);
       Student s8 = new Student("张四", "2023092908");
       LinkedList<Course> c8 = new LinkedList<>();
       c8.add(new Course("必修", "116701", 5));
       c8.add(new Course("体育", "116702", 3));
       c8.add(new Course("外语拓展", "116703", 2));
       c8.add(new Course("艺术类", "116704", 2));
       c8.add(new Course("海洋科学", "116707", 1));
       float[] score8 = new float[] { 95, 95, 95 , 95, 95};
       s8.recordScores(c8, score8);
       class2.append(s8);
       // 输出班级1的成绩表
       System.out.println("班级1成绩表:");
       class1.outs();
       // 输出班级2的成绩表
       System.out.println("班级2成绩表:");
       class2.outs();

       // 合并两个班级的数据
       class1.merge(class2);
       // 输出合并后的班级成绩表
       System.out.println("两个班排序后的班级成绩表:");
       class1.outsByPoint();
   }
}

3.2.3 Node类代码

java 复制代码
package experience1.LinkedList;
public class Node {
   Student data;
   Node next;
   public Node(Student data) {
       this.data = new Student(data.getName(), data.getNumber());
       for (Course course : data.getCourses()) {
           Course newCourse = new Course();
           newCourse.setCourseName(course.getCourseName());
           newCourse.setCourseID(course.getCourseID());
           newCourse.setScore(course.getScore());
           newCourse.setCredit(course.getCredit());
           this.data.getCourses().add(newCourse);
       }
       this.next = null;
   }
}

3.2.4 ScoreLink类代码

java 复制代码
package experience1.LinkedList;

import java.util.LinkedHashMap;
import java.util.Map;

public class ScoreLink {
   private Node head;
   private int studentCounts;

   public ScoreLink() {
       head = null;
       studentCounts = 0;
   }

   public void append(Student s) {
       Node newNode = new Node(s);

       if (head == null) {
           head = newNode;
       } else {
           Node current = head;
           while (current.next != null) {
               current = current.next;
           }
           current.next = newNode;
       }

       studentCounts++;
   }

   public void outs() {
       System.out.println("学生人数为:" + studentCounts);
       Map<String, Course> courseMap = getAllCourses();

       // 设置固定分隔符长度
       int maxCourseNameLength = getMaxCourseNameLength(courseMap);
       int separate = maxCourseNameLength + 5;

       // 输出表头
       System.out.printf("%-10s", "姓名");
       for (Course course : courseMap.values()) {
           System.out.printf("%-" + separate + "s", course.getCourseName());
       }
       System.out.println("平均绩点");

       separate +=3;
       Node current = head;
       while (current != null) {
           System.out.printf("%-10s", current.data.getName());
           for (Course course : courseMap.values()) {
               float score = current.data.getScore(course);
               if (score != -1) {
                   System.out.printf("%-" + separate + ".2f", score);
               } else {
                   System.out.printf("%-" + separate + "s", "非选 ");
               }
           }
           System.out.printf("%8.2f%n", current.data.getAveragePoint());
           current = current.next;
       }
       System.out.println();
   }

   private Map<String, Course> getAllCourses() {
       Map<String, Course> courseMap = new LinkedHashMap<>();
       Node current = head;
       while (current != null) {
           for (Course course : current.data.getCourses()) {
               if (!courseMap.containsKey(course.getCourseName())) {
                   courseMap.put(course.getCourseName(), course);
               }
           }
           current = current.next;
       }
       return courseMap;
   }

   private int getMaxCourseNameLength(Map<String, Course> courseMap) {
       int maxLength = 0;
       for (Course course : courseMap.values()) {
           int length = course.getCourseName().length();
           if (length > maxLength) {
               maxLength = length;
           }
       }
       return maxLength;
   }

   public void merge(ScoreLink scoreLink) {
       Node current = head;
       while (current.next != null) {
           current = current.next;
       }
       current.next = scoreLink.head;
       studentCounts += scoreLink.studentCounts;
   }
   public void sort() {
       boolean swapped;
       Node current;
       Node last = null;
       if (head == null) {
           return;
       }

       do {
           swapped = false;
           current = head;

           while (current.next != last) {
               if (current.data.getAveragePoint() < current.next.data.getAveragePoint()) {
                   Student temp = current.data;
                   current.data = current.next.data;
                   current.next.data = temp;
                   swapped = true;
               }
               current = current.next;
           }
           last = current;
       } while (swapped);
   }
   public void outsByPoint() {
       sort();
       // 输出表头
       System.out.format("%-8s%-15s", "排名", "姓名");
       Map<String, Course> courseMap = getAllCourses();
       for (Course course : courseMap.values()) {
           System.out.format("%-15s", course.getCourseName());
       }
       System.out.println("平均绩点");
       // 输出学生信息和成绩
       int rank = 1;
       Node current = head;
       while (current != null) {
           System.out.format("%-10d%-15s", rank++, current.data.getName());
           for (Course course : courseMap.values()) {
               float score = current.data.getScore(course);
               if (score != -1) {
                   System.out.format("%-20.2f", score);
               } else {
                   System.out.format("%-20s", "非选");
               }
           }
           System.out.format("%.2f%n", current.data.getAveragePoint());
           current = current.next;
       }
   }
}

3.2.5 Student类代码

java 复制代码
package experience1.LinkedList;
import java.util.LinkedList;
public class Student {
   LinkedList<Course> courses;
   String name;
   String number;
   float averagePoint;
   public Student() {
       courses = new LinkedList<>();
       averagePoint = 0;
   }
   public Student(String name, String number) {
       courses = new LinkedList<>();
       this.name = name;
       this.number = number;
       averagePoint = 0;
   }
   public void setNameAndNumber(String name, String number) {
       this.name = name;
       this.number = number;
   }

   public void recordScores(LinkedList<Course> courses, float[] scores) {
       for (int i = 0; i < courses.size(); i++) {
           Course course = new Course();
           course.courseName = courses.get(i).courseName;
           course.courseID = courses.get(i).courseID;
           course.score = scores[i];
           course.credit = courses.get(i).credit;
           this.courses.add(course);
       }

       this.setPoint();
   }
   public void setPoint() {
       float getPoints = 0;// 个人绩点和
       float allPoints = 0; // 课程总绩点
       for (Course c : courses) {
           getPoints += (c.score - 50) / 10 * c.credit;
           allPoints += c.credit;
       }
       averagePoint = getPoints / allPoints;
   }
   public float getScore(Course course) {
       for (Course c : courses) {
           if (c.courseID.equals(course.courseID)) {
               return c.score;
           }
       }
       return -1; // 如果找不到指定课程的分数,则返回-1表示未选修该课程
   }

   public LinkedList<Course> getCourses() {
       return courses;
   }
   public String getName() {
       return name;
   }
   public float getAveragePoint() {
       return averagePoint;
   }
   public String getNumber() {
       return number;
   }
}

3.3程序运行结果

四 实验总结

在本次实验中,我设计了一个成绩管理的抽象数据类型(ADT),并通过顺序表和链表两种数据结构来实现。

通过使用顺序表,我能够将学生的成绩信息按照顺序存储在数组中,并利用数组的索引进行快速访问和修改。这种实现方式适用于班级人数较少,查询和修改操作较为频繁的情况。

而通过使用链表,我将学生的成绩信息以节点的形式连接起来,每个节点包含学生的姓名、学号、所选修的课程和对应的成绩。链表的灵活性使得插入和删除操作更加高效,适用于班级人数较多,需要频繁进行插入和删除操作的情况。

此外,我还考虑了班级人数、课程情况和奖学金比例等细节。通过合理的设计,可以满足不同班级的需求,并进行综合排名和奖学金评定。

代码的优点:

  1. 可扩展性:通过使用抽象数据类型(ADT)的设计,代码具备良好的扩展性。可以轻松添加新的功能或修改现有功能,以适应不同的需求。

  2. 模块化:代码结构清晰,逻辑层次分明,各个功能模块相互独立,易于理解和维护。

  3. 灵活性:使用了顺序表和链表两种数据结构来实现,可以根据具体情况选择最合适的数据结构,以提高代码的效率和性能。

代码的缺点:

  1. 性能问题:如果数据量很大,使用顺序表可能会导致插入和删除的操作变得很慢,因为需要移动大量的元素。而使用链表则可能会导致遍历操作变得相对较慢。因此,在涉及大规模数据时,可能需要考虑其他更高效的数据结构。

  2. 错误处理:代码中可能没有足够的错误处理机制,例如没有对输入进行严格的检查和验证,导致可能出现潜在的错误或异常情况。

  3. 可读性与可维护性:虽然代码结构模块化,但仍有可能存在一些重复的代码段或命名不够清晰的情况,导致代码可读性和可维护性降低。

通过本次实验,我深入了解了成绩管理系统的设计和实现过程,加深了对顺序表和链表数据结构的理解。同时,也锻炼了问题分析与设计的能力,提高了代码实现的技巧和效率。

总的来说,本次实验对我个人的学习和成长有着积极的影响,使我能够更好地应用数据结构和算法来解决实际问题。

相关推荐
葵野寺2 分钟前
【Java--数据结构】优先级队列( PriorityQueue)
数据结构
Cindy辛蒂1 小时前
C语言:计算并输出三个整数的最大值 并对三个数排序
c语言·数据结构·算法
论迹1 小时前
【数据结构】-- LinkedList与链表(2)
java·开发语言·数据结构·链表
BingLin-Liu2 小时前
蓝桥杯备考:数据结构vector-----询问学号
数据结构·职场和发展·蓝桥杯
橘子真甜~2 小时前
36.C++二叉树进阶5(平衡二叉搜索树 - 红黑树及其插入操作图解)
开发语言·数据结构·c++·算法·面试·二叉树·红黑树
Run_Teenage2 小时前
C语言之数据结构:理解什么是数据结构和算法(启航)
c语言·数据结构·算法
BAGAE2 小时前
Facebook 的框架及技术栈
大数据·数据结构·python·算法·数据挖掘·memcached
十年一梦实验室3 小时前
C++ 输出指针地址 输出指针数据
数据结构·c++·算法
退1️⃣码头3 小时前
数据结构(排序)
c语言·数据结构·算法
Wnq100723 小时前
DDS:保障物联网系统的稳定运行和高效协作
java·数据结构·分布式·嵌入式硬件·物联网·深度优先·信号处理