removes the Student associated with this id; if the id is not found in the table or on the waitlist, then it should return null; otherwise, it should return the Student associated with the id. If the student that is removed was registered, then this student should be replaced by the student who is first in the waitlist queue. If the student who is removed was on the waitlist, then they should just be removed from the waitlist.
Need help debugging this method: remove(int id)
-
removes the Student associated with this id; if the id is not found in the table or on the waitlist, then it should return null; otherwise, it should return the Student associated with the id.
If the student that is removed was registered, then this student should be replaced by the student who is first in the waitlist queue. If the student who is removed was on the waitlist, then they should just be removed from the waitlist.
public class Course {
public String code;
public int capacity;
public SLinkedList<Student>[] studentTable;
public int size;
public SLinkedList<Student> waitlist;
public Course(String code) {
this.code = code;
this.studentTable = new SLinkedList[10];
this.size = 0;
this.waitlist = new SLinkedList<Student>();
this.capacity = 10;
}
public Course(String code, int capacity) {
this.code = code;
this.studentTable = new SLinkedList[capacity];
this.size = 0;
this.waitlist = new SLinkedList<>();
this.capacity = capacity;
}
public Student remove(int id) {
int m = studentTable.length;
int slot = id % m;
for (int i = 0; i < studentTable[slot].size (); i++) {
if (studentTable[slot].get (i) == id) {
studentTable[slot].add (i, waitlist.getFirst ());
return null;
}
}
for (int i = 0; i < waitlist.size (); i++) {
if (waitlist.get (i).id == id) {
waitlist.remove (waitlist.get (i));
return null;
}
}
}
Step by step
Solved in 2 steps