如何生成功能性 LinkedList Class?

How to produce a functional LinkedList Class?

作为作业的一部分,我必须生成一个名为 Registry 的 LinkedList class。它旨在成为具有界面的简单学生注册系统的一部分。

不幸的是,我确实碰壁了,不知道下一步该怎么做。注册表 class 只是为了管理一个名为 studentList 的学生链表。

下面是目前的,相当不完整的class我到目前为止所做的。

import java.util.*;

public class Registry 
{
   LinkedList<Student> studentList;

   public Registry()
   {

   }

   public void addStudent(Student aStudent)
   {
      studentList.add(aStudent);
   }

   public void deleteStudent(int studentID)
   {
      studentList.remove(studentID);
   }

   @Override
   public String toString()
   {
      return getClass().getName() + 
   }

   public String format()
   {
      System.out.format(studentList);
   }
}

现在,我主要担心的是使用 Student。作为作业的一部分,我不得不创建另一个名为 Student 的 class,它创建 Students 的实例,包含名字、姓氏、学生 ID 和学位计划作为字符串。

我怎样才能使用那个 sperate class 添加到在注册表中实例化的 LinkedList 中?我怎样才能让注册表 class 充分发挥作用?

我会尝试根据要求提供任何其他详细信息。我可能不清楚,所以如果我不清楚,请告诉我,我会尽力解释。

编辑:这是要求的学生Class:

public class Student 
{
   private String foreName;
   private String surName;
   private String studentID;
   private String degreeScheme;

   public Student()
   {

   }

   public void setForeName(String foreName)
   {
       this.foreName = foreName;
   }

   public String getForeName()
   {
       return foreName;
   }

   public void setSurName(String surName)
   {
       this.surName = surName;
   }

   public String getSurName()
   {
       return surName;
   }

   public void setStudentID(String studentID)
   {
       this.studentID = studentID;
   }

   public String getStudentID()
   {
       return studentID;
   }

   public void setDegreeScheme(String degreeScheme)
   {
       this.degreeScheme = degreeScheme;
   }

   public String getDegreeScheme()
   {
       return degreeScheme;
   }

   @Override
   public String toString()
   {
       return getClass().getName() + "[foreName = " + foreName + " surName "
               + surName + " studentID " + studentID + " degreeScheme " 
               + degreeScheme + "]";
   }

   public void format()
   {
       System.out.format("%5s%20s%11s%20s", foreName, surName, studentID, degreeScheme);
   }
}

在任何数据结构中,几乎总是需要实现三个函数:

  1. 插入
  2. 正在搜索
  3. 删除

让我首先澄清一般的链表结构由什么组成。 链表通过对节点进行操作来工作。每个节点包含你想要store/modify/access.

的实际数据
  • Registry:这个应该负责维护结构,提供inserting/searchingfor/deleting列表中特定节点的方式
  • Student:存储数据并控制数据的访问和修改方式

到目前为止,您已经正确创建了 Registry 框架。 (虽然你会想实现一个搜索方法。)

对于您的学生 class,您只需创建您需要的 class 的成员变量,以及适合它们的 getters/setters。

public class Student {
   private String id;
   private String forename;
   private String surname;
   private String degreeScheme;
   private Student next;  // Maintains a reference to the next node in the list

   Student () {
    //Default constructor values
   }

   Student (String id, String forename, String surname, String degreeScheme, Student next) {
      this.id = id;
      this.forename = forename;
      this.surname  = surname;
      this.degreeScheme = degreeScheme; 
      this.next = next;
   }

   public void setID (String id) {
      this.id = id;
   }

   public String getID () {
      return id;
   }

   public void setforename (String forename) {
      this.forename = forename;
   }

   public String getforename () {
      return forename;
   }

   public void setSurname(String surname) {
      this.surname = surname;
   }

   public String getSurname () {
      return surname;
   }

   public void setDegreeScheme(String degreeScheme) {
      this.degreeScheme = degreeScheme;
   }

   public String getDegreeScheme () {
      return degreeScheme;
   }

   public void setNext (Student next) {
      this.next = next;
   }

   public Student getNext () {
      return next;
   }
} //End Student Class

这应该是您入门的良好基础。请记住,在创建数据结构时,通过确保明确 关注点分离,可以避免很多麻烦。 让学生 class 完全负责存储和维护数据,并让注册表 class 负责维护结构本身!

此时,您可以在您的注册表 class 中使用 Student class,只需插入您想要的节点,搜索它们,根据需要删除它们。

import java.util.Iterator;
import java.util.LinkedList;

public class Tester {
  public static void main(String[] args) {
    Registry r = new Registry();
    r.addStudent(new Student("13", "John", "Doe", "Physics")); // Add a student to the Registry
    r.addStudent(new Student("212", "Jane", "Bow", "Chem")); // Add another Student
    System.out.println(r); // Print the Student List

    r.deleteStudent(212); // Deletes student with ID 212
    System.out.println(r);
  }
}


class Student {
  private String studentID;
  private String foreName;
  private String surName;
  private String degreeScheme;

  public Student(String studentId, String foreName, String surName, String degreeScheme) {
    this.studentID = studentId;
    this.foreName = foreName;
    this.surName = surName;
    this.degreeScheme = degreeScheme;
  }

  public void setForeName(String foreName) {
    this.foreName = foreName;
  }

  public String getForeName() {
    return foreName;
  }

  public void setSurName(String surName) {
    this.surName = surName;
  }

  public String getSurName() {
    return surName;
  }

  public void setStudentID(String studentID) {
    this.studentID = studentID;
  }

  public String getStudentID() {
    return studentID;
  }

  public void setDegreeScheme(String degreeScheme) {
    this.degreeScheme = degreeScheme;
  }

  public String getDegreeScheme() {
    return degreeScheme;
  }

  @Override
  public String toString() {
    return getClass().getName() + "[foreName = " + foreName + " surName " + surName + " studentID "
        + studentID + " degreeScheme " + degreeScheme + "]";
  }

  public void format() {
    System.out.format("%5s%20s%11s%20s", foreName, surName, studentID, degreeScheme);
  }
}


class Registry {
  LinkedList<Student> studentList;

  public Registry() { // Creates studentList
    studentList = new LinkedList<>();
  }

  public void addStudent(Student aStudent) {
    studentList.add(aStudent);
  }

  public void deleteStudent(int studentID) {
    int index = searchList(studentID); // Gets index of the student in the Registry
    if (index == -1)
      throw new IllegalArgumentException("Student not found");
    // Since studentList is implemented as LinkedList, .remove removes element at specified position
    studentList.remove(index);
  }

  // Search by studentID , if found, return position in the list
  private int searchList(int studentID) {
    Iterator<Student> it = studentList.iterator();
    int count = -1;
    while (it.hasNext()) {
      count++;
      Student temp;
      temp = it.next();
      if (Integer.parseInt(temp.getStudentID()) == studentID) {
        return count;
      }
    }
    return -1;
  }

  @Override
  //Suggestions to improve the toString are welcome
  public String toString() {
    for (Student student : studentList) {
      student.format();
      System.out.println();

    }

    return "";
  }
}