Write a linked list class(Java) to accept an object and one pointer to the next
ID: 3726213 • Letter: W
Question
Write a linked list class(Java) 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(offset) //removes an object from the arraylist using its offset. if your first object has the offset of 0, to remove the third object from the arraylist you need to pass 2 as offset.
Explanation / Answer
Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
LinkedList.java
=================
public class LinkedList {
class Node
{
Object data;
Node next;
Node(Object o)
{
data = o;
next = null;
}
}
private Node head, tail;
private int size;
public LinkedList()
{
head = null;
tail = null;
}
public boolean IsEmpty()
{
return head == null;
}
public void Add(Object o)
{
Node n = new Node(o);
if(IsEmpty())
head = tail = n;
else
{
tail.next = n;
tail = n;
}
size++;
}
public boolean IsIn(Object o)
{
Node curr = head;
while(curr != null)
{
if(curr.data.equals(o))
return true;
curr = curr.next;
}
return false;
}
public void Remove(Object o)
{
Node curr = head;
Node prev = null;
while(curr != null)
{
if(curr.data.equals(o))
break;
prev = curr;
curr = curr.next;
}
if(curr != null) //found
{
if(curr == head) //deleteing head node
{
head = head.next;
if(head == null)
tail = null;
}
else
{
prev.next = curr.next;
if(curr == tail)
tail = prev;
}
size--;
}
}
public void RemoveObj(int offset)
{
Node curr = head;
Node prev = null;
int index = 0;
while(curr != null && index < offset)
{
prev = curr;
curr = curr.next;
index++;
}
if(curr != null) //found
{
if(curr == head) //deleteing head node
{
head = head.next;
if(head == null)
tail = null;
}
else
{
prev.next = curr.next;
if(curr == tail)
tail = prev;
}
size--;
}
}
public int Size()
{
return size;
}
public Object get(int offset)
{
Node curr = head;
int index = 0;
while(curr != null && index < offset)
{
curr = curr.next;
}
if(curr != null) //found
return curr.data;
else
return null;
}
public void Print()
{
Node curr = head;
System.out.print("[");
if(curr != null)
{
System.out.print(curr.data);
curr = curr.next;
while(curr != null)
{
System.out.print(", " + curr.data);
curr = curr.next;
}
}
System.out.println("]");
}
}
TestLinkedList.java
=====================
public class TestLinkedList {
public static void main(String[] args) {
LinkedList list = new LinkedList();
System.out.println("Adding orange, apple, mango");
list.Add("orange");
list.Add("apple");
list.Add("mango");
System.out.print("List contains");
list.Print();
System.out.println("IsEmpty() = " + list.IsEmpty());
System.out.println("Removing mango");
list.Remove("mango");
System.out.print("List contains");
list.Print();
System.out.println("Adding grapes, banana");
list.Add("grapes");
list.Add("banana");
System.out.print("List contains");
list.Print();
System.out.println("size = " + list.Size());
System.out.println("Check if mango is in list: " + list.IsIn("mango"));
System.out.println("Check if grapes is in list: " + list.IsIn("grapes"));
System.out.println("Removing at index 2");
list.RemoveObj(2);
System.out.print("List contains");
list.Print();
}
}
output
======
Adding orange, apple, mango
List contains[orange, apple, mango]
IsEmpty() = false
Removing mango
List contains[orange, apple]
Adding grapes, banana
List contains[orange, apple, grapes, banana]
size = 4
Check if mango is in list: false
Check if grapes is in list: true
Removing at index 2
List contains[orange, apple, banana]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.