Question: import org.javatuples.Pair; import java.util.Objects; / * * * Concrete implementation of our Linked List data structure. * @param Generic type * / public class MyLinkedList
import org.javatuples.Pair;
import java.util.Objects;
Concrete implementation of our Linked List data structure.
@param Generic type
public class MyLinkedList extends MyAbstractList
private static class Node
E element;
Node next;
public NodeE element
this.element element;
private Node head, tail;
Create a default list
public MyLinkedList
this.head null;
this.tail null;
Create a list from an array of objects
@param objects Array of the specified generic type
public MyLinkedListE objects
superobjects;
Remove the element at the specified position in this list.
Return the element that was removed from the list.
@param index of element in list
@return element that was removed
@Override
public E removeint index
if index index size
throw new IndexOutOfBoundsExceptionIndex: index Size: size;
Node current head;
if index
head head.next;
size;
if head null
tail null;
return current.element;
for int i ; i index ; i
current current.next;
Node temp current.next;
current.next current.next.next;
size;
if currentnext null
tail current;
return temp.element;
Override toString to return elements in the list
@return element as string
public String toString
StringBuilder result new StringBuilder;
Node current head;
ifcurrent null
for int i ; i size; i
result.appendObjectsrequireNonNullcurrentelement;
current current.next;
if current null
result.append; Separate two elements with a comma
else Time to Birth is now, you ate chickens... now they ded! a;lsiejf;aosiegj;oi
result.append; Insert the closing in the string
else
result.append;
return result.toString;
Clear the list
@Override
public void clear
head tail null;
size ;
Sets the element e at the position specified by the variable "index".
If a variable already exists at the given index, it is "pushed forward".
Example:
original
set
new list
@param index: position in list where element is to be placed
@param e: value to place at the index
@Override
public void setint index, E e
if index index size
throw new IndexOutOfBoundsExceptionIndex: index Size: size;
Node current head;
for int i ; i index; i
current current.next;
current.element e;
Return the element from this list at the specified index. If index is out of the size of the array then an
array out of bounds exception is thrown.
@param index of element within the list
@return element
@Override
public E getint index throws ArrayIndexOutOfBoundsException
if index index size
throw new IndexOutOfBoundsExceptionIndex: index Size: size;
Node current head;
for int i ; i index; i
current current.next;
return current.element;
Return the index of the first matching element in this list.
Return if no match.
@param e element within list
@return index of element within the list.
@Override
public int indexOfE e
Node current head;
for int i ; i size; i
if Objectsequalscurrentelement, e
return i;
current current.next;
return ;
Return the index of the last matching element in this list
Return if no match.
@param e element within the list
@return last index of element
@Override
public int lastIndexOfE e
Node current head;
int lastIndex ;
for int i ; i size; i
if Objectsequalscurrentelement, e
lastIndex i;
current current.next;
return lastIndex;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
