diff --git a/IntListReview.iml b/IntListReview.iml index c90834f..0f07f6c 100644 --- a/IntListReview.iml +++ b/IntListReview.iml @@ -4,8 +4,25 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/ArrayIntList.java b/src/ArrayIntList.java new file mode 100644 index 0000000..de31b33 --- /dev/null +++ b/src/ArrayIntList.java @@ -0,0 +1,331 @@ +import java.util.Iterator; +import java.util.NoSuchElementException; + +public class ArrayIntList implements IntList +{ + private int size; + private int[] buffer; + + public ArrayIntList() + { + //initialize my fields + size = 0; + buffer = new int[10]; + } + /** + * Prepends (inserts) the specified value at the front of the list (at index 0). + * Shifts the value currently at the front of the list (if any) and any + * subsequent values to the right. + * + * @param value value to be inserted + */ + @Override + public void addFront(int value) + { + // start at 0, stop at the last element, increase by one + for (int i = size; i >= 0; i--) + { + //shift over to the right + buffer[i + 1] = buffer[i]; + } +// buffer[4] = buffer[3]; +// buffer[3] = buffer[2]; +// buffer[2] = buffer[1]; +// buffer[1] = buffer[0]; + + // put the value at the front of the array at position 0 + buffer[0] = value; + size++; + } + + /** + * Appends (inserts) the specified value at the back of the list (at index size()-1). + * + * @param value value to be inserted + */ + @Override + public void addBack(int value) + { + // TODO: check to see if we are full - if so, we need to create a larger buffer + if (size == buffer.length) + { + resize(size * 2); + } + + buffer[size] = value; + size += 1; + } + + /** + * Inserts the specified value at the specified position in this list. + * Shifts the value currently at that position (if any) and any subsequent + * values to the right. + * + * @param index index at which the specified value is to be inserted + * @param value value to be inserted + * @throws IndexOutOfBoundsException if the index is out of range + */ + @Override + public void add(int index, int value) + { + if (size == buffer.length) + { + resize(size * 2); + } + buffer[index] = value; + size++; + } + + /** + * Removes the value located at the front of the list + * (at index 0), if it is present. + * Shifts any subsequent values to the left. + */ + @Override + public void removeFront() + { + if (!isEmpty()) { + for (int i = 0; i < size; i++) + { + buffer[i] = buffer[i + 1]; + } + + //optional, but a good idea - since we shifted everything to the left by 1 + // we want to clear out the right-most value to be zero + buffer[size - 1] = 0; + + size--; + } + // for loop to shift elements to the left + // buffer[0] = buffer[1] + // buffer[1] = buffer[2] + + } + + /** + * Removes the value located at the back of the list + * (at index size()-1), if it is present. + */ + @Override + public void removeBack() + { + if (!isEmpty()) { + buffer[size - 1] = 0; + size--; + } + } + + /** + * Removes the value at the specified position in this list. + * Shifts any subsequent values to the left. Returns the value + * that was removed from the list. + * + * @param index the index of the value to be removed + * @return the value previously at the specified position + * @throws IndexOutOfBoundsException if the index is out of range + */ + @Override + public int remove(int index) + { + //first, check the index to see if it is valid + if (index < 0) { + throw new IndexOutOfBoundsException("Index cannot be negative"); + } + else if (index >= size) + { + throw new IndexOutOfBoundsException("Index is higher than size"); + } + + //save a copy of the value to be removed so we can return it later + int copyOfRemovedValue = buffer[index]; + + //shift values to the left + for (int i = index; index <= size - 1; index++) + { + buffer[i] = buffer[i + 1]; + } + + //reassign the last value of the size to 0 + buffer[size - 1] = 0; + + //don't forget to decrement size + size--; + + return copyOfRemovedValue; + } + + /** + * Returns the value at the specified position in the list. + * + * @param index index of the value to return + * @return the value at the specified position in this list + * @throws IndexOutOfBoundsException if the index is out of range + */ + @Override + public int get(int index) + { + return buffer[index]; + } + + /** + * Returns true if this list contains the specified value. + * + * @param value value whose presence in this list is to be searched for + * @return true if this list contains the specified value + */ + @Override + public boolean contains(int value) + { + for (int i = 0; i < size; i++) + { + if (buffer[i] == value) + { + return true; + } + } + return false; + } + + /** + * Returns the index of the first occurrence of the specified value + * in this list, or -1 if this list does not contain the value. + * + * @param value value to search for + * @return the index of the first occurrence of the specified value in this list + * or -1 if this list does not contain the value + */ + @Override + public int indexOf(int value) + { + if (isEmpty()) + { + return -1; + } + + //run through the array using a for loop + for (int i = 0; i < size; i++) + { + //if the buffer[] == value, return the index + if (buffer[i] == value) + { + return i; + } + } + //otherwise, return a -1 + return -1; + } + + /** + * Returns true if this list contains no values. + * + * @return true if this list contains no values + */ + @Override + public boolean isEmpty() + { + return size == 0; + } + + /** + * Returns the number of values in this list. + * + * @return the number of values in this list + */ + @Override + public int size() + { + return size; + } + + /** + * Removes all the values from this list. + * The list will be empty after this call returns. + */ + @Override + public void clear() + { + for (int i = 0; i < size; i++) + { + buffer[i] = 0; + } + //another way to do this + + //buffer = new int[10]; + //size = 0; + } + + public void resize(int newSize) + { + //create new space, separate from the old space (buffer) + int[] newBuffer = new int[newSize]; + + //copy everything over from buffer into newBuffer + for (int i = 0; i < buffer.length; i++) + { + newBuffer[i] = buffer[i]; + } + + // set the new space into buffer + buffer = newBuffer; + + //the old buffer space is no longer "pointed to" and will eventually + //be cleaned up by the garbage collector + } + + /** + * Returns an iterator over elements of type {@code T}. + * + * @return an Iterator. + */ + @Override + public Iterator iterator() + { + IntListIterator theIterator = new IntListIterator(); + return theIterator; + } + + //create a private helper Iterator class + private class IntListIterator implements Iterator { + //private fields: + private int i; + + public IntListIterator() + { + i = 0; + } + /** + * Returns {@code true} if the iteration has more elements. + * (In other words, returns {@code true} if {@link #next} would + * return an element rather than throwing an exception.) + * + * @return {@code true} if the iteration has more elements + */ + @Override + public boolean hasNext() + { + return i < size; + } + + /** + * Returns the next element in the iteration. + * + * @return the next element in the iteration + * @throws NoSuchElementException if the iteration has no more elements + */ + @Override + public Integer next() + { + if (i >= size) + { + throw new NoSuchElementException("i is now out of bounds"); + } + int currentValue = buffer[i]; + i++; + + return currentValue; + } + } + + // iterators are what enables main/client to use a for-each loop + // on my IntList +} diff --git a/src/ArrayIntListDriver.java b/src/ArrayIntListDriver.java new file mode 100644 index 0000000..cd81ded --- /dev/null +++ b/src/ArrayIntListDriver.java @@ -0,0 +1,95 @@ +import java.util.Iterator; + +//TIP To Run code, press or +// click the icon in the gutter. +public class ArrayIntListDriver +{ + public static void main(String[] args) { + //TIP Press with your caret at the highlighted text + // to see how IntelliJ IDEA suggests fixing it. + System.out.println("Hello and welcome!"); + + // create ArrayIntList + ArrayIntList testList = new ArrayIntList(); + + //add methods + testList.add(0, 1); + testList.add(1, 3); + testList.add(2, 5); + Iterator itr = testList.iterator(); + System.out.println("add():"); + printArray(testList, itr); + System.out.println(); + + System.out.println("addFront()"); + testList.addFront(6); + System.out.println("Front: " + testList.get(0)); + printArray(testList, itr); + + System.out.println(); + System.out.println("addBack()"); + testList.addBack(50); + printArray(testList, itr); + + System.out.println(); + System.out.println("removeFront()"); + testList.removeFront(); + printArray(testList, itr); + + System.out.println(); + System.out.println("removeBack()"); + testList.removeBack(); + printArray(testList, itr); + + System.out.println(); + System.out.println("remove()"); + testList.remove(1); + printArray(testList, itr); + + System.out.println(); + System.out.println("get()"); + System.out.println(testList.get(1)); + + System.out.println(); + System.out.println("contains()"); + System.out.println("Contains 10: " + testList.contains(10)); + System.out.println("Contains 1: " + testList.contains(1)); + + System.out.println(); + System.out.println("indexOf()"); + System.out.println("Index of 1: " + testList.indexOf(1)); + System.out.println("Index of 5: " + testList.indexOf(5)); + System.out.println("Index of 3: " + testList.indexOf(3)); + + System.out.println(); + System.out.println("isEmpty()"); + ArrayIntList emptyList = new ArrayIntList(); + System.out.println("emptyList: " + emptyList.isEmpty()); + System.out.println("testList: " + testList.isEmpty()); + + System.out.println(); + System.out.println("size()"); + System.out.println("Size of testList: " + testList.size()); + + System.out.println(); + System.out.println("resize()"); + System.out.println("currentList: " + testList.size()); + testList.resize(20); + for (int i = 0; i <= 20; i++) + { + testList.add(i, 4); + } + System.out.println(testList.size()); + } + + public static void printArray(ArrayIntList list, Iterator iterator) + { + System.out.print("testList = {"); + iterator = list.iterator(); + while (iterator.hasNext()) + { + System.out.print(iterator.next() + ", "); + } + System.out.println("}"); + } +} \ No newline at end of file diff --git a/src/DoublyLinkedIntList.java b/src/DoublyLinkedIntList.java new file mode 100644 index 0000000..69c63d3 --- /dev/null +++ b/src/DoublyLinkedIntList.java @@ -0,0 +1,468 @@ +import java.util.Iterator; +import java.util.NoSuchElementException; + +public class DoublyLinkedIntList implements IntList +{ + //private fields + int size; + + private class Node { + int data; + Node next; //address of the node "after" this one in line + Node last; //address of the node "before" this one in line + + public Node() { + next = null; + last = null; + } + } + + private Node head; + private Node tail; + + public DoublyLinkedIntList() { + // an empty list has 3 sentinel (dummy) nodes that serve as bookends + head = new Node(); + tail = new Node(); + head.next = tail; + tail.last = head; + size = 0; + } + + + /** + * Prepends (inserts) the specified value at the front of the list (at index 0). + * Shifts the value currently at the front of the list (if any) and any + * subsequent values to the right. + * + * @param value value to be inserted + */ + @Override + public void addFront(int value) + { + Node newNode = new Node(); + Node tempNode = head; + newNode.data = value; + + //assign head to new value if no value is there + if (head == null) + { + head = newNode; + } + else if (head.data == 0) + { + //assign the new node as the head + head = newNode; + //head as tail + tail = head; + //reference each other + size++; + } + // if there is, then assign + // head.last to the new node + // assign the new node to next + // and assign the head to new node + else + { + head.last = newNode; + head = newNode; + head.next = tempNode; + size++; + } + } + + /** + * Appends (inserts) the specified value at the back of the list (at index size()-1). + * + * @param value value to be inserted + */ + @Override + public void addBack(int value) + { + //create node with new value + Node newNode = new Node(); + newNode.data = value; + + if (head == null && tail == null) + { + head = newNode; + tail = newNode; + } + + //assign the next node of tail to new node + //reassign the tail to the new node + //increase size + + //current tail + tail.next = newNode; + + //new tail + newNode.last = tail; + tail = newNode; + size++; + } + + /** + * Inserts the specified value at the specified position in this list. + * Shifts the value currently at that position (if any) and any subsequent + * values to the right. + * + * @param index index at which the specified value is to be inserted + * @param value value to be inserted + * @throws IndexOutOfBoundsException if the index is out of range + */ + @Override + public void add(int index, int value) + { + //throw out of bounds exception if out of range + if (index > size || index == 0) + { + throw new IndexOutOfBoundsException("The given index is out of range"); + } + + //keep track of index, current node, previous node, and create new node + int currentIndex = 0; + Node current = head; + Node newNode = new Node(); + newNode.data = value; + Node previousNode = current.last; + + //if there's only one node + if (head == tail) + { + //reassign all nodes + //head reassignments + head.next = newNode; + + //new node reassignments + newNode.last = head; + newNode.next = tail; + + //tail + tail = newNode; + tail.last = head; + tail.next = new Node(); + size++; + } + else + { + //traverse list - while currentIndex does not equal the index + while (currentIndex != index) + { + previousNode = current; + current = current.next; + currentIndex++; + } + //if found, add the node by connecting all nodes to each other + //previous node + previousNode.next = newNode; + + //new node + newNode.last = previousNode; + if (current == null) + { + tail = newNode; + size++; + } + else { + newNode.next = current.next; + size++; + } + } + } + + /** + * Removes the value located at the front of the list + * (at index 0), if it is present. + * Shifts any subsequent values to the left. + */ + @Override + public void removeFront() + { + if (head == null) + { + throw new NoSuchElementException("The list is empty"); + } + + if (size == 1) + { + head = null; + } + else + { + //change the head to head.next + //reassign the new heads last to node + head = head.next; + head.last = null; + size--; + } + } + + /** + * Removes the value located at the back of the list + * (at index size()-1), if it is present. + */ + @Override + public void removeBack() + { + if (head == null && tail == null) + { + throw new NoSuchElementException("The list is empty"); + } + else if (size > 0) { + //set up a temporary variable for convenience + Node theOneToRemove = tail.last; + + theOneToRemove.last.next = tail; + tail.last = theOneToRemove.last; + + //optional, but strongly recommended to clean up + theOneToRemove.next = null; + theOneToRemove.last = null; + theOneToRemove.data = 0; + + size--; + } + } + + /** + * Removes the value at the specified position in this list. + * Shifts any subsequent values to the left. Returns the value + * that was removed from the list. + * + * @param index the index of the value to be removed + * @return the value previously at the specified position + * @throws IndexOutOfBoundsException if the index is out of range + */ + @Override + public int remove(int index) + { + //index out of bounds + if (index > size) + { + throw new IndexOutOfBoundsException("Index is out of range"); + } + + //current index and current node + Node current = head; + int currentIndex = 0; + int returnValue = 0; + + //traverse through list and compare + while (currentIndex != index) + { + current = current.next; + currentIndex++; + } + //remove the node by reassign/rearranging the nodes + //previous.next to next node, + //next node to previous + returnValue = current.data; + current.last = current.next; + current.next.last = current.last; + + //reduce the size and return value + size--; + + return returnValue; + } + + /** + * Returns the value at the specified position in the list. + * + * @param index index of the value to return + * @return the value at the specified position in this list + * @throws IndexOutOfBoundsException if the index is out of range + */ + @Override + public int get(int index) + { + //check if the index is out of bounds + //get the size and check the index + if (index > size) + { + throw new IndexOutOfBoundsException("The index is out of range"); + } + + //traverse list with current starting at the head + //and current index + Node current = head; + int currentIndex = 0; + + if (index == 0) + { + return head.data; + } + else if (index == size) + { + return tail.data; + } + else + { + // while loop that traverses list and + // increments current index + while (index != currentIndex) + { + current = current.next; + currentIndex++; + } + return current.data; + } + } + + /** + * Returns true if this list contains the specified value. + * + * @param value value whose presence in this list is to be searched for + * @return true if this list contains the specified value + */ + @Override + public boolean contains(int value) + { + // create current node + Node current = head; + + // run through list until you hit the value + while (current.data != value) + { + // check if the next node is null (this means we've hit the + // end of the list and the value does not exist + if (current.next == null) + { + return false; + } + current = current.next; + } + return true; + } + + /** + * Returns the index of the first occurrence of the specified value + * in this list, or -1 if this list does not contain the value. + * + * @param value value to search for + * @return the index of the first occurrence of the specified value in this list + * or -1 if this list does not contain the value + */ + @Override + public int indexOf(int value) + { + // create current node and currentIndex + Node current = head; + int currentIndex = 0; + + // run through list until you hit the value + while (current.data != value) + { + current = current.next; + currentIndex++; + } + return currentIndex; + } + + /** + * Returns true if this list contains no values. + * + * @return true if this list contains no values + */ + @Override + public boolean isEmpty() + { + return head == null; + } + + /** + * Returns the number of values in this list. + * + * @return the number of values in this list + */ + @Override + public int size() + { + //check if the list is empty + if (head == null) + { + throw new NoSuchElementException("The list is empty"); + } + + //use current to traverse linked list + //and keep count + Node current = head; + int count = 0; + + //move through until you get to the end + while (current != null) + { + current = current.next; + count++; + } + return count; + } + + /** + * Removes all the values from this list. + * The list will be empty after this call returns. + */ + @Override + public void clear() + { + head = null; + } + + /** + * Returns an iterator over elements of type {@code T}. + * + * @return an Iterator. + */ + @Override + public Iterator iterator() + { + DoublyLinkedIterator theIterator = new DoublyLinkedIterator(); + return theIterator; + } + + private class DoublyLinkedIterator implements Iterator{ + + private Node current; + + public DoublyLinkedIterator() { + current = head; + } + /** + * Returns {@code true} if the iteration has more elements. + * (In other words, returns {@code true} if {@link #next} would + * return an element rather than throwing an exception.) + * + * @return {@code true} if the iteration has more elements + */ + @Override + public boolean hasNext() + { + //return current != null; <----- another way to write this + if (current == null) { + return false; + } + else { + return true; + } + } + + /** + * Returns the next element in the iteration. + * + * @return the next element in the iteration + * @throws NoSuchElementException if the iteration has no more elements + */ + @Override + public Integer next() + { + if (current == null) + { + throw new NoSuchElementException("There is no next one to go to!!"); + } + int item = current.data; + current = current.next; + return item; + } + } +} diff --git a/src/IntList.java b/src/IntList.java index 398c27b..0ffc8c3 100644 --- a/src/IntList.java +++ b/src/IntList.java @@ -3,18 +3,21 @@ * for an ordered (indexed) collection of ints, which * in mathematics is known as a sequence. */ -public interface IntList extends Iterable { +public interface IntList extends Iterable +{ /** * Prepends (inserts) the specified value at the front of the list (at index 0). * Shifts the value currently at the front of the list (if any) and any * subsequent values to the right. + * * @param value value to be inserted */ void addFront(int value); /** * Appends (inserts) the specified value at the back of the list (at index size()-1). + * * @param value value to be inserted */ void addBack(int value); @@ -23,6 +26,7 @@ public interface IntList extends Iterable { * Inserts the specified value at the specified position in this list. * Shifts the value currently at that position (if any) and any subsequent * values to the right. + * * @param index index at which the specified value is to be inserted * @param value value to be inserted * @throws IndexOutOfBoundsException if the index is out of range @@ -46,6 +50,7 @@ public interface IntList extends Iterable { * Removes the value at the specified position in this list. * Shifts any subsequent values to the left. Returns the value * that was removed from the list. + * * @param index the index of the value to be removed * @return the value previously at the specified position * @throws IndexOutOfBoundsException if the index is out of range @@ -54,6 +59,7 @@ public interface IntList extends Iterable { /** * Returns the value at the specified position in the list. + * * @param index index of the value to return * @return the value at the specified position in this list * @throws IndexOutOfBoundsException if the index is out of range @@ -62,6 +68,7 @@ public interface IntList extends Iterable { /** * Returns true if this list contains the specified value. + * * @param value value whose presence in this list is to be searched for * @return true if this list contains the specified value */ @@ -70,6 +77,7 @@ public interface IntList extends Iterable { /** * Returns the index of the first occurrence of the specified value * in this list, or -1 if this list does not contain the value. + * * @param value value to search for * @return the index of the first occurrence of the specified value in this list * or -1 if this list does not contain the value @@ -78,12 +86,14 @@ public interface IntList extends Iterable { /** * Returns true if this list contains no values. + * * @return true if this list contains no values */ boolean isEmpty(); /** * Returns the number of values in this list. + * * @return the number of values in this list */ int size(); diff --git a/src/LinkedIntList.java b/src/LinkedIntList.java new file mode 100644 index 0000000..853186e --- /dev/null +++ b/src/LinkedIntList.java @@ -0,0 +1,351 @@ +import java.util.Iterator; +import java.util.NoSuchElementException; + +public class LinkedIntList implements IntList { + + // define what a node is + private class Node { + int data; + Node next; + } + + // set up the head field + private Node head; + + // set up the size field + private int size; + + //add a constructor to initialize + public LinkedIntList() + { + head = null; + size = 0; + } + + /** + * Prepends (inserts) the specified value at the front of the list (at index 0). + * Shifts the value currently at the front of the list (if any) and any + * subsequent values to the right. + * + * @param value value to be inserted + */ + @Override + public void addFront(int value) + { + //considerations: empty list + + // set up a new node + Node newNode = new Node(); + + if (head == null) { + // the list is currently empty + head = newNode; + head.data = value; + size++; + } + else { + // the list currently has some nodes in it + newNode.next = head; + head = newNode; + head.data = value; + size++; + } + } + + /** + * Appends (inserts) the specified value at the back of the list (at index size()-1). + * + * @param value value to be inserted + */ + @Override + public void addBack(int value) + { + //considerations: empty list + //create a current to traverse list + //create new node + Node current = head; + Node node = new Node(); + node.data = value; + + if (head == null) { + // the list is currently empty + head.data = node.data; + size++; + } + + //otherwise, assign the current.next to the new value + while (current.next != null) + { + current = current.next; + } + current.next = node; + } + + /** + * Inserts the specified value at the specified position in this list. + * Shifts the value currently at that position (if any) and any subsequent + * values to the right. + * + * @param index index at which the specified value is to be inserted + * @param value value to be inserted + * @throws IndexOutOfBoundsException if the index is out of range + */ + @Override + public void add(int index, int value) + { + //considerations: empty list or invalid index + + //keep track of current + Node current = head; + int currentIndex = 0; + + Node newNode = new Node(); + newNode.data = value; + + //invalid index + if (index < 0) + { + throw new IndexOutOfBoundsException("Index is out of range"); + } + + //if head is null - index 0 + if (head == null) + { + head.data = value; + size++; + } + //if index 1 is null, assign new node + else if (current.next == null) + { + current.next = newNode; + } + else //if head && current.next are not null + { + Node previousNode = current; + + //move through until you get to the last + while (currentIndex != index && current.next != null) + { + previousNode = current; + current = current.next; + currentIndex++; + } + previousNode.next = newNode; + newNode.next = current.next; + //shift everything over by reassigning + } + } + + /** + * Removes the value located at the front of the list + * (at index 0), if it is present. + * Shifts any subsequent values to the left. + */ + @Override + public void removeFront() + { + Node current = head; + //consider: empty list + if (head == null) + { + throw new NoSuchElementException("The list is empty"); + } + head = current.next; + } + + /** + * Removes the value located at the back of the list + * (at index size()-1), if it is present. + */ + @Override + public void removeBack() + { + Node current = head; + while (current.next != null) + { + current = current.next; + } + current = null; + } + + /** + * Removes the value at the specified position in this list. + * Shifts any subsequent values to the left. Returns the value + * that was removed from the list. + * + * @param index the index of the value to be removed + * @return the value previously at the specified position + * @throws IndexOutOfBoundsException if the index is out of range + */ + @Override + public int remove(int index) + { + int currentIndex = 0; + Node current = head; + Node previous = current; + + while (currentIndex != index) + { + previous = current; + current = current.next; + currentIndex++; + } + previous = current.next; + return current.data; + } + + /** + * Returns the value at the specified position in the list. + * + * @param index index of the value to return + * @return the value at the specified position in this list + * @throws IndexOutOfBoundsException if the index is out of range + */ + @Override + public int get(int index) + { + Node current = head; + int currentIndex = 0; + + while (currentIndex != index && current.next != null) + { + current = current.next; + currentIndex++; + } + return current.data; + } + + /** + * Returns true if this list contains the specified value. + * + * @param value value whose presence in this list is to be searched for + * @return true if this list contains the specified value + */ + @Override + public boolean contains(int value) + { + Node current = head; + while (current.data != value && current.next != null) + { + current = current.next; + } + return true; + } + + /** + * Returns the index of the first occurrence of the specified value + * in this list, or -1 if this list does not contain the value. + * + * @param value value to search for + * @return the index of the first occurrence of the specified value in this list + * or -1 if this list does not contain the value + */ + @Override + public int indexOf(int value) + { + Node current = head; + int currentIndex = 0; + while (current.data != value) + { + current = current.next; + currentIndex++; + } + return currentIndex; + } + + /** + * Returns true if this list contains no values. + * + * @return true if this list contains no values + */ + @Override + public boolean isEmpty() + { + return head == null; + } + + /** + * Returns the number of values in this list. + * + * @return the number of values in this list + */ + @Override + public int size() + { + Node current = head; + while (current != null) + { + size++; + current = current.next; + } + return size - 1; + } + + /** + * Removes all the values from this list. + * The list will be empty after this call returns. + */ + @Override + public void clear() + { + head = null; + } + + /** + * Returns an iterator over elements of type {@code T}. + * + * @return an Iterator. + */ + @Override + public Iterator iterator() + { + SinglyLinkedIterator theIterator = new SinglyLinkedIterator(); + return theIterator; + } + + //helper class/type that defines how the iterator works + private class SinglyLinkedIterator implements Iterator{ + + private Node current; + + public SinglyLinkedIterator() { + current = head; + } + /** + * Returns {@code true} if the iteration has more elements. + * (In other words, returns {@code true} if {@link #next} would + * return an element rather than throwing an exception.) + * + * @return {@code true} if the iteration has more elements + */ + @Override + public boolean hasNext() + { + //return current != null; <----- another way to write this + if (current == null) { + return false; + } + else { + return true; + } + } + + /** + * Returns the next element in the iteration. + * + * @return the next element in the iteration + * @throws NoSuchElementException if the iteration has no more elements + */ + @Override + public Integer next() + { + if (current == null) + { + throw new NoSuchElementException("There is no next one to go to!!"); + } + int item = current.data; + current = current.next; + return item; + } + } +} diff --git a/src/Main.java b/src/Main.java deleted file mode 100644 index 930198c..0000000 --- a/src/Main.java +++ /dev/null @@ -1,15 +0,0 @@ -//TIP To Run code, press or -// click the icon in the gutter. -public class Main { - public static void main(String[] args) { - //TIP Press with your caret at the highlighted text - // to see how IntelliJ IDEA suggests fixing it. - System.out.printf("Hello and welcome!"); - - for (int i = 1; i <= 5; i++) { - //TIP Press to start debugging your code. We have set one breakpoint - // for you, but you can always add more by pressing . - System.out.println("i = " + i); - } - } -} \ No newline at end of file diff --git a/tests/DoublyLinkedIntListTest.java b/tests/DoublyLinkedIntListTest.java new file mode 100644 index 0000000..d7635de --- /dev/null +++ b/tests/DoublyLinkedIntListTest.java @@ -0,0 +1,226 @@ +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class DoublyLinkedIntListTest +{ + private DoublyLinkedIntList emptyList; + private DoublyLinkedIntList fullList; + private DoublyLinkedIntList oneItemList; + private DoublyLinkedIntList twoItemList; + + public void createLists() + { + //empty list + emptyList = new DoublyLinkedIntList(); + + //full list + fullList = new DoublyLinkedIntList(); + fullList.addFront(5); + fullList.add(1, 10); + fullList.add(2, 15); + fullList.add(3, 20); + fullList.addBack(25); + + //one item list + oneItemList = new DoublyLinkedIntList(); + oneItemList.addFront(5); + + //two items + twoItemList = new DoublyLinkedIntList(); + twoItemList.addFront(5); + twoItemList.addBack(25); + } + + @Test + void addFront() + { + createLists(); + emptyList.addFront(7); + fullList.addFront(7); + oneItemList.addFront(7); + twoItemList.addFront(7); + + assertEquals(7, emptyList.get(0)); + assertEquals(7, fullList.get(0)); + assertEquals(7, oneItemList.get(0)); + assertEquals(7, twoItemList.get(0)); +// DoublyLinkedIntList list = createList(); +// list.addFront(50); +// assertEquals(50, list.get(0)); + } + + @Test + void addBack() + { + createLists(); + emptyList.addBack(1); + fullList.addBack(1); + oneItemList.addBack(1); + twoItemList.addBack(1); + + assertEquals(1, emptyList.get(emptyList.size)); + assertEquals(1, fullList.get(fullList.size)); + assertEquals(1, oneItemList.get(oneItemList.size)); + assertEquals(1, twoItemList.get(twoItemList.size)); + +// DoublyLinkedIntList list = createList(); +// list.addBack(30); +// assertEquals(30, list.get(list.size)); + } + + @Test + void add() + { + createLists(); + +// emptyList.add(1, 3); -> throws IndexOutOfBoundException + fullList.add(1, 3); + oneItemList.add(1, 3); + twoItemList.add(1, 3); + + assertEquals(3, fullList.get(1)); + assertEquals(3, oneItemList.get(1)); + assertEquals(3, twoItemList.get(1)); + +// DoublyLinkedIntList list = createList(); +// list.add(5, 21); +// assertEquals(21, list.get(5)); + } + + @Test + void removeFront() + { + createLists(); + + int removedElementOne = fullList.get(0); + int removedElementTwo = oneItemList.get(0); + int removedElementThree = twoItemList.get(0); + int removedElement = 5; + +// assertEquals(emptyList); -> Throws exception + fullList.removeFront(); + oneItemList.removeFront(); + twoItemList.removeFront(); + + assertEquals(removedElement, removedElementOne); + assertEquals(removedElement, removedElementTwo); + assertEquals(removedElement, removedElementThree); + +// DoublyLinkedIntList list = createList(); +// int newHead = list.get(1); +// list.removeFront(); +// assertEquals(newHead, list.get(0)); + } + + @Test + void removeBack() + { + createLists(); + +// emptyList.removeFront(); + fullList.removeBack(); + oneItemList.removeBack(); + twoItemList.removeBack(); + +// assertEquals(); +// DoublyLinkedIntList list = createList(); +// int newTail = list.get(list.size - 1); +// list.removeBack(); +// assertEquals(newTail, list.get(list.size)); + } + + @Test + void remove() + { + createLists(); + + assertEquals(5, fullList.remove(0)); + assertEquals(5, oneItemList.remove(0)); + assertEquals(5, twoItemList.remove(0)); +// DoublyLinkedIntList list = createList(); +// assertEquals(list.get(2), list.remove(2)); + } + + @Test + void get() + { + createLists(); + +// assertEquals(5, fullList.indexOf(0)); + assertEquals(5, fullList.get(0)); + assertEquals(5, oneItemList.get(0)); + assertEquals(5, twoItemList.get(0)); + } +// + @Test + void contains() + { + createLists(); + + assertEquals(true, fullList.contains(5)); + assertEquals(true, oneItemList.contains(5)); + assertEquals(true, twoItemList.contains(5)); + + +// DoublyLinkedIntList list = createList(); +// assertEquals(true, list.contains(20)); + } + + @Test + void indexOf() + { + createLists(); + +// assertEquals(1, emptyList); + assertEquals(0, fullList.indexOf(0)); + assertEquals(0, oneItemList.indexOf(0)); + assertEquals(0, twoItemList.indexOf(0)); + +// DoublyLinkedIntList list = createList(); +// assertEquals(4, list.indexOf(25)); + } + + @Test + void isEmpty() + { + createLists(); + + assertEquals(true, emptyList); + assertEquals(false, fullList); + assertEquals(false, oneItemList); + assertEquals(false, twoItemList); +// DoublyLinkedIntList list = createList(); +// assertEquals(false, list.isEmpty()); + } + + @Test + void size() + { + createLists(); + + assertEquals(0, emptyList.size()); + assertEquals(5, fullList.size()); + assertEquals(1, oneItemList.size()); + assertEquals(2, twoItemList.size()); +// DoublyLinkedIntList list = createList(); +// assertEquals(5, list.size()); + } + + @Test + void clear() + { + createLists(); + + emptyList.clear(); + fullList.clear(); + oneItemList.clear(); + twoItemList.clear(); + + assertEquals(true, emptyList.isEmpty()); + assertEquals(true, emptyList.isEmpty()); + assertEquals(true, emptyList.isEmpty()); + assertEquals(true, emptyList.isEmpty()); + + } +} \ No newline at end of file diff --git a/tests/LinkedIntListTest.java b/tests/LinkedIntListTest.java new file mode 100644 index 0000000..1dab0c7 --- /dev/null +++ b/tests/LinkedIntListTest.java @@ -0,0 +1,125 @@ +import org.junit.jupiter.api.Test; +import org.junit.platform.engine.support.hierarchical.Node; + +import java.util.Random; + +import static org.junit.jupiter.api.Assertions.*; + +class LinkedIntListTest +{ + public LinkedIntList createList() + { + int sizeOfList = 5; + LinkedIntList list = new LinkedIntList(); + list.addFront(1); + + for (int i = 2; i < sizeOfList; i++) + { + Random rand = new Random(); + int randomNum = rand.nextInt(100); + list.add(i, randomNum); + } + return list; + } + + @Test + void addFront() + { + LinkedIntList linkedList = createList(); + linkedList.addFront(1); + assertEquals(1, linkedList.get(0)); + } + + @Test + void addBack() + { + LinkedIntList linkedIntList = createList(); + linkedIntList.addBack(2); + assertEquals(2, linkedIntList.get(linkedIntList.size())); + } + + @Test + void add() + { + LinkedIntList linkedIntList = createList(); + linkedIntList.add(3, 5); + assertEquals(5, linkedIntList.get(3)); + } + + @Test + void removeFront() + { + LinkedIntList linkedIntList = createList(); + int newFront = linkedIntList.get(1); + linkedIntList.removeFront(); + assertEquals(linkedIntList.get(0), newFront); + } + + @Test + void removeBack() + { + LinkedIntList linkedIntList = createList(); + int newLastNode = linkedIntList.get(linkedIntList.size() - 1); + linkedIntList.removeBack(); + assertEquals(linkedIntList.get(linkedIntList.size() - 1), newLastNode); + } + + @Test + void remove() + { + LinkedIntList linkedIntList = createList(); + int removedNode = linkedIntList.get(2); + assertEquals(removedNode, linkedIntList.remove(2)); + } + + @Test + void get() + { + LinkedIntList linkedIntList = createList(); + int retrievedData = linkedIntList.get(3); + assertEquals(retrievedData, linkedIntList.get(3)); + } + + @Test + void contains() + { + LinkedIntList linkedIntList = createList(); + linkedIntList.add(linkedIntList.size(), 3); + assertEquals(true, linkedIntList.contains(3)); + } + + @Test + void indexOf() + { + LinkedIntList fixedList = new LinkedIntList(); + fixedList.add(0, 1); + fixedList.add(1, 2); + fixedList.add(2, 3); + + fixedList.addBack(6); + assertEquals(fixedList.size() - 1, fixedList.indexOf(6)); + } + + @Test + void isEmpty() + { + LinkedIntList emptyList = new LinkedIntList(); + assertEquals(true, emptyList.isEmpty()); + } + + @Test + void size() + { + LinkedIntList linkedIntList = createList(); + assertEquals(4, linkedIntList.size()); + } + + @Test + void clear() + { + LinkedIntList linkedIntList = createList(); + int size = linkedIntList.size(); + linkedIntList.clear(); + assertEquals(true, linkedIntList.isEmpty()); + } +} \ No newline at end of file