1

for a class project I built a custom linked list that maintains Node objects of class Student. In one method, int countNodesRec(Node list), I must pass a Node object as a parameter. While adding my Student objects to the linked list, they are stored as Node objects in the linked list. However, I have no idea how to pass the head node object as the parameter so I may recursively traverse through the linked list. Also, I don't need help with the code for that method. My classes are designed exactly as specified by the project, by the way. Thank you for the help!

TestList Class:

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

Student Class:

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 Class:

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

4 Answers 4

0

I'm not sure why you're using two nodes for this routine; since there's no code, I have no idea what functionality requires that duality.

list1 itself is a reference to the head node of the list. You use that property repeatedly in your code. You should be able to call the method with merely list. When you recur, you do so on this.next, which is the head of the rest of the list.

There's nothing special about a node versus a list head: every node is the head of a list that continues with its next reference.

Does that clear up the structure for you?

Sign up to request clarification or add additional context in comments.

5 Comments

I can call the method as list1.countNodesRec(Node list). I just don't know how to pass a Node object which is the head of list1 in that method as a parameter.
You already have the head node! It's list1, which appears as this inside the method. If you insist on two references, you could call it with list1.countNodesRec(list1), but that's redundant and dangerous. list1 is a Node object.
... or do you perhaps want a more direct reference with list1.list? Again, that's dangerous, passing a pair of tightly-related pointers into a routine, but allowing them to appear as unrelated.
Pardon me, but I tried calling it as list1.countNodesRec(list1), but I'm now getting the error "LinkedList cannot be converted to LinkedList.Node
Did you try it then with list1.list? That passes a Node, rather than a LinkedList. Still, what is it that you're trying to do in the routine that requires two list references?
0

To pass the head node you just need to pass which ever node is the first node in your linkedlist, in this case it's s5. But you take in a student object and create a node around it then add it to your list, currently the only node is list which is a private node object that resides in your list data structure. in order to use the method you would have to pass it a reference to your head, but because it's private there is no way to do that from main. you can make list public and do the following

list1.countNodesRec(list1.list);

If you don't want to make the Node list object public I suggest you create Node objects with Students inside and pass those to your LinkedList structure. you can also do the following

list1.countNodesRec(new Node(s5));

The main issue is you're requiring a parameter for countNodesRec but the class LinkedList has a reference to its head so there is no need to pass the head when it's already contained within your LinkedList structure, the Node named list.

1 Comment

That doesn't work because s5 is a student object. I get an error saying that Student cannot be converted to LinkedList.Node
0

You need a getList() method in your LinkedList class. Then in the test class you should use that method accordingly. example: list1.countNodesRec(list1.getList())

Comments

0

As the head of the list is a private field, I would add a parameterless method:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.