繁体   English   中英

以节点为参数的Java链接列表递归

[英]Linked List Recursion in Java with Node as Parameter

对于一个类项目,我构建了一个自定义链表,用于维护类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;      
    }      
  }
}

要传递头节点,您只需要传递哪个节点是链表中的第一个节点,在这种情况下它是s5。 但是你接受一个学生对象并在它周围创建一个节点然后将它添加到你的列表中,目前唯一的节点是list,它是一个驻留在列表数据结构中的私有节点对象。 为了使用这种方法,你必须向它传递一个对你头部的引用,但因为它是私有的,所以无法从main那里做到这一点。 您可以将列表公开并执行以下操作

list1.countNodesRec(list1.list);

如果您不想公开Node列表对象,我建议您创建包含Students的Node对象,并将它们传递给LinkedList结构。 你也可以做以下事情

list1.countNodesRec(new Node(s5));

主要问题是你需要一个countNodesRec的参数,但是LinkedList类有一个对它的头的引用,所以当它已经包含在你的LinkedList结构(名为Node的节点)中时,不需要传递头。

我不确定为什么你在这个例程中使用两个节点; 由于没有代码,我不知道什么功能需要这种二元性。

list1本身是对列表头部节点的引用。 您在代码中重复使用该属性。 您应该能够仅使用列表调用该方法。 当你重复时,你会在this.next上这样做,这是列表其余部分的头部。

节点与列表头没有什么特别之处:每个节点都是列表的头部,它继续下一个引用。

这会为你清理结构吗?

您需要在LinkedList类中使用getList()方法。 然后在测试类中,您应该相应地使用该方法。 示例:list1.countNodesRec(list1.getList())

由于列表的头部是私有字段,我将添加一个无参数的方法:

public int countNodesRec() {
    return countNodesRec(list);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM