对于一个类项目,我构建了一个自定义链表,用于维护类Student的Node对象。 在一个方法int countNodesRec(Node list)中,我必须传递一个Node对象作为参数。将我的Student对象添加到链接列表时,它们将作为Node对象存储在链接列表中。但是,我不知道如何将头节点对象作为参数传递,因此我可以递归遍历链表。此外,我不需要帮助该方法的代码。顺便提一下,我的课程的设计完全符合项目的规定。谢谢你的帮助!
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;
}
}
}
答案 0 :(得分:0)
要传递头节点,您只需要传递哪个节点是链表中的第一个节点,在这种情况下它是s5。但是你接受一个学生对象并在它周围创建一个节点然后将它添加到你的列表中,目前唯一的节点是list,它是一个驻留在列表数据结构中的私有节点对象。为了使用这种方法,你必须向它传递一个对你头部的引用,但因为它是私有的,所以无法从main那里做到这一点。您可以公开列表并执行以下操作
list1.countNodesRec(list1.list);
如果您不想公开节点列表对象,我建议您创建包含Students的Node对象,并将它们传递给您的LinkedList结构。你也可以做以下
list1.countNodesRec(new Node(s5));
主要问题是你需要一个countNodesRec参数,但是LinkedList类引用了它的头部,因此当它已经包含在你的LinkedList结构中时,不需要传递头部,节点命名列表。
答案 1 :(得分:0)
我不确定为什么你在这个例程中使用两个节点;由于没有代码,我不知道哪种功能需要这种二元性。
list1 本身是对列表头部节点的引用。您在代码中重复使用该属性。您应该只能使用列表来调用该方法。当您再次出现时,可以在 this.next 上执行此操作,该列表是列表 rest 的头部。
节点与列表头没有什么特别之处:每个节点都是列表的头部,继续其下一个引用。
这会为你清理结构吗?
答案 2 :(得分:0)
您的LinkedList类中需要一个getList()方法。然后在测试类中,您应该相应地使用该方法。 示例:list1.countNodesRec(list1.getList())
答案 3 :(得分:0)
由于列表的头部是私有字段,我将添加一个无参数的方法:
public int countNodesRec() {
return countNodesRec(list);
}