2017-04-10 16 views
0

私はクラスのクラスのノードオブジェクトを持つカスタムリンクリストを持っています。 countNodesRec(ノードリスト)& worstStudentRec(ノードリスト)という2つの再帰的メソッドがあり、どちらもノードオブジェクトをパラメータとして必要とします。ノードをパラメータとして渡す方法

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

リストはあなたのLinkedListクラスのプライベートフィールドです –

+0

getterを作成するか、リストをpublicにしてLinkedListクラスの外で使用します –

+1

この質問に対する回答は、[あなたのこの質問] (http://stackoverflow.com/questions/43311029/implementing-singly-linked-list-methods)。呼び出すオブジェクトを無視すると、これはクラスメソッドですか?コールのセマンティクスは何ですか?私は、引数** Node list **を持たず、代わりに呼び出しオブジェクト(** this **)を扱うべきだと思う。 – Prune

答えて

0

あなたはメインでメソッドを呼び出した後、リンクリストクラスでGETLISTメソッドを作成することができます。

list1.worstStudentRec(list1.getList()) 

list1.countNodesRec(list1.getList()) 
関連する問題