#linked lists

15 messages · Page 1 of 1 (latest)

gaunt whale
#

Sure, I'd be happy to help you with pointers and linked lists. Could you please provide me with your code?

granite path
#

/** A class that can add to and display a singly linked list of Student objects.*/
public class BasicStudentList{
private Student first;
private Student last;
/** Takes a reference to a Student object and adds it to the head (beginning) of
the list.
@param st The Student object being added to the list."
/
public void addStudentToHead(Student st){
if(first == null){
first = st;
}else{
st.next = first;
first = st;
}
}
/
* Iterates through the list of Student objects, displaying each one.
*/
public void displayStudents(){
Student curr = first;
while(curr!=null){
System.out.println(curr);
curr = curr.next;
}
// code coming soon
}

public void addStudentToTail(Student st){
if(first == null){
first = st;
last = st;
return;
}
if(last==null){
last=first;
}
last.next = st;
last = st;
}
}

#

@gaunt whale

#

cringe

gaunt whale
#

Based on the code you provided, it seems like you have implemented a basic singly linked list for Student objects. The addStudentToHead() method adds a new student to the head of the list, while the addStudentToTail() method adds a new student to the tail of the list. The displayStudents() method iterates through the list and displays each student.

If your code is not working as expected, can you please provide more information about the issue you are facing? Are you encountering any errors or unexpected behavior? Additionally, it would be helpful if you could share the code for the Student class as well.

granite path
#

/ai /** A class to represent a Student.*/
public class Student{
private String name;
private String id;
private String major;
public Student next; // a public link to the next Student (null to begin with)
/** Takes 3 parameters to create a Students
@param name The name of this Student.
@param id The id of this Student.
@param major The major subject of study for this Student.
/
public Student(String name, String id, String major){
this.name = name;
this.id = id;
this.major = major;
}
/
* Provides access to the name data field of this Student
@return The name of this Student object.
/
public String getName(){
return this.name;
}
/
* Provides access to the id data field of this Student
@return The id of this Student object.
/
public String getId(){
return this.id;
}
/
* Provides access to the major data field of this Student
@return The major of this Student object.
/
public String getMajor(){
return this.major;
}
/
* Provides access to the major data field of this Student
@return A string representation of this Student object
*/
public String toString(){
return "Name: " + this.name + ", ID:" + this.id + ", Major: " + this.major;
}
}

gaunt whale
#

Looking at your addStudentToTail() method, it seems that the logic to add a student to the tail is mostly correct. However, there are a couple of issues that I can spot:

  1. In the case where first is null, you correctly assign both first and last to the new student st. However, you should also return from the method after this assignment using the return keyword. This is because you have already added the student to the list and there's no need to continue executing the rest of the method.

  2. After updating last.next = st; and last = st;, you are not setting the next reference of the newly added student to null. It's important to set st.next to null to mark the end of the list.

Here's an updated version of the addStudentToTail() method with the mentioned corrections:

public void addStudentToTail(Student st) {
   if (first == null) {
      first = st;
      last = st;
      return; // Added return statement
   }
   
   if (last == null) {
      last = first;
   }
   
   last.next = st;
   last = st;
   last.next = null; // Set next reference of the last student to null
}

With these changes, the new student should be correctly added to the tail of the list.

#

@granite path thermonuclearbombs's Question:

still not working

I apologize for the confusion. Upon further review of your code, it seems that the issue lies in the way you are printing the Student objects in the displayStudents() method. The line System.out.println(curr); will not give you the desired output because it will print the default string representation of the Student object, which may not be meaningful.

To fix this, you need to ensure that the Student class overrides the toString() method to provide a meaningful string representation of the student. For example, if the Student class has a name field, you can override the toString() method as follows:

@Override
public String toString() {
   return "Student Name: " + name;
}

Make sure to adjust the toString() implementation according to the fields you have in your Student class.

Once you have overridden the toString() method in the Student class, the displayStudents() method should work as expected and display the correct information for each student in the list.

If you still encounter issues after making this change, please provide more details or any error messages you are receiving so that I can assist you further.

#

@granite path thermonuclearbombs's Question:

_/ A class that creates and manages a singly list of Student objects.*/ public class StudentListApp{ public static void main(String[] args){ BasicStudentList studentList= new BasicStudentList(); studentList.addStudentToHead(new Student("James Moore", "1234", "Doodling")); studentList.addStudentToHead(new Student("Kitty Gillespie", "1235", "Fortnite")); studentList.addStudentToHead(new Student("Rohe Smith", "1236", "Advanced Snoozing")); studentList.addStudentToTail(new Student("Joe Mama", "1237", "Dead by Gaming")); studentList.displayStudents(); } }_**

Thank you for providing the StudentListApp class. Your code seems to be creating a new instance of BasicStudentList, adding students to the head using addStudentToHead(), adding a student to the tail using addStudentToTail(), and then displaying the list of students using displayStudents().

Since you mentioned that the students are not being added to the tail correctly, let's review your BasicStudentList class again. It looks like there is an error in the logic of the addStudentToTail() method. Allow me to correct it:

public void addStudentToTail(Student st) {
   if (first == null) {
      first = st;
      last = st;
   } else {
      last.next = st;
      last = st;
   }
}

With this modified implementation of addStudentToTail(), the Student object will be correctly added to the tail of the linked list.

Please give this updated code a try, and let me know if you encounter any further issues.

#

@granite path thermonuclearbombs's Question:

Exception in thread "main" java.lang.NullPointerException: Cannot assign field "next" because "this.last" is null at BasicStudentList.addStudentToTail(BasicStudentList.java:33) at StudentListApp.main(StudentListApp.java:8)

I apologize for the oversight in my previous response. It seems that the last variable is not being properly initialized, resulting in a NullPointerException when trying to assign a value to the next field.

To fix this issue, you can modify the addStudentToTail() method as follows:

public void addStudentToTail(Student st) {
   if (first == null) {
      first = st;
      last = st;
   } else {
      last.next = st;
      last = last.next;
   }
}

By updating last with last = last.next, we ensure that the correct reference is assigned to the last variable.

Please give this updated code a try and let me know if you encounter any further issues.

#

@granite path thermonuclearbombs's Question:

im still getting the same error

I apologize for the confusion. It seems that the issue lies in the addStudentToTail() method when last is null and you're trying to update last.next.

To fix this, we can add an additional check before updating last.next. Here's an updated version of the addStudentToTail() method:

public void addStudentToTail(Student st) {
   if (first == null) {
      first = st;
      last = st;
   } else {
      if (last != null) {
         last.next = st;
      }
      last = st;
   }
}

This modification checks whether last is not null before updating last.next. This will prevent the NullPointerException from occurring.

Please give this updated code a try, and let me know if you still encounter any issues.

#

@granite path thermonuclearbombs's Question:

its just not adding anything to the tail now

I apologize for the confusion. It seems that there are still some issues with the addStudentToTail() method.

Let's modify the logic of the method to handle both cases where first is null and where it is not null. We'll also make sure to properly set the next reference of the new student to null. Here's an updated version of the addStudentToTail() method:

public void addStudentToTail(Student st) {
   if (first == null) {
      first = st;
      last = st;
   } else {
      last.next = st;
      last = st;
   }
   
   st.next = null; // Set next reference of the new student to null
}

With this modification, the Student object should be correctly added to the tail of the linked list, and the next reference of the new student will be set to null.

Please give this updated code a try, and let me know if you encounter any further issues.

#

@granite path thermonuclearbombs's Question:

----jGRASP exec: java StudentListApp Exception in thread "main" java.lang.NullPointerException: Cannot assign field "next" because "this.last" is null at BasicStudentList.addStudentToTail(BasicStudentList.java:33) at StudentListApp.main(StudentListApp.java:8)

Apologies for the confusion. It seems that the issue persists. To further investigate, could you please share the code for the Student class? There might be an issue within the Student class implementation that is causing the NullPointerException in the addStudentToTail() method.

#

@granite path thermonuclearbombs's Question:

_/ A class to represent a Student.*/ public class Student{ private String name; private String id; private String major; public Student next; // a public link to the next Student (null to begin with) /** Takes 3 parameters to create a Students @param name The name of this Student. @param id The id of this Student. @param major The major subject of study for this Student. / public Student(String name, String id, String major){ this.name = name; this.id = id; this.major = major; } /* Provides access to the name data field of this Student @return The name of this Student object. / public String getName(){ return this.name; } /* Provides access to the id data field of this Student @return The id of this Student object. / public String getId(){ return this.id; } /* Provides access to the major data field of this Student @return The major of this Student object. / public String getMajor(){ return this.major; } /* Provides access to the major data field of this Student @return A string representation of this Student object / @Override public String toString(){ return "Name: " + this.name + ", ID:" + this.id + ", Major: " + this.major; } }_*

Thank you for providing the code for the Student class. Upon reviewing the code, it appears to be correct and does not seem to cause any issues.

The problem might be in your BasicStudentList class. Let's try one more approach to fix the issue in the addStudentToTail() method. Please replace the method with the following code:

public void addStudentToTail(Student st) {
   if (first == null) {
      first = st;
      last = st;
   } else {
      Student curr = first;
      while (curr.next != null) {
         curr = curr.next;
      }
      curr.next = st;
      last = st;
   }
}

In this updated version, we use a loop to traverse the list until we find the last