Import Java
Import Java
NoSuchElementException;
/**
* Your implementation of a non-circular SinglyLinkedList with a tail pointer.
*
* @author Sherry Shao
* @version 1.0
* @userid sshao40
* @GTID 903531823
*/
public class SinglyLinkedList<T> {
/**
* Adds the element to the specified index.
*
* Must be O(1) for indices 0 and size and O(n) for all other cases.
*
* @param index the index to add the new element
* @param data the data to add
* @throws java.lang.IndexOutOfBoundsException if index < 0 or index > size
* @throws java.lang.IllegalArgumentException if data is null
*/
public void addAtIndex(int index, T data) {
if (index == 0) {
addToFront(data);
} else if (index == size) {
addToBack(data);
} else {
SinglyLinkedListNode<T> node = head;
for (int i = 0; i < (index - 1); i++) {
node = node.getNext();
}
/**
* Adds the element to the front of the list.
*
* Must be O(1).
*
* @param data the data to add to the front of the list
* @throws java.lang.IllegalArgumentException if data is null
*/
public void addToFront(T data) {
if (data == null) {
throw new IllegalArgumentException("Cannot insert null data into linked list.");
}
if (size == 0) {
SinglyLinkedListNode<T> node = new SinglyLinkedListNode<>(data);
head = node;
tail = node;
} else {
SinglyLinkedListNode<T> node = new SinglyLinkedListNode<>(data);
node.setNext(head);
head = node;
}
size++;
}
/**
* Adds the element to the back of the list.
*
* Must be O(1).
*
* @param data the data to add to the back of the list
* @throws java.lang.IllegalArgumentException if data is null
*/
public void addToBack(T data) {
if (data == null) {
throw new IllegalArgumentException("Cannot insert null data into linked list.");
}
if (size == 0) {
SinglyLinkedListNode<T> node = new SinglyLinkedListNode<>(data);
node.setNext(head);
head = node;
tail = node;
} else {
SinglyLinkedListNode<T> node = new SinglyLinkedListNode<>(data);
tail.setNext(node);
tail = node;
}
size++;
}
/**
* Removes and returns the element at the specified index.
*
* Must be O(1) for indices 0 and size - 1 and O(n) for all other
* cases.
*
* @param index the index of the element to remove
* @return the data that was removed
* @throws java.lang.IndexOutOfBoundsException if index < 0 or index >= size
*/
public T removeAtIndex(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("Cannot remove data outside the bounds of (0, size].");
}
T nodeRemoved = head.getData();
if (index == 0) {
return removeFromFront();
} else {
SinglyLinkedListNode<T> node = head;
for (int i = 0; i < (index - 1); i++) {
node = node.getNext();
}
nodeRemoved = node.getNext().getData();
node.setNext(node.getNext().getNext());
size--;
}
return nodeRemoved;
}
/**
* Removes and returns the first data of the list.
*
* Must be O(1).
*
* @return the data formerly located at the front of the list
* @throws java.util.NoSuchElementException if the list is empty
*/
public T removeFromFront() {
if (size == 0) {
throw new NoSuchElementException("Cannot remove data from an empty list.");
}
T nodeFront = head.getData();
if (size == 1) {
head = null;
} else {
head = head.getNext();
}
size--;
return nodeFront;
}
/**
* Removes and returns the last data of the list.
*
* Must be O(n).
*
* @return the data formerly located at the back of the list
* @throws java.util.NoSuchElementException if the list is empty
*/
public T removeFromBack() {
if (size == 0) {
throw new NoSuchElementException("Cannot remove data from an empty list.");
}
T nodeBack = tail.getData();
if (size == 1) {
head = null;
tail = null;
} else {
SinglyLinkedListNode<T> node = head;
node.setNext(null);
tail = node;
}
size--;
return nodeBack;
}
/**
* Returns the element at the specified index.
*
* Must be O(1) for indices 0 and size - 1 and O(n) for all other cases.
*
* @param index the index of the element to get
* @return the data stored at the index in the list
* @throws java.lang.IndexOutOfBoundsException if index < 0 or index >= size
*/
public T get(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("Cannot remove data outside the bounds of (0, size].");
}
if (index == 0) {
return head.getData();
}
SinglyLinkedListNode<T> node = head;
for (int i = 0; i < index; i++) {
node = node.getNext();
}
return node.getData();
}
/**
* Returns whether or not the list is empty.
*
* Must be O(1).
*
* @return true if empty, false otherwise
*/
public boolean isEmpty() {
return size == 0;
}
/**
* Clears the list.
*
* Clears all data and resets the size.
*
* Must be O(1).
*/
public void clear() {
head = null;
tail = null;
size = 0;
}
/**
* Removes and returns the last copy of the given data from the list.
*
* Do not return the same data that was passed in. Return the data that
* was stored in the list.
*
* Must be O(n).
*
* @param data the data to be removed from the list
* @return the data that was removed
* @throws java.lang.IllegalArgumentException if data is null
* @throws java.util.NoSuchElementException if data is not found
*/
public T removeLastOccurrence(T data) {
if (data == null) {
throw new IllegalArgumentException("Cannot have null data in a linked list,"
+ "therefore cannot remove null data element.");
}
node = node.getNext();
}
if (index == -1) {
throw new NoSuchElementException("Data was not found in the linked list.");
}
T nodeRemoved = removeAtIndex(index);
return nodeRemoved;
}
/**
* Returns an array representation of the linked list.
*
* Must be O(n) for all cases.
*
* @return the array of length size holding all of the data (not the
* nodes) in the list in the same order
*/
public T[] toArray() {
if (size == 0) {
@SuppressWarnings("unchecked")
T[] list = (T[]) new Object[0];
return list;
}
@SuppressWarnings("unchecked")
T[] list = (T[]) new Object[size];
SinglyLinkedListNode<T> node = head;
if (size >= 1) {
int i = 0;
while (node.getNext() != null) {
list[i] = node.getData();
node = node.getNext();
i++;
}
list[i] = node.getData();
} else if (size == 1) {
list[0] = node.getData();
}
return list;
}
/**
* Returns the head node of the list.
*
* For grading purposes only. You shouldn't need to use this method since
* you have direct access to the variable.
*
* @return the node at the head of the list
*/
public SinglyLinkedListNode<T> getHead() {
// DO NOT MODIFY!
return head;
}
/**
* Returns the tail node of the list.
*
* For grading purposes only. You shouldn't need to use this method since
* you have direct access to the variable.
*
* @return the node at the tail of the list
*/
public SinglyLinkedListNode<T> getTail() {
// DO NOT MODIFY!
return tail;
}
/**
* Returns the size of the list.
*
* For grading purposes only. You shouldn't need to use this method since
* you have direct access to the variable.
*
* @return the size of the list
*/
public int size() {
// DO NOT MODIFY!
return size;
}
}