I need a Linked List with these methods. Comment if you need any details toStrin
ID: 3671208 • Letter: I
Question
I need a Linked List with these methods. Comment if you need any details
toString(),
public boolean equals(UnilinkedList list), to test list equality.
public boolean isEmpty(), to check whether or not the list is empty.
public void clear(), to remove all elements in this list.
public int size(), to return the size of this list.
public int indexOf(E element), to return the index of the given element if it is present in the list, otherwise this method should return 1.
public boolean contains(E element), to return true if the list contains the given element
public void add(E element), to add the given element at the end of the list.
public void addAfter(E mark, E elementToAdd), to insert the second argument in the list right after the mark. This method should throw an exception if the ‘mark’ is not present in the list.
public void addFirst(E element), to add the given element at the beginning of the list.
public E head(), return the first element of this list.
public boolean remove(E element), to remove the first occurrence of the given element from the list. If the element does not exist, it should return false.
public boolean removeAll(E element), to remove all occurrences of the given element from the list.
public void deduplicate(), to remove all duplicates from the list. The method should only retain the first occurrence of every element in this list.
Explanation / Answer
import java.io.*;
import java.util.*;
class Node<E>{
E value;
Node<E> next;
public Node(E n){
value = n;
next = null;
}
}
class UnilinkedList<E>{
Node<E> head;
public UnilinkedList(){
head = null;
}
public boolean isEmpty(){
if (head == null) return true;
return false;
}
public void clear(){
head = null;
}
public int size(){
Node<E> temp = head;
int size = 0;
while (temp != null){
temp = temp.next;
size += 1;
}
return size;
}
public int indexOf(E element){
Node<E> temp = head;
int index = -1;
while (temp != null && temp.value != element){
index += 1;
temp = temp.next;
}
return index;
}
public boolean contains(E element){
Node<E> temp = head;
while (temp != null){
if (temp.value == element) return true;
temp = temp.next;
}
return false;
}
public void add(E element){
Node temp = new Node(element);
Node temp_1 = head;
while (temp_1.next != null) temp_1 = temp_1.next;
temp_1.next = temp;
}
public void addAfter(E mark, E elementToAdd){
Node temp_1 = head;
while (temp_1 != null && temp_1.value != mark)
temp_1 = temp_1.next;
if (temp_1 == null){
System.out.println("CAN NOT BE INSERT, "+mark+" is not in the list");
return;
}
Node temp = new Node(elementToAdd);
temp.next = temp_1.next;
temp_1.next = temp;
}
public void addFirst(E element){
Node temp = new Node(element);
temp.next = head;
head = temp;
}
public E head(){
if (head == null) return null;
return head.value;
}
public boolean remove(E element){
Node curr = head;
Node prev = null;
while (curr != null && curr.value != element){
prev = curr;
curr = curr.next;
}
if (curr == null) return false;
prev.next = curr.next;
return true;
}
public boolean removeAll(E element){
Node temp_1 = head;
while (temp_1 != null){
if (temp_1.value == element){
if (remove(element) == false) return false;
}
temp_1 = temp_1.next;
}
return true;
}
}
class main{
public static void main(String[] args){
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.