返回链接列表中具有最大值的对象 java

Returning object with max value inside of a linked List java

我正在研究方法 bestStudent()。我return GPA 最高的学生。我一直收到错误 Student cannot be converted to LinkedList.node。我正在尝试使用 while 循环 return 具有最高 gpa 的对象 Student。

public class LinkedListStud
{
    private Node list;

    public LinkedListStud()
    {
      list = null;   
    }

    public boolean isEmpty()
    {
      return (list == null);
    }

    public void addFront(Student s)
    {
      Node newNode = new Node(s);
      newNode.next = list;
      list = newNode;
    }

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

        if (isEmpty())
        list = newNode;
        else
        {
            current = list;
            while(current.next !=null)
                  current = current.next;
                  current.next = newNode;
        }
    }

    public Student bestStudent()
    {
       Node current = list;
       while (current.next != null)
       if (current.data.getGPA() > current.next.data.getGPA())
       return current = current.data;
    }

    public void printLinkedList()
    {
        Node l = list;
        while (list != null)
        l.toString();
        System.out.println("next Link: " + l.next);
        l = l.next;
        System.out.println();
    }

    private class Node
    {
        public Student data;
        public Node next;

        public Node(Student s)
        {
            data = s;
            next = null;      
        }
    }
}

public class Student implements Comparable<Student>
{
    public String lastName;
    public double gpa;
    public int age;

    public Student(String lastName, double gpa, int age) 
    {
        this.lastName = lastName;
        this.gpa = gpa;
        this.age = age;
    }

    public int compareTo(Student s)
    {
        return (this.gpa < s.gpa) ? -1 : (this.gpa > s.gpa) ? 1: 0;
    }

    public String toString()
    {
        return "Student: \t" + lastName + "GPA: \t" + gpa+ "Age: \t" + age;
    }

    public double getGPA()
    {
        return gpa;
    }
}

为了使该方法起作用,您应该按照下面的步骤进行操作。

public Student bestStudent() {
    Node current = list;
    Student bestStudent = null;
    while(current.next != null) {
        if(bestStudent == null || (current.next != null && (current.data.getGPA() > current.next.data.getGPA()))) {
            bestStudent = current.data;
        }
    }
    return bestStudent;
}