2017-04-10 1 views
0

클래스 노드의 노드 개체가있는 사용자 지정 연결 목록이 있습니다. 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()); 

    }   
} 

당신은 링크 된 목록 클래스의 getList 방법을 만들 수 있습니다

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를 만들거나 LinkedList 클래스 외부에서 사용하도록 목록을 공개로 만듭니다. –

+1

이 질문에 대한 대답은 [내 질문] (http://stackoverflow.com/questions/43311029/implementing-singly-linked-list-methods). 호출 객체를 무시할 때 이것이 클래스 메소드 인 이유는 무엇입니까? 통화의 의미는 무엇입니까? 나는 인자 ** 노드리스트 **가 없어야하고, 대신 호출 객체 (** this **)를 사용해야한다고 생각한다. – Prune

답변

0

학생 클래스

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; 
    } 
} 

링크 된 목록 클래스는 다음 주에 메소드를 호출합니다

list1.worstStudentRec(list1.getList()) 

list1.countNodesRec(list1.getList()) 
관련 문제