Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA Write a linked list class to accept an object and one pointer to the next n

ID: 3726186 • Letter: J

Question

JAVA

Write a linked list class to accept an object and one pointer to the next node, add getters, setters and a print method.

Use this class to make a linked list and print it

And then add the following methods to the linked list class and test them inside your program:

Boolean IsIn(Object) // Tells you if an object is in the linked list or not by retiring true/false

Void Remove(Object)// A method that deletes the first object

boolen IsEmpty() // a method that will return true/false if the linked list is empty

Void removeObj(object, offset) //remove an object from the array list using its offset. If your first object has the offset of 0, to remove the third object from the array list you need to pass 2 as an offset.

Explanation / Answer

Solution:

code:

class Student

{

    String name;

    String id;

    Student next;

    Student(String name,String id)

    {

        this.name=name;

        this.id=id;

    }

    public String toString()

    {

        return "ID: "+id+", Name: "+name;

    }

}

StudentList.java

class StudentList {

Student first, last, next = null;

public void insert(Student newStud) {

if (first == null) {

first = newStud;

last = first;

} else {

last.next = newStud;

last = last.next;

}

}

public void print() {

Student current = first;

while (current != null) {

System.out.println(current.toString());

current = current.next;

}

}

public void remove(Student stud) {

Student current = first;

if (current.equals(stud)) {

first = current.next;

} else {

while (current.next.next != null) {

if (current.next.equals(stud)) {

current.next = current.next.next;

} else {

current = current.next;

}

}

}

if (current.next.equals(stud)) {

last = current;

last.next = null;

}

}

boolean IsIn(Student stud) {

Student s = first;

while (s != null) {

if (s == stud) {

return true;

}

s = s.next;

}

return false;

}

boolean IsEmpty(){

if(first==null)

return true;

return false;

}

}

//Demo.java

import java.util.*;

class Demo{

public static void main(String[] args){

StudentList list = new StudentList();

System.out.println(" Linked List Is Empty: "+list.IsEmpty());

Student std1 = new Student("Tester 1", "R092013");

Student std2 = new Student("Tester 2", "R092014");

list.insert(std1);

list.print();

System.out.println(" Checking Whether std2 is in list: "+list.IsIn(std2));

list.insert(std2);

list.print();

System.out.println(" Checking Whether std2 is in list: "+list.IsIn(std2));

list.remove(std1);

System.out.println(" After Removing std1: ");

list.print();

System.out.println(" Linked List Is Empty: "+list.IsEmpty());

}

}

I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)