Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Your implementation of a circular singly linked list.
- *
- * @author Bhanu grg
- * @userid bgarg6
- * @GTID 903444695
- * @version 1.0
- */
- public class SinglyLinkedList<T> {
- // Do not add new instance variables or modify existing ones.
- private LinkedListNode<T> head;
- private int size;
- /**
- * Adds the element to the index specified.
- *
- * Adding to indices 0 and {@code size} should be O(1), all other cases are
- * O(n).
- *
- * @param index the requested index for the new element
- * @param data the data for the new element
- * @throws java.lang.IndexOutOfBoundsException if index is negative or
- * index > size
- * @throws java.lang.IllegalArgumentException if data is null
- */
- public void addAtIndex(int index, T data) {
- if (index < 0 || index > this.size) {
- throw new IndexOutOfBoundsException("the index provided is "
- + "outside the bounds of the list");
- }
- if (data == null) {
- throw new IllegalArgumentException("the data "
- + "provided does not exist, is null");
- }
- if (size == 0) {
- LinkedListNode<T> replaceHead = new LinkedListNode<>(data, head);
- head = replaceHead;
- }
- if (size > 0) {
- LinkedListNode<T> temp = head;
- for (int i = 0; i < index; i++) {
- if (i > 0) {
- temp = temp.getNext();
- }
- }
- LinkedListNode<T> input = new LinkedListNode<>(data, temp.getNext());
- temp.setNext(input);
- }
- this.size++;
- }
- /**
- * Adds the element to the front of the list.
- *
- * Must be O(1) for all cases.
- *
- * @param data the data for the new element
- * @throws java.lang.IllegalArgumentException if data is null
- */
- public void addToFront(T data) {
- if (data == null) {
- throw new IllegalArgumentException("the data provided"
- + "does not exist, is null");
- }
- if (size == 0) {
- LinkedListNode<T> replaceHead = new LinkedListNode<>(data, head);
- head = replaceHead;
- this.size++;
- } else {
- LinkedListNode<T> input = new LinkedListNode<>(data, head.getNext());
- head.setNext(input);
- input.setData(head.getData());
- head.setData(data);
- this.size++;
- }
- }
- /**
- * Adds the element to the back of the list.
- *
- * Must be O(1) for all cases.
- *
- * @param data the data for the new element
- * @throws java.lang.IllegalArgumentException if data is null
- */
- public void addToBack(T data) {
- if (data == null) {
- throw new IllegalArgumentException("the data provided"
- + "does not exist, is null");
- }
- if (size == 0) {
- LinkedListNode<T> replaceHead = new LinkedListNode<>(data, head);
- head = replaceHead;
- this.size++;
- } else {
- LinkedListNode<T> temp = head;
- for (int i = 0; i < size - 1; i++) {
- temp = temp.getNext();
- }
- // while (temp.getNext() != head) {
- // temp = temp.getNext();
- // }
- LinkedListNode<T> input = new LinkedListNode<>(data, head);
- temp.setNext(input);
- this.size++;
- }
- //could also try add at index
- }
- /**
- * Removes and returns the element from the index specified.
- *
- * Removing from index 0 should be O(1), all other cases are O(n).
- *
- * @param index the requested index to be removed
- * @return the data formerly located at index
- * @throws java.lang.IndexOutOfBoundsException if index is negative or
- * index >= size
- */
- public T removeAtIndex(int index) {
- if (index < 0 || index >= this.size) {
- throw new IndexOutOfBoundsException("the index provided"
- + "is outside the bounds of the list");
- }
- LinkedListNode<T> temp = head;
- for (int i = 0; i < index - 1; i++) {
- temp = temp.getNext();
- }
- LinkedListNode<T> retData = temp.getNext();
- temp.setNext(temp.getNext().getNext());
- this.size--;
- return retData.getData();
- }
- /**
- * Removes and returns the element at the front of the list. If the list is
- * empty, return {@code null}.
- *
- * Must be O(1) for all cases.
- *
- * @return the data formerly located at the front, null if empty list
- */
- public T removeFromFront() {
- LinkedListNode<T> retData = head;
- head.setData(head.getNext().getData());
- head.setNext(head.getNext().getNext());
- this.size--;
- return retData.getData();
- }
- /**
- * Removes and returns the element at the back of the list. If the list is
- * empty, return {@code null}.
- *
- * Must be O(n) for all cases.
- *
- * @return the data formerly located at the back, null if empty list
- */
- public T removeFromBack() {
- LinkedListNode<T> temp = head;
- for (int i = 0; i < size - 2; i++) {
- temp = temp.getNext();
- }
- LinkedListNode<T> retData = temp.getNext();
- temp.setNext(head);
- this.size--;
- return retData.getData();
- }
- /**
- * Removes the last copy of the given data from the list.
- *
- * Must be O(n) for all cases.
- *
- * @param data the data to be removed from the list
- * @return the removed data occurrence from the list itself (not the data
- * passed in), null if no occurrence
- * @throws java.lang.IllegalArgumentException if data is null
- */
- public T removeLastOccurrence(T data) {
- if (data == null) {
- throw new IllegalArgumentException("the data provided"
- + "does not exist, is null");
- }
- LinkedListNode<T> temp = head.getNext();
- if (head.getData() == data) {
- return removeFromFront();
- }
- int counter = 1;
- while (temp.getData() != data) {
- if (temp == head) {
- return null;
- }
- if (temp.getData() == data) {
- return removeAtIndex(counter);
- }
- temp = temp.getNext();
- counter++;
- }
- return null;
- }
- /**
- * Returns the element at the specified index.
- *
- * Getting index 0 should be O(1), all other cases are O(n).
- *
- * @param index the index of the requested element
- * @return the object stored at index
- * @throws java.lang.IndexOutOfBoundsException if index < 0 or
- * index >= size
- */
- public T get(int index) {
- if (index < 0 || index >= this.size) {
- throw new IndexOutOfBoundsException("the index provided"
- + "is outside the bounds of the list");
- }
- LinkedListNode<T> temp = head;
- if (index == 0) {
- return temp.getData();
- }
- for (int i = 0; i < index; i++) {
- temp.getNext();
- }
- return temp.getData();
- }
- /**
- * Returns an array representation of the linked list.
- *
- * Must be O(n) for all cases.
- *
- * @return an array of length {@code size} holding all of the objects in
- * this list in the same order
- */
- public Object[] toArray() {
- T[] arr = (T[]) new Object[this.size];
- LinkedListNode<T> temp = head;
- arr[0] = head.getData();
- int counter = 0;
- while (temp.getNext() != head) {
- temp = temp.getNext();
- ++counter;
- arr[counter] = temp.getData();
- }
- return arr;
- }
- /**
- * Returns a boolean value indicating if the list is empty.
- *
- * Must be O(1) for all cases.
- *
- * @return true if empty; false otherwise
- */
- public boolean isEmpty() {
- if (head == null) {
- return true;
- }
- return false;
- }
- /**
- * Clears the list of all data.
- *
- * Must be O(1) for all cases.
- */
- public void clear() {
- head = null;
- this.size = 0;
- }
- /**
- * Returns the number of elements in 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;
- }
- /**
- * Returns the head node of the linked list.
- *
- * For grading purposes only. You shouldn't need to use this method since
- * you have direct access to the variable.
- *
- * @return node at the head of the linked list
- */
- public LinkedListNode<T> getHead() {
- // DO NOT MODIFY!
- return head;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement