2017-04-10 88 views
0

我有一個自定義鏈接列表,它具有Student類的節點對象。有兩種遞歸方法稱爲countNodesRec(節點列表)& worstStudentRec(節點列表),它們都需要節點對象作爲參數。如何傳遞節點作爲參數

list1.worstStudentRec(?????)

list1.countNodesRec(????)

參數我想已經是給我的錯誤

  • list1.list
  • list1

不知道該放什麼,請幫忙!

測試類

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(); 

    //1 
    list1.addFront(s1); 
    list1.addFront(s2); 
    list1.addFront(s3); 
    list1.addFront(s4); 
    list1.addFront(s5); 
    list1.printLinkedList(); 
    System.out.println("Worst Student" + list1.worstStudentRec()); 
    System.out.println("Number of Students" + list1.countNodesRec()); 

    }   
} 

學生班級

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; 
    } 
    } 
    public String toString() 
    { 
     return lastName + "\t" + gpa + "\t" + age; 
    } 
    public double getGpa() 
    { 
     return gpa; 
    } 
} 

鏈表類

public class LinkedList 
{ 
    private class Node 
    { 
    public Student data; 
    public Node next; 
    public Node(Student s) 
    { 
     data = s; 
     next = null;  
    }  
    } 
    private Node list; 
    public LinkedList() 
    { 
    list = null; 
    } 
    public Student bestStudent() 
    { 
    Student bestStudent, bstStu; 
    Node current; 
    if (list == null) 
    { 
     return bestStudent = null; 
    } 
    else 
    { 
     current = list; 
     bstStu = new Student("", 0.00, 0); 
     while (current != null) 
     { 
     if (bstStu.getGpa() <= current.data.getGpa()) 
     { 
      bstStu = current.data; 
     } 
     current = current.next; 
     } 
     bestStudent = bstStu; 
    } 
    return bestStudent; 
    } 
    public int countNodesRec(Node list) 
    { 
    if(list == null) 
    { 
     return 0; 
    } 
    else 
    { 
     return 1 + countNodesRec(list.next); 
    } 
    } 
    public Student worstStudentRec(Node list) 
    { 
    if (list == null) 
    { 
     return null; 
    } 
    else if (list.next == null) 
    { 
     return list.data; 
    } 
    Student worstStudent = worstStudentRec(list.next); 
    return (list.data.compareTo(worstStudent) <= 0) ? list.data : worstStudent; 
    } 

} 
+0

list是你的LinkedList類中的一個私有字段 –

+0

可以讓一個getter或者讓列表公開在LinkedList類之外使用它 –

+1

這個問題的答案仍然取決於你回答我[你的這個問題] (http://stackoverflow.com/questions/43311029/implementing-singly-linked-list-methods)。爲什麼當你忽略調用對象時這是一個類方法?呼叫的語義是什麼?我懷疑你應該沒有參數**節點列表**,並使用調用對象(** this **)來代替。 – Prune

回答

0

可以在鏈表類創建的GetList方法,然後調用該方法主要:

list1.worstStudentRec(list1.getList()) 

list1.countNodesRec(list1.getList())