我構建了一個自定義鏈表來維護類Student的Node對象。 在一個方法int countNodesRec(Node list)中,我必須傳遞一個Node對象作爲參數。將我的Student對象添加到鏈接列表中時,它們作爲節點對象存儲在鏈接列表中。但是,我不知道如何傳遞頭節點對象作爲參數,所以我可以遞歸遍歷鏈表。另外,我不需要該方法的代碼幫助。順便說一下,我的課程完全按照項目的規定進行設計。感謝您的幫助!鏈接列表Java中的節點作爲參數的遞歸
TestList類:
public class TestList {
public static void main(String[] args) {
Student s1 = new Student("Adams", 3.9, 26);
Student s2 = new Student("Lewis", 2.1, 29);
Student s3 = new Student("Lopez", 4.0, 53);
Student s4 = new Student("Smith", 3.2, 22);
Student s5 = new Student("Zeeler", 3.6, 38);
LinkedList list1 = new LinkedList();
LinkedList list2 = new LinkedList();
LinkedList list3 = new LinkedList();
//Adds students to LinkedLists and invokes methods to display their data
list1.addFront(s1);
list1.addFront(s2);
list1.addFront(s3);
list1.addFront(s4);
list1.addFront(s5);
list1.printLinkedList();
System.out.println("Best student: " + list1.bestStudent());
list1.countNodesRec(//Don't know how to pass Node);
}
}
學生類別:
public class Student
{
private String lastName;
private double gpa;
private int age;
public Student(String lastName, double gpa, int age)
{
this.lastName = lastName;
this.gpa = gpa;
this.age = age;
}
public int compareTo(Student s)
{
if (gpa < s.gpa)
return -1;
else if (gpa > s.gpa)
return 1;
else
return 0;
}
@Override
public String toString()
{
return lastName + "\t" + gpa + "\t" + age;
}
public double getGpa()
{
return gpa;
}
}
LinkedList類:
public class LinkedList
{
private Node list;
public LinkedList()
{
list = null;
}
// Adds a Node object to the front of the LinkedList
public void addFront(Student s)
{
if (list == null)
list = new Node(s);
else
{
Node temp = new Node(s);
// Assigns Node s next reference to the
// object at the beginning of the LinkedList
temp.next = list;
//Beginning of the list now equals Student s
list = temp;
}
}
// Adds a Node object to the back of the LinkedList
public void addTail(Student s)
{
Node node = new Node(s);
Node current;
if (list == null)
list = node;
else
{
current = list;
while (current.next != null)
current = current.next;
current.next = node;
}
}
public Student bestStudent()
{
Student bestStudent, bestStudentInner;
Node current;
if (list == null)
return bestStudent = null;
else
{
current = list;
bestStudentInner = new Student("base case", 0.00, 0);
while (current != null)
{
if (bestStudentInner.getGpa() <= current.data.getGpa())
bestStudentInner = current.data;
current = current.next;
}
bestStudent = bestStudentInner;
}
return bestStudent;
}
public void printLinkedList()
{
Node current;
if (list == null)
System.out.println("Empty");
else
{
current = list;
while (current != null)
{
System.out.println(current.data.toString());
current = current.next;
}
}
}
public int countNodesRec(Node list)
{
//To-do here;
}
public Student worstStudentRec(Node list)
{
//To-do here;
}
// Inner class that creates Nodes to be stored in LinkedList
private class Node
{
public Student data;
public Node next;
public Node(Student s)
{
data = s;
next = null;
}
}
}
這不起作用,因爲S5是學生對象。我收到一個錯誤,說學生不能轉換爲LinkedList.Node – JMarotta