diff --git a/.idea/misc.xml b/.idea/misc.xml index 6f29fee..1acf042 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/IntListReview.iml b/IntListReview.iml index c90834f..1df54e0 100644 --- a/IntListReview.iml +++ b/IntListReview.iml @@ -7,5 +7,37 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/ArrayIntList.java b/src/ArrayIntList.java new file mode 100644 index 0000000..2a35df0 --- /dev/null +++ b/src/ArrayIntList.java @@ -0,0 +1,320 @@ +import java.util.Iterator; +import java.util.NoSuchElementException; + +public class ArrayIntList implements IntList { + + // internal (private) representation + private int[] buffer; + private int size; // number of "spots used" in the buffer + private final static int INITIAL_CAPACITY = 10; + + public ArrayIntList() { + buffer = new int[INITIAL_CAPACITY]; + 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) { + + // check if empty + if (size == 0) { + resize(size + 1); + buffer[0] = value; + size++; + } else { + // check if full + if (size == buffer.length) { + resize(size + 1); + } + + // open a spot at index 0 where value will be saved + // shift everything over to the right by 1 position + + // put value in position [0] + for (int i = size; i >= 1; i--) { + buffer[i] = buffer[i-1]; + } + buffer[0] = value; + } + } + + private void resize(int newSize) { + // create a new array that is of the new size + int[] temp = new int[newSize]; + // copy over values from the existing buffer + for (int i = 0; i < size; i++) { + temp[i] = buffer[i]; + } + + // make the switchover + buffer = temp; + + } + + /** + * 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) { + if (size == buffer.length) { + // if the size matches the capacity, then I know I'm "full" + // and I need to resize (create a new larger buffer and copy + // the values over from the older smaller buffer) + + // makes the newSize up by 1 each time it's called + resize(size+1); + + } + buffer[size] = value; + 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) { + + // if the size is 0, resize the array and add the value to the 0 position + if (size == 0) { + resize(size + 1); + buffer[0] = value; + size++; + + // if the selected index is greater than the size, it'll just add it to the back + } else if (index > size){ + addBack(value); + + // if all upper cases aren't true, we'll add the value to the index + } else { + resize(size + 1); + for (int i = size; i > 1; i--) { + if (i >= index) { + buffer[i] = buffer[i-1]; + } + } + + if (index == 0) { + addFront(value); + } else { + 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() { + buffer[0] = 0; + int[] temp = new int[buffer.length - 1]; + for (int i = 0; i < temp.length; i++) { + temp[i] = buffer[i + 1]; + } + + buffer = temp; + size--; + } + + /** + * Removes the value located at the back of the list + * (at index size()-1), if it is present. + */ + @Override + public void removeBack() { + if (size == 0) { + throw new IllegalStateException("already empty"); + } + int[] temp = new int[buffer.length - 1]; + for (int i = 0; i < temp.length; i++) { + temp[i] = buffer[i]; + } + + buffer = temp; + 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) { + int removed = buffer[index]; + for (int i = 0; i < size; i++) { + if (i >= index) { + if (i < size - 1) { + buffer[i] = buffer[i + 1]; + } + } + } + + removeBack(); + + return removed; + } + + /** + * 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) { + int index = 0; + for (int i = 0; i < size; i++) { + if (buffer[i] == value) { + index = i; + } + } + return index; + } + + /** + * Returns true if this list contains no values. + * + * @return true if this list contains no values + */ + @Override + public boolean isEmpty() { + if (buffer.length == 0 || size == 0) { + return true; + } else { + return false; + } + } + + /** + * 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() { + int[] list = new int[0]; + size = 0; + buffer = list; + } + + /** + * Returns an iterator over elements of type {@code T}. + * + * @return an Iterator. + */ + @Override + public Iterator iterator() { + // return a new instance of the helper iterator class (below) + return new ArrayIntListIterator(); + } + + // nested or inner class (helper class) + public class ArrayIntListIterator implements Iterator { + private int currentPosition; + + public ArrayIntListIterator() { + currentPosition = 0; + } + + @Override + public boolean hasNext() { + return (currentPosition < size); + } + + @Override + public Integer next() { + // just to be safe - make sure there is a next + if (!hasNext()) { + throw new NoSuchElementException(); + } + int value = get(currentPosition); + currentPosition++; + return value; + } + } + + @Override + public String toString() { + if (size == 0) { + return "[]"; + } + StringBuilder sb = new StringBuilder(); + + sb.append("["); + for (int i = 0; i < buffer.length - 1; i++) { + sb.append(buffer[i]); + sb.append(", "); + } + sb.append(buffer[buffer.length - 1]); + sb.append("]"); + + + return sb.toString(); + } +} diff --git a/src/ArrayIntListTest.java b/src/ArrayIntListTest.java new file mode 100644 index 0000000..1f7c1f6 --- /dev/null +++ b/src/ArrayIntListTest.java @@ -0,0 +1,224 @@ +import static org.junit.jupiter.api.Assertions.*; + +class ArrayIntListTest { + + @org.junit.jupiter.api.Test + // regular case where the list has items, add value to front + void addFront() { + ArrayIntList theList = new ArrayIntList(); + theList.addBack(1); + theList.addBack(2); + theList.addBack(3); + theList.addBack(4); + theList.addBack(5); + theList.addBack(6); + + theList.addFront(7); + assertEquals(theList.get(0), 7); + } + + @org.junit.jupiter.api.Test + // test addFront if there are no values in the list (list is empty) + void addFrontWithNoValues() { + ArrayIntList theList = new ArrayIntList(); + theList.addFront(2); + assertEquals(theList.get(0), 2); + } + + @org.junit.jupiter.api.Test + // test if there is only a single value in the list + void addFrontWithSingleValue() { + ArrayIntList theList = new ArrayIntList(); + theList.addFront(2); + theList.addFront(100); + assertEquals(theList.get(0), 100); + } + + @org.junit.jupiter.api.Test + void addBack() { + ArrayIntList theList = new ArrayIntList(); + theList.addBack(42); + assertEquals(theList.get(0), 42); + } + + @org.junit.jupiter.api.Test + void addBackWithResize() { + ArrayIntList theList = new ArrayIntList(); + // fill the list with INITIAL_CAPACITY or 10 items + for (int i = 0; i < 100; i++) { + theList.addBack(i); + } + + // now add item 11: + theList.addBack(42); + + assertEquals(theList.get(theList.size()-1), 42); + } + + @org.junit.jupiter.api.Test + void add() { + ArrayIntList theList = new ArrayIntList(); + theList.addBack(1); + theList.addBack(2); + theList.addBack(3); + theList.addBack(4); + theList.addBack(5); + theList.add(3, 55); + + System.out.println(theList); + } + + @org.junit.jupiter.api.Test + void removeFront() { + ArrayIntList theList = new ArrayIntList(); + theList.addBack(1); + theList.addBack(2); + theList.addBack(3); + theList.addBack(4); + theList.addBack(5); + + theList.removeFront(); + + System.out.println(theList); + } + + @org.junit.jupiter.api.Test + void removeBack() { + ArrayIntList theList = new ArrayIntList(); + theList.addBack(1); + theList.addBack(2); + theList.addBack(3); + theList.addBack(4); + theList.addBack(5); + + theList.removeBack(); + + System.out.println(theList); + } + + @org.junit.jupiter.api.Test + void removeBackFromEmptyList() { + // create an empty list + ArrayIntList theList = new ArrayIntList(); + + assertThrowsExactly(IllegalStateException.class, () -> {theList.removeBack();}); + } + + @org.junit.jupiter.api.Test + void removeBackFromSingletonList() { + + } + + @org.junit.jupiter.api.Test + void removeBackFromListOf10() { + ArrayIntList theList = new ArrayIntList(); + + + // fill list with 10 items + for (int i = 0; i < 10; i++) { + theList.addBack(i); + } + + // remove the item in the back + theList.removeBack(); + + String out = theList.toString(); + assertEquals(out, "[0, 1, 2, 3, 4, 5, 6, 7, 8, 0]"); + } + + @org.junit.jupiter.api.Test + void remove() { + ArrayIntList theList = new ArrayIntList(); + theList.addBack(1); + theList.addBack(2); + theList.addBack(3); + theList.addBack(4); + theList.addBack(5); + + ArrayIntList removed = new ArrayIntList(); + removed.addBack(1); + removed.addBack(2); + removed.addBack(4); + removed.addBack(5); + + theList.remove(2); + + assertEquals(removed.size(), theList.size()); + } + + @org.junit.jupiter.api.Test + void get() { + ArrayIntList theList = new ArrayIntList(); + theList.addBack(1); + theList.addBack(2); + theList.addBack(3); + theList.addBack(4); + theList.addBack(5); + + assertEquals(5, theList.get(4)); + } + + @org.junit.jupiter.api.Test + void contains() { + ArrayIntList theList = new ArrayIntList(); + theList.addBack(1); + theList.addBack(2); + theList.addBack(3); + theList.addBack(4); + theList.addBack(5); + + assertEquals(false, theList.contains(6)); + } + + @org.junit.jupiter.api.Test + void indexOf() { + ArrayIntList theList = new ArrayIntList(); + theList.addBack(1); + theList.addBack(2); + theList.addBack(3); + theList.addBack(4); + theList.addBack(5); + + System.out.println(theList.indexOf(4)); + } + + @org.junit.jupiter.api.Test + void isEmpty() { + ArrayIntList theList = new ArrayIntList(); + + assertEquals(0, theList.size()); + } + + @org.junit.jupiter.api.Test + void size() { + ArrayIntList theList = new ArrayIntList(); + theList.addBack(1); + theList.addBack(2); + theList.addBack(3); + theList.addBack(4); + theList.addBack(5); + + System.out.println(theList.size()); + } + + @org.junit.jupiter.api.Test + void clear() { + ArrayIntList theList = new ArrayIntList(); + theList.addBack(1); + theList.addBack(2); + theList.addBack(3); + theList.addBack(4); + theList.addBack(5); + + theList.clear(); + assertEquals(0, theList.size()); + } + + @org.junit.jupiter.api.Test + void iterator() { + } + + @org.junit.jupiter.api.Test + void testToString() { + } +} \ No newline at end of file diff --git a/src/IntList.java b/src/IntList.java index 398c27b..6b7776e 100644 --- a/src/IntList.java +++ b/src/IntList.java @@ -1,96 +1,96 @@ -/** - * The IntList interface defines a set of operations - * for an ordered (indexed) collection of ints, which - * in mathematics is known as a sequence. - */ -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); - - /** - * 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 - */ - void add(int index, int value); - - /** - * Removes the value located at the front of the list - * (at index 0), if it is present. - * Shifts any subsequent values to the left. - */ - void removeFront(); - - /** - * Removes the value located at the back of the list - * (at index size()-1), if it is present. - */ - void removeBack(); - - /** - * 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 - */ - int remove(int index); - - /** - * 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 - */ - int get(int 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 - */ - boolean contains(int value); - - /** - * 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 - */ - int indexOf(int value); - - /** - * 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(); - - /** - * Removes all the values from this list. - * The list will be empty after this call returns. - */ - void clear(); -} +/** + * The IntList interface defines a set of operations + * for an ordered (indexed) collection of ints, which + * in mathematics is known as a sequence. + */ +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); + + /** + * 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 + */ + void add(int index, int value); + + /** + * Removes the value located at the front of the list + * (at index 0), if it is present. + * Shifts any subsequent values to the left. + */ + void removeFront(); + + /** + * Removes the value located at the back of the list + * (at index size()-1), if it is present. + */ + void removeBack(); + + /** + * 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 + */ + int remove(int index); + + /** + * 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 + */ + int get(int 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 + */ + boolean contains(int value); + + /** + * 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 + */ + int indexOf(int value); + + /** + * 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(); + + /** + * Removes all the values from this list. + * The list will be empty after this call returns. + */ + void clear(); +} diff --git a/src/LinkedIntList.java b/src/LinkedIntList.java new file mode 100644 index 0000000..e4f913c --- /dev/null +++ b/src/LinkedIntList.java @@ -0,0 +1,374 @@ +import java.util.Iterator; +import java.util.NoSuchElementException; + +public class LinkedIntList implements IntList { + + // helper inner/nested class + public static class Node { + int data; + Node next; + + public Node() { + data = 0; + next = null; + } + + public Node(int data, Node next) { + this.data = data; + this.next = next; + } + } // end of class node + + // fields for LinkedIntList class + private Node head; // address of first node in list + private int size; // number of nodes/items in list + + 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) { + // if the list is empty + if (head == null) { + head = new Node(value, null); + return; + } + + // if the list is not empty + head = new Node(value, head); + size++; + } + + /** + * Appends (inserts) the specified value at the back of the list (at index size()-1). + * + * if list is empty (go into if), t = 5 which is 0(1) constant time + * if list is not empty (go into else), t = 2 * size + 6, which is 0(n) + * t = 2n + 6, which is 0(n) linear + * + * @param value value to be inserted + */ + @Override + public void addBack(int value) { + + if (head == null) { + head = new Node(value, null); + } else { + Node current = head; + while (current.next != null) { + current = current.next; + } + + current.next = new Node(value, null); + 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) { + if (index > size) { + System.out.println("index larger than size"); + return; + } else if (index < 0) { + System.out.println("index lower than 0"); + return; + } + + int nodeIndex = 0; + Node current = head; + Node temp = null; + + while (current.next != null && nodeIndex != index) { + current = current.next; + nodeIndex++; + } + + if (size == index && current.next == null) { + current.next = new Node(value, null); + size++; + } else { + temp = new Node(current.data, current.next); + current.data = value; + current.next = temp; + 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.next == null) { + System.out.println("single value in list"); + } + head = head.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; + + if (current == null) { + System.out.println("Empty"); + } else if (current.next == null) { + System.out.println("No back to remove"); + } else { + while (current.next.next != null) { + current = current.next; + } + + current.next = 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 valueRemoved = 0; + + if (head == null) { + System.out.println("Empty head"); + } + + if (index == 0) { + head = head.next; + } else { + Node prev = null; + Node current = head; + int count = 0; + + while (current != null && count < index) { + prev = current; + current = current.next; + count++; + } + + if (current != null) { + valueRemoved = current.data; + prev.next = current.next; + } + } + return valueRemoved; + } + + /** + * 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 indexCount = 0; + + if (index > size) { + throw new IndexOutOfBoundsException(); + } + + while (current.next != null) { + if (indexCount == index) { + return current.data; + } else { + current = current.next; + } + } + + return indexCount; + } + + /** + * 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) { + boolean result = false; + Node current = head; + + while (current.next != null) { + if (current.data == value) { + result = true; + } + current = current.next; + } + + if (current.data == value) { + result = true; + } + + return result; + } + + /** + * 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 nodeIndex = 0; + + while (current.next != null && current.data != value) { + current = current.next; + nodeIndex++; + } + + return nodeIndex; + } + + /** + * Returns true if this list contains no values. + * + * @return true if this list contains no values + */ + @Override + public boolean isEmpty() { + if (head == null) { + return true; + } else { + return false; + } + } + + /** + * 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() { + head = null; + } + + /** + * Returns an iterator over elements of type {@code T}. + * + * @return an Iterator. + */ + @Override + public Iterator iterator() { + return new LinkedIterator(); + } + + // helper method (not required, but nice example to reference) + public void print() { + // create temp variable )almost like an index i) + // copy in the address from head and save it + Node current = head; + + while (current != null) { + // print the value inside the node + System.out.println(current.data); + + // go to the next node + current = current.next; + } + + } + + @Override + public String toString() { + if (head == null) { + return "[]"; + } + + // If I get here, the list is not empty + StringBuilder sb = new StringBuilder(); + sb.append("["); + + Node current = head; + while (current.next != null) { + sb.append(current.data); + sb.append(", "); + + current = current.next; + } + + sb.append(current.data); + + sb.append("]"); + + return sb.toString(); + } + + public class LinkedIterator implements Iterator { + private Node current; + + public LinkedIterator() { + // start the current position at the first node in list + current = head; + } + @Override + public boolean hasNext() { + if (current == null) { + return false; + } else { + return true; + } + } + + @Override + public Integer next() { + if (!hasNext()) { + throw new NoSuchElementException(); + } + + int result = current.data; + current = current.next; + return result; + } + } +} diff --git a/src/Main.java b/src/Main.java index 930198c..6078295 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,15 +1,155 @@ -//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 +import java.util.Arrays; +import java.util.Iterator; + +public class Main { + public static void main(String[] args) { + IntList list = new ArrayIntList(); + IntList list2 = new LinkedIntList(); + + System.out.println("****************************************************"); + System.out.println("Array List"); + System.out.println("****************************************************"); + + list.addBack(92); + list.addBack(24); + list.addBack(56); + list.addBack(61); + list.addBack(756); + list.addBack(777); + list.addBack(723); + list.addBack(947); + list.addBack(724); + list.addBack(986); + list.addBack(1235); + list.addBack(3453); + list.addBack(8766); + list.addBack(3554); + list.addBack(1251); + System.out.println(list.toString()); + + System.out.println("Add front"); + list.addFront(2); + System.out.println("Added 2 to front: --->" + list.toString()); + System.out.println(); + + System.out.println("Add back"); + list.addBack(2); + System.out.println("Added 2 to back: --->" + list.toString()); + System.out.println(); + + System.out.println("Add to index 5"); + list.add(5, 2); + System.out.println("Added 2 to index 5: --->" + list.toString()); + System.out.println(); + + System.out.println("Remove front"); + list.removeFront(); + System.out.println("Removed front: --->" + list.toString()); + System.out.println(); + + System.out.println("Remove back"); + list.removeBack(); + System.out.println("Removed back: --->" + list.toString()); + System.out.println(); + + System.out.println("Remove index of 4"); + list.remove(4); + System.out.println("Removed back: --->" + list.toString()); + System.out.println(); + + System.out.println("Gets value of index 5"); + System.out.println("Result: " + list.get(5)); + System.out.println(); + + System.out.println("Contains value 777"); + System.out.println("Result: " + list.contains(777)); + System.out.println(); + + System.out.println("Index of value 777"); + System.out.println("Result: " + list.indexOf(777)); + System.out.println(); + + System.out.println("Is empty: " + list.isEmpty()); + System.out.println("Size: " + list.size()); + list.clear(); + System.out.println("Cleared list: -->" + list.toString()); + + // ------------------------------- + // Linked List Short Test + // ------------------------------- + System.out.println("****************************************************"); + System.out.println("Linked List"); + System.out.println("****************************************************"); + list2.addBack(92); + list2.addBack(24); + list2.addBack(56); + list2.addBack(61); + list2.addBack(756); + list2.addBack(777); + list2.addBack(723); + list2.addBack(947); + list2.addBack(724); + list2.addBack(986); + list2.addBack(1235); + list2.addBack(3453); + list2.addBack(8766); + list2.addBack(3554); + list2.addBack(1251); + + System.out.println(list2.toString()); + + System.out.println("Add front"); + list2.addFront(2); + System.out.println("Added 2 to front: --->" + list2.toString()); + System.out.println(); + + System.out.println("Add back"); + list2.addBack(2); + System.out.println("Added 2 to back: --->" + list2.toString()); + System.out.println(); + + System.out.println("Add to index 5"); + list2.add(5, 2); + System.out.println("Added 2 to index 5: --->" + list2.toString()); + System.out.println(); + + System.out.println("Remove front"); + list2.removeFront(); + System.out.println("Removed front: --->" + list2.toString()); + System.out.println(); + + System.out.println("Remove back"); + list2.removeBack(); + System.out.println("Removed back: --->" + list2.toString()); + System.out.println(); + + System.out.println("Remove index of 4"); + list2.remove(4); + System.out.println("Removed back: --->" + list2.toString()); + System.out.println(); + + System.out.println("Gets value of index 5"); + System.out.println("Result: " + list2.get(5)); + System.out.println(); + + System.out.println("Contains value 777"); + System.out.println("Result: " + list2.contains(777)); + System.out.println(); + + System.out.println("Index of value 777"); + System.out.println("Result: " + list2.indexOf(777)); + System.out.println(); + + System.out.println("Is empty: " + list2.isEmpty()); + System.out.println("Size: " + list2.size()); + list2.clear(); + System.out.println("Cleared list: -->" + list2.toString()); + +// Iterator iterator = list2.iterator(); +// while (iterator.hasNext()) { +// int value = iterator.next(); +// System.out.println(value); +// } + + } +}