Java语法数据结构创建单链表和完善信息

 

思路:

1.定义学生类

2.定义节点类,把学生类当做节点的数据属性

3.创建链表录入数据

按照c语言的方式写就是了,

 @Test
  public void test001() {
    class Student {
      private String name;
      private String number;
      private double score;
      private Student next;

      Student(String name, String number, double score) {
        this.name = name;
        this.number = number;
        this.score = score;
        this.next = null;
      }

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

      public Student getNext() {
        return next;
      }

      public double getScore() {
        return score;
      }
    }

    Student student1 = new Student("李明", "0003", 96);
    Student student2 = new Student("张强", "0008", 86);
    Student student3 = new Student("何倩", "0005", 90);
    Student student4 = new Student("汪水成", "0015", 56);
    Student student5 = new Student("王自强", "0006", 78);

    student1.setNext(student2);
    student2.setNext(student3);
    student3.setNext(student4);
    student4.setNext(student5);
    double highScore = student1.getScore();
    double lowScore = student1.getScore();
    Student next = student1.getNext();
    while (true) {
      if (next == null) {
        break;
      }
      double score = next.getScore();
      if (score > highScore) {
        highScore = score;
      }
      if (score < lowScore) {
        lowScore = score;
      }
      next = next.getNext();
    }
    System.out.println("最高分:" + highScore);
    System.out.println("最低分:" + lowScore);
  }