You will be given an interface List representing the List ADT, which extends Iterable. Your LinkedList class will have to implement this interface. To create the LinkedList, you will also need to create a Node class and a custom iterator class, LinkedListIterator.
Important: Do NOT return objects of type Object or use raw types instead of generic types. An example of a raw type is ArrayList as opposed to ArrayList< String >.
This interface defines the List ADT and will contain methods that your LinkedList class must implement. The JavaDocs in this file will contain more information on the following methods you must implement:
Make sure you read the JavaDocs to see information on when to throw exceptions and what to return for edge cases.
This interface also extends the Iterable interface, so you must also implement the iterator() method. Refer to the Java API documentation for more information on this interface.
Note: You do NOT need to implement the forEach() and spliterator() methods of the Iterable interface for this assignment.
This class will represent an individual node in your LinkedList. Most implementations of a LinkedList node only have two instance variables:
Ensure that you follow the principle of encapsulation and write getters and setters where necessary. You may write constructors to make your code more concise.
In order to successfully implement the Iterable interface (which the provided List interface extends from), your LinkedList class must provide an implementation for the iterator() method. This method returns an instance of an Iterator for your data type. To successfully do this, we will create our own Iterator, LinkedListIterator, which implements the Iterator interface. An iterator is an object that allows you to loop over objects in a custom collection you create, such as our LinkedList. Note that this also needs to be a generic class, since it should iterate over a LinkedList of any type. This class will need to implement the following methods:
Hint: What instance variables are needed? What arguments may the constructor take in to be able to iterate over a LinkedList?
This is the main file for your LinkedList and will utilize the previous two classes to satisfy the requirements of the List ADT. This class will implement all the methods from the List interface (including the iterator() method inherited from the Iterable interface). This class also needs to be generic.
Instance variables:
Methods:
In addition to the methods from the List interface, implement the following methods:
import java.util.NoSuchElementException;
/**
* List Abstract Data Type.
*/
public interface List< E > extends Iterable< E > {
/**
* Inserts the element at the front of the list.
*
* @param element data we are adding to the list
* @throws IllegalArgumentException if the passed in element is null
*/
void add(E element) throws IllegalArgumentException;
/**
* Inserts the element at the specified index.
*
* @param index index to add the element
* @param element data we are adding to the list
* @throws IllegalArgumentException if the passed in element is null
* @throws IndexOutOfBoundsException if the passed in index is invalid. index == list.size() is valid
*/
void add(int index, E element) throws IllegalArgumentException, IndexOutOfBoundsException;
/**
* Removes the element at the specified index and returns it.
*
* @param index index of element to be removed
* @return the removed element at the specified index
* @throws IndexOutOfBoundsException if the passed in index is invalid
*/
E remove(int index) throws IndexOutOfBoundsException;
/**
* Removes the specified element from the list and returns it.
*
* @param element data to be removed
* @return the removed element from the list
* @throws NoSuchElementException if the passed in element is not in the list
*/
E remove(E element) throws NoSuchElementException;
/**
* Removes the head (first element) from the list and returns it.
*
* @return the removed element from the head of the list, null if the list is empty
*/
E remove();
/**
* Returns the element at the specified index.
*
* @param index index of the element to return
* @return element at the specified index
* @throws IndexOutOfBoundsException if the passed in index is invalid
*/
E get(int index) throws IndexOutOfBoundsException;
/**
* Checks if a list contains a specific element.
*
* @param element data to check for in list
* @return boolean representing if the list has the element or not
*/
boolean contains(E element);
/**
* Replaces the element at a specific index with the passed in data.
*
* @param index index of the element to replace
* @param element the element to set
* @return element that has been replaced
* @throws IndexOutOfBoundsException if the passed in index is invalid
* @throws IllegalArgumentException if the passed in element is null
*/
E set(int index, E element) throws IndexOutOfBoundsException, IllegalArgumentException;
/**
* Clears the list.
*/
void clear();
/**
* Checks if the list is empty.
*
* @return true if the list is empty, false otherwise
*/
boolean isEmpty();
/**
* Returns the number of elements in the list.
*
* @return int representing size of list
*/
int size();
}