Question: How would I make methods to add/remove students at a specific location in the linked list? I have given my 3 classes i made. StudentMain,
How would I make methods to add/remove students at a specific location in the linked list? I have given my 3 classes i made. StudentMain, StudentLinkedList, and Student
public class StudentMain {
static StudentLinkedList studentList;
public static void main(String[] args) {
Student student = new Student("John", "Doe", "doe1j");
studentList = new StudentLinkedList(student);
System.out.println(studentList.size());
student = new Student("John", "Deer", "deer1j");
studentList.add(student);
System.out.println(studentList.size());
System.out.println();
System.out.println(studentList.toString());// prints students in list
System.out.println();
System.out.print(student.getFname() + " ");// gets first name of student
System.out.println(student.getLname());// gets last name of student
}
}
public class Student {
private String fname;
private String lname;
private String id;
private long rnumber;
Student next; // this is the pointer to the next Student node in the list
Student(String FirstName, String LastName, String ID) {
fname = FirstName;
lname = LastName;
id = ID.toLowerCase();
rnumber = 0;
}
Student(String FirstName, String LastName, String ID, long RandomNumber) {
fname = FirstName;
lname = LastName;
id = ID.toLowerCase();
rnumber = RandomNumber;
}
public String getFname() {
return fname;
}
public String getLname() {
return lname;
}
public String getId() {
return id;
}
public long getRnumber() {
return rnumber;
}
public String toString() {
return fname + "," + lname + "," + id + "," + String.valueOf(rnumber);
}
public Student getNext() {
return next;
}
public void setNext(Student nextStudent) {
next = nextStudent;
}
}
public class StudentLinkedList {
private Student firstNode;
private static int listCounter;
// Constructor: must provide a "head" node for us
public StudentLinkedList(Student student) {
firstNode = student;
listCounter = 1;
}
// Returns number of elements in list to the outside
public int size() {
return listCounter;
}
public void add(Student student) {
Student tempNode = student;
Student currentNode = firstNode; // start at the beginning
while (currentNode.getNext() != null) { // not at the end yet, move node by node
currentNode = currentNode.getNext();
}
// the last node.next currently is null, needs to point to our newly added node
currentNode.setNext(tempNode);
// all done, now update our counter
listCounter++;
}
public void remove(Student student) {
Student lastNode = null;
Student currentNode = firstNode; // start at the beginning
while (currentNode.getNext() != null) { // not at the end yet, move node by node
lastNode = currentNode;
currentNode = currentNode.getNext();
}
lastNode.setNext(null);
listCounter--;// all done, now update our counter
}
@Override
public String toString() {
String dummyString = "";
Student currentNode = firstNode;
while (currentNode.getNext() != null) {
dummyString += currentNode.toString();
currentNode = currentNode.getNext();
}
return dummyString;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
