I need to impletement the removeElementAt method, help would be most appreciated
ID: 3630010 • Letter: I
Question
I need to impletement the removeElementAt method, help would be most appreciated! Method at the bottom!
import java.util.Iterator;
public class LinkedList<E> extends List<E> {
/**
* The Node<T> class implements nodes to be used in LinkedList<E>.
*/
protected static class Node<T> {
/**
* The element held in this Node.
*/
public T element;
/**
* A reference to the next Node in the list.
*/
public Node<T> next;
/**
* Constructs a Node.
* @param element the element held in the Node.
* @param next the next Node in the list.
*/
Node(T element, Node<T> next) {
this.element = element;
this.next = next;
}
}
/**
* A reference to the first element in the list.
*/
protected Node<E> start = null;
/**
* Returns an iterator over a set of elements of type E
* @return an iterator over a set of elements of type E
*/
@Override
public Iterator<E> iterator() {
return new Iterator<E>() {
Node<E> i = start;
@Override
public boolean hasNext() {
return i != null;
}
@Override
public E next() {
E e = i.element;
i = i.next;
return e;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
/**
* Inserts the specified element at the specified position in this List.
* while shifting the element currently at that position (if any)
* and any subsequent elements to the right (adds one to their indices).
*
* @param element the element to insert.
* @param index where to insert the new element.
*/
@Override
public void insertElementAt(E element, int index) {
if (index == 0) {
start = new Node<E>(element, start);
}
else {
Node<E> current = start;
for (int i = 0; i < index-1; i++) {
current = current.next;
}
current.next = new Node<E>(element, current.next);
}
}
/**
* Removes the element at the specified index.
* while shifting any subsequent elements to the left
* (subtracts one from their indices).
*
* @param index the index of the element to remove.
* @return the element previously at the specified position.
*/
@Override
public E removeElementAt(int index) {
return null;
}
}