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..6853689 --- /dev/null +++ b/src/ArrayIntList.java @@ -0,0 +1,199 @@ +import java.util.Iterator; +import java.util.NoSuchElementException; + +public class ArrayIntList implements IntList +{ + private int[] buffer; + private int size; + public ArrayIntList() + { + buffer = new int[10]; + size = 0; + } + + @Override + // Normally fast (constant time unless you have to resize) + public void addBack(int value) + { + checkSize(); + buffer[size] = value; + size++; + } + + @Override + //Slow must move every element in array O(N) + public void addFront(int value) + { + checkSize(); + add(0, value); + } + + @Override + //Slow, must move every element in array located above entry index O(N) + public void add(int index, int value) + { + checkSize(); + size ++; + checkIndex(index); + for(int i = size; i > index; i--) + { + buffer[i] = buffer[i-1]; + } + buffer[index] = value; + } + + @Override + // Fast, constant time. + public void removeBack() + { + if(size == 0) + { + throw new IndexOutOfBoundsException("Attempt to remove from empty arrayList."); + } + size--; + } + + @Override + //Slow must move every element in array O(N) + public void removeFront() + { + remove(0); + } + + @Override + //Slow, must move every element in array located above entry index O(N) + public int remove(int index) + { + if(size == 0) + { + throw new IndexOutOfBoundsException("Attempt to remove from empty arrayList."); + } + checkIndex(index); + int returnInt = buffer[index]; + for(int i = index; i < size; i++) + { + buffer[i] = buffer[i + 1]; + } + size --; + return returnInt; + } + + @Override + //Very fast operation. O(1) constant time + public int get(int index) + { + checkIndex(index); + return buffer[index]; + } + + @Override + public boolean contains(int value) + { + return indexOf(value) >= 0; + } + + @Override + public int indexOf(int value) + { + for (int i = 0; i < size; i++) + { + if(buffer[i] == value) { + return i; + } + } + return -1; + } + + @Override + public boolean isEmpty() + { + return size == 0; + } + + @Override + public int size() + { + return size; + } + + @Override + public void clear() + { + buffer = new int[10]; + size = 0; + } + private void checkIndex(int index) throws IndexOutOfBoundsException + { + if(index >= size || index < 0) + { + throw new IndexOutOfBoundsException(index + " is outside Array."); + } + } + + public String toString() + { + if(size == 0) + { + return "[]"; + } + else + { + StringBuilder returnString = new StringBuilder(); + returnString.append("[").append(buffer[0]); + for(int i = 1; i < size; i++) + { + returnString.append(", ").append(buffer[i]); + } + returnString.append("]"); + return returnString.toString(); + } + } + private void checkSize() + { + if(size > buffer.length - 2) + { + buffer = resize(); + } + } + // Slow because you must "touch" every element in array to copy to new array O(N) + private int[] resize() { + int[] returnInt = new int[buffer.length * 2]; + + for (int i = 0; i < size; i++) + { + returnInt[i] = buffer[i]; + } + return returnInt; + } + + @Override + public Iterator iterator() { + return new ArrayIntListIterator(); + } + + private class ArrayIntListIterator implements Iterator + { + private int position; + + public ArrayIntListIterator() + { + position = 0; + } + + public boolean hasNext() + { + return position != size; + } + + public Integer next() + { + if(hasNext()) + { + Integer output = buffer[position]; + position++; + return output; + } + throw new NoSuchElementException("No elements left."); + } + } +} diff --git a/src/ArrayIntListTest.java b/src/ArrayIntListTest.java new file mode 100644 index 0000000..bb0f50f --- /dev/null +++ b/src/ArrayIntListTest.java @@ -0,0 +1,551 @@ +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class ArrayIntListTest { + @Test + void addEmptyFront() + { + // List of one + ArrayIntList testList1 = new ArrayIntList(); + testList1.addFront(-42); + String out = testList1.toString(); + assertEquals(out, "[-42]"); + + ArrayIntList testList2 = new ArrayIntList(); + testList2.addFront(0); + out = testList2.toString(); + assertEquals(out, "[0]"); + + ArrayIntList testList3 = new ArrayIntList(); + testList3.addFront(420000); + out = testList3.toString(); + assertEquals(out, "[420000]"); + } + // List of one + @Test + void addToListOfOneFront() + { + ArrayIntList testList1 = new ArrayIntList(); + testList1.addFront(0); + testList1.addFront(-42); + String out = testList1.toString(); + assertEquals(out, "[-42, 0]"); + + ArrayIntList testList2 = new ArrayIntList(); + testList2.addFront(0); + testList2.addFront(0); + out = testList2.toString(); + assertEquals(out, "[0, 0]"); + + ArrayIntList testList3 = new ArrayIntList(); + testList3.addFront(0); + testList3.addFront(420000); + out = testList3.toString(); + assertEquals(out, "[420000, 0]"); + } + // List of large + @Test + void addToListOfManyFront() + { + ArrayIntList testList1 = new ArrayIntList(); + testList1.addFront(0); + testList1.addFront(-42); + testList1.addFront(-51); + testList1.addFront(-41); + testList1.addFront(-31); + testList1.addFront(-21); + testList1.addFront(-11); + testList1.addFront(-1); + testList1.addFront(0); + testList1.addFront(1); + testList1.addFront(11); + testList1.addFront(21); + testList1.addFront(31); + testList1.addFront(41); + testList1.addFront(51); + testList1.addFront(61); + testList1.addFront(71); + testList1.addFront(81); + testList1.addFront(91); + testList1.addFront(101); + testList1.addFront(1000); + testList1.addFront(10000); + testList1.addFront(100000); + testList1.addFront(1000000); + testList1.addFront(10000000); + String out = testList1.toString(); + assertEquals(out, "[10000000, 1000000, 100000, 10000, 1000, 101, 91, 81, 71, 61, 51, 41, 31, 21, 11, 1, 0, -1, -11, -21, -31, -41, -51, -42, 0]"); + } + + @Test + void addEmptyBack() + { + ArrayIntList testList1 = new ArrayIntList(); + testList1.addBack(-42); + String out = testList1.toString(); + assertEquals(out, "[-42]"); + + ArrayIntList testList2 = new ArrayIntList(); + testList2.addBack(0); + out = testList2.toString(); + assertEquals(out, "[0]"); + + ArrayIntList testList3 = new ArrayIntList(); + testList3.addBack(420000); + out = testList3.toString(); + assertEquals(out, "[420000]"); + } + + @Test + void addToListOfOneBack() + { + ArrayIntList testList1 = new ArrayIntList(); + testList1.addBack(0); + testList1.addBack(-42); + String out = testList1.toString(); + assertEquals(out, "[0, -42]"); + + ArrayIntList testList2 = new ArrayIntList(); + testList2.addBack(0); + testList2.addBack(0); + out = testList2.toString(); + assertEquals(out, "[0, 0]"); + + ArrayIntList testList3 = new ArrayIntList(); + testList3.addBack(0); + testList3.addBack(420000); + out = testList3.toString(); + assertEquals(out, "[0, 420000]"); + } + @Test + void addToListOfManyBack() { + ArrayIntList testList1 = new ArrayIntList(); + testList1.addBack(0); + testList1.addBack(-42); + testList1.addBack(-51); + testList1.addBack(-41); + testList1.addBack(-31); + testList1.addBack(-21); + testList1.addBack(-11); + testList1.addBack(-1); + testList1.addBack(0); + testList1.addBack(1); + testList1.addBack(11); + testList1.addBack(21); + testList1.addBack(31); + testList1.addBack(41); + testList1.addBack(51); + testList1.addBack(61); + testList1.addBack(71); + testList1.addBack(81); + testList1.addBack(91); + testList1.addBack(101); + testList1.addBack(1000); + testList1.addBack(10000); + testList1.addBack(100000); + testList1.addBack(1000000); + testList1.addBack(10000000); + String out = testList1.toString(); + assertEquals(out, "[0, -42, -51, -41, -31, -21, -11, -1, 0, 1, 11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 1000, 10000, 100000, 1000000, 10000000]"); + } + + @Test + void addToEmpty() { + ArrayIntList testList1 = new ArrayIntList(); + testList1.add(0,-42); + String out = testList1.toString(); + assertEquals(out, "[-42]"); + + ArrayIntList testList2 = new ArrayIntList(); + testList2.add(0,0); + out = testList2.toString(); + assertEquals(out, "[0]"); + + ArrayIntList testList3 = new ArrayIntList(); + testList3.add(0,420000); + out = testList3.toString(); + assertEquals(out, "[420000]"); + } + + @Test + void removeFrontEmptyList() { + ArrayIntList testList1 = new ArrayIntList(); + try + { + testList1.removeFront(); + } + catch(Exception e) + { + assertEquals(e.getClass(), IndexOutOfBoundsException.class); + assertEquals(e.getMessage(), "Attempt to remove from empty arrayList."); + } + } + @Test + void removeFrontListOfOne() { + ArrayIntList testList1 = new ArrayIntList(); + testList1.add(0,-42); + testList1.removeFront(); + String out = testList1.toString(); + assertEquals(out, "[]"); + + ArrayIntList testList2 = new ArrayIntList(); + testList2.add(0,0); + testList2.removeFront(); + out = testList2.toString(); + assertEquals(out, "[]"); + + ArrayIntList testList3 = new ArrayIntList(); + testList3.add(0,420000); + testList3.removeFront(); + out = testList3.toString(); + assertEquals(out, "[]"); + + } + @Test + void removeFrontLargeList() { + ArrayIntList testList1 = new ArrayIntList(); + testList1.addBack(0); + testList1.addBack(-42); + testList1.addBack(-51); + testList1.addBack(-41); + testList1.addBack(-31); + testList1.addBack(-21); + testList1.addBack(-11); + testList1.addBack(-1); + testList1.addBack(0); + testList1.addBack(1); + testList1.addBack(11); + testList1.addBack(21); + testList1.addBack(31); + testList1.addBack(41); + testList1.addBack(51); + testList1.addBack(61); + testList1.addBack(71); + testList1.addBack(81); + testList1.addBack(91); + testList1.addBack(101); + testList1.addBack(1000); + testList1.addBack(10000); + testList1.addBack(100000); + testList1.addBack(1000000); + testList1.addBack(10000000); + testList1.removeFront(); + String out = testList1.toString(); + assertEquals(out, "[-42, -51, -41, -31, -21, -11, -1, 0, 1, 11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 1000, 10000, 100000, 1000000, 10000000]"); + } + + @Test + void removeBackEmptyList() + { + ArrayIntList testList1 = new ArrayIntList(); + try + { + testList1.removeBack(); + } + catch(Exception e) + { + assertEquals(e.getClass(), IndexOutOfBoundsException.class); + assertEquals(e.getMessage(), "Attempt to remove from empty arrayList."); + } + } + @Test + void removeBackListOfOne() + { + ArrayIntList testList1 = new ArrayIntList(); + testList1.add(0,-42); + testList1.removeBack(); + String out = testList1.toString(); + assertEquals(out, "[]"); + + ArrayIntList testList2 = new ArrayIntList(); + testList2.add(0,0); + testList2.removeBack(); + out = testList2.toString(); + assertEquals(out, "[]"); + + ArrayIntList testList3 = new ArrayIntList(); + testList3.add(0,420000); + testList3.removeBack(); + out = testList3.toString(); + assertEquals(out, "[]"); + + } + @Test + void removeBackLargeList() + { + ArrayIntList testList1 = new ArrayIntList(); + testList1.addBack(0); + testList1.addBack(-42); + testList1.addBack(-51); + testList1.addBack(-41); + testList1.addBack(-31); + testList1.addBack(-21); + testList1.addBack(-11); + testList1.addBack(-1); + testList1.addBack(0); + testList1.addBack(1); + testList1.addBack(11); + testList1.addBack(21); + testList1.addBack(31); + testList1.addBack(41); + testList1.addBack(51); + testList1.addBack(61); + testList1.addBack(71); + testList1.addBack(81); + testList1.addBack(91); + testList1.addBack(101); + testList1.addBack(1000); + testList1.addBack(10000); + testList1.addBack(100000); + testList1.addBack(1000000); + testList1.addBack(10000000); + testList1.removeBack(); + String out = testList1.toString(); + assertEquals(out, "[0, -42, -51, -41, -31, -21, -11, -1, 0, 1, 11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 1000, 10000, 100000, 1000000]"); + } + + @Test + void remove() + { + ArrayIntList testList1 = new ArrayIntList(); + testList1.addBack(0); + testList1.addBack(-42); + testList1.addBack(-51); + testList1.addBack(-41); + testList1.addBack(-31); + testList1.addBack(-21); + testList1.addBack(-11); + testList1.addBack(-1); + testList1.addBack(0); + testList1.addBack(1); + testList1.addBack(11); + testList1.addBack(21); + testList1.addBack(31); + testList1.addBack(41); + testList1.addBack(51); + testList1.addBack(61); + testList1.addBack(71); + testList1.addBack(81); + testList1.addBack(91); + testList1.addBack(101); + testList1.addBack(1000); + testList1.addBack(10000); + testList1.addBack(100000); + testList1.addBack(1000000); + testList1.addBack(10000000); + assertEquals(0, testList1.remove(0)); + String out = testList1.toString(); + assertEquals(out, "[-42, -51, -41, -31, -21, -11, -1, 0, 1, 11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 1000, 10000, 100000, 1000000, 10000000]"); + + assertEquals(10000000, testList1.remove(23)); + out = testList1.toString(); + assertEquals(out, "[-42, -51, -41, -31, -21, -11, -1, 0, 1, 11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 1000, 10000, 100000, 1000000]"); + + assertEquals(21, testList1.remove(10)); + out = testList1.toString(); + assertEquals(out, "[-42, -51, -41, -31, -21, -11, -1, 0, 1, 11, 31, 41, 51, 61, 71, 81, 91, 101, 1000, 10000, 100000, 1000000]"); + } + + @Test + void get() { + ArrayIntList testList1 = new ArrayIntList(); + testList1.addBack(0); + testList1.addBack(-42); + testList1.addBack(-51); + testList1.addBack(-41); + testList1.addBack(-31); + testList1.addBack(-21); + testList1.addBack(-11); + testList1.addBack(-1); + testList1.addBack(0); + testList1.addBack(1); + testList1.addBack(11); + testList1.addBack(21); + testList1.addBack(31); + testList1.addBack(41); + testList1.addBack(51); + testList1.addBack(61); + testList1.addBack(71); + testList1.addBack(81); + testList1.addBack(91); + testList1.addBack(101); + testList1.addBack(1000); + testList1.addBack(10000); + testList1.addBack(100000); + testList1.addBack(1000000); + testList1.addBack(10000000); + assertEquals(0, testList1.get(0)); + assertEquals(10000000, testList1.get(24)); + assertEquals(-11, testList1.get(6)); + String out = testList1.toString(); + assertEquals(out, "[0, -42, -51, -41, -31, -21, -11, -1, 0, 1, 11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 1000, 10000, 100000, 1000000, 10000000]"); + } + + @Test + void contains() { + ArrayIntList testList1 = new ArrayIntList(); + testList1.addBack(0); + testList1.addBack(-42); + testList1.addBack(-51); + testList1.addBack(-41); + testList1.addBack(-31); + testList1.addBack(-21); + testList1.addBack(-11); + testList1.addBack(-1); + testList1.addBack(0); + testList1.addBack(1); + testList1.addBack(11); + testList1.addBack(21); + testList1.addBack(31); + testList1.addBack(41); + testList1.addBack(51); + testList1.addBack(61); + testList1.addBack(71); + testList1.addBack(81); + testList1.addBack(91); + testList1.addBack(101); + testList1.addBack(1000); + testList1.addBack(10000); + testList1.addBack(100000); + testList1.addBack(1000000); + testList1.addBack(10000000); + assertTrue(testList1.contains(0)); + assertFalse(testList1.contains(24)); + assertTrue(testList1.contains(-31)); + assertTrue(testList1.contains(10000000)); + + String out = testList1.toString(); + assertEquals(out, "[0, -42, -51, -41, -31, -21, -11, -1, 0, 1, 11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 1000, 10000, 100000, 1000000, 10000000]"); + } + + @Test + void indexOf() { + ArrayIntList testList1 = new ArrayIntList(); + testList1.addBack(0); + testList1.addBack(-42); + testList1.addBack(-51); + testList1.addBack(-41); + testList1.addBack(-31); + testList1.addBack(-21); + testList1.addBack(-11); + testList1.addBack(-1); + testList1.addBack(0); + testList1.addBack(1); + testList1.addBack(11); + testList1.addBack(21); + testList1.addBack(31); + testList1.addBack(41); + testList1.addBack(51); + testList1.addBack(61); + testList1.addBack(71); + testList1.addBack(81); + testList1.addBack(91); + testList1.addBack(101); + testList1.addBack(1000); + testList1.addBack(10000); + testList1.addBack(100000); + testList1.addBack(1000000); + testList1.addBack(10000000); + assertEquals(0, testList1.indexOf(0)); + assertEquals(-1, testList1.indexOf(24)); + assertEquals(4, testList1.indexOf(-31)); + assertEquals(24, testList1.indexOf(10000000)); + + String out = testList1.toString(); + assertEquals(out, "[0, -42, -51, -41, -31, -21, -11, -1, 0, 1, 11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 1000, 10000, 100000, 1000000, 10000000]"); + } + + @Test + void isEmpty() { + ArrayIntList testList1 = new ArrayIntList(); + assertTrue(testList1.isEmpty()); + testList1.add(0,-42); + assertFalse(testList1.isEmpty()); + testList1.removeBack(); + assertTrue(testList1.isEmpty()); + String out = testList1.toString(); + assertEquals(out, "[]"); + + ArrayIntList testList2 = new ArrayIntList(); + assertTrue(testList2.isEmpty()); + testList2.add(0,-42); + assertFalse(testList2.isEmpty()); + testList2.removeFront(); + assertTrue(testList2.isEmpty()); + out = testList2.toString(); + assertEquals(out, "[]"); + } + + @Test + void size() { + ArrayIntList testList1 = new ArrayIntList(); + assertEquals(0, testList1.size()); + testList1.addBack(0); + assertEquals(1, testList1.size()); + testList1.addBack(-42); + testList1.addBack(-51); + testList1.addBack(-41); + testList1.addBack(-31); + testList1.addBack(-21); + testList1.addBack(-11); + testList1.addBack(-1); + testList1.addBack(0); + testList1.addBack(1); + testList1.addBack(11); + testList1.addBack(21); + testList1.addBack(31); + testList1.addBack(41); + testList1.addBack(51); + testList1.addBack(61); + testList1.addBack(71); + testList1.addBack(81); + testList1.addBack(91); + testList1.addBack(101); + testList1.addBack(1000); + testList1.addBack(10000); + testList1.addBack(100000); + testList1.addBack(1000000); + testList1.addBack(10000000); + assertEquals(25, testList1.size()); + + String out = testList1.toString(); + assertEquals(out, "[0, -42, -51, -41, -31, -21, -11, -1, 0, 1, 11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 1000, 10000, 100000, 1000000, 10000000]"); + testList1.clear(); + assertEquals(0, testList1.size()); + } + + @Test + void clear() { + ArrayIntList testList1 = new ArrayIntList(); + testList1.addBack(0); + testList1.addBack(-42); + testList1.addBack(-51); + testList1.addBack(-41); + testList1.addBack(-31); + testList1.addBack(-21); + testList1.addBack(-11); + testList1.addBack(-1); + testList1.addBack(0); + testList1.addBack(1); + testList1.addBack(11); + testList1.addBack(21); + testList1.addBack(31); + testList1.addBack(41); + testList1.addBack(51); + testList1.addBack(61); + testList1.addBack(71); + testList1.addBack(81); + testList1.addBack(91); + testList1.addBack(101); + testList1.addBack(1000); + testList1.addBack(10000); + testList1.addBack(100000); + testList1.addBack(1000000); + testList1.addBack(10000000); + testList1.clear(); + + String out = testList1.toString(); + assertEquals(out, "[]"); + } +} \ No newline at end of file diff --git a/src/LinkedIntList.java b/src/LinkedIntList.java new file mode 100644 index 0000000..fe0bb78 --- /dev/null +++ b/src/LinkedIntList.java @@ -0,0 +1,299 @@ +import java.util.Iterator; +import java.util.NoSuchElementException; + +public class LinkedIntList implements IntList +{ + int size; + IntNode headNode; + IntNode tailNode; + public LinkedIntList() + { + 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. + * This is a fast function. O(1), no loops required. + * + * @param value value to be inserted + */ + @Override + public void addFront(int value) + { + if(size == 0) + { + headNode = new IntNode(value); + tailNode = headNode; + } + else + { + headNode = new IntNode(value, headNode); + } + size++; + } + + @Override + public void addBack(int value) + { + if(size == 0) + { + headNode = new IntNode(value); + tailNode = headNode; + } + else + { + tailNode.setNext(new IntNode(value)); + tailNode = tailNode.nextNode(); + } + size++; + } + + @Override + public void add(int index, int value) throws IndexOutOfBoundsException + { + if(index > size || index < 0) + { + throw new IndexOutOfBoundsException(index + " is outside Array."); + } + if(index == 0) + { + addFront(value); + } + else if(index == size-2) + { + addBack(value); + } + else + { + IntNode tempNode = headNode; + for (int i = 0; i < index -1; i ++) + { + tempNode = tempNode.nextNode(); + } + tempNode.setNext(new IntNode(value, tempNode.nextNode())); + size++; + } + } + + @Override + public void removeFront() + { + if(size == 0) + { + throw new IndexOutOfBoundsException("Attempt to remove from empty arrayList."); + } + if(size == 1) + { + clear(); + } + else { + headNode = headNode.nextNode(); + size--; + } + } + + @Override + public void removeBack() + { + if(size == 0) + { + throw new IndexOutOfBoundsException("Attempt to remove from empty arrayList."); + } + if(size == 1) + { + clear(); + } + else + { + remove(size - 1); + } + } + + @Override + public int remove(int index) + { + if(size == 0) + { + throw new IndexOutOfBoundsException("Attempt to remove from empty arrayList."); + } + checkIndex(index); + if(size == 1) + { + int returnValue = headNode.innerValue; + clear(); + return returnValue; + } + if(index == 0) + { + int returnValue = headNode.innerValue; + removeFront(); + return returnValue; + } + else + { + IntNode tempNode = headNode; + int returnValue; + for (int i = 0; i < index - 1; i++) + { + tempNode = tempNode.nextNode(); + } + returnValue = tempNode.nextNode().innerValue; + if (index == size - 1) + { + tailNode = tempNode; + } + tempNode.setNext(tempNode.nextNode().nextNode()); + size--; + return returnValue; + } + } + + @Override + public int get(int index) + { + checkIndex(index); + if(index == size-1) + { + return tailNode.innerValue; + } + else + { + IntNode tempNode = headNode; + for(int i = 0; i < index; i++) + { + tempNode = tempNode.nextNode(); + } + return tempNode.innerValue; + } + } + + @Override + public boolean contains(int value) + { + IntNode tempNode = headNode; + for(int i = 0; i < size; i++) + { + if(tempNode.innerValue == value) + { + return true; + } + tempNode = tempNode.nextNode(); + } + return false; + } + + @Override + public int indexOf(int value) { + IntNode tempNode = headNode; + for(int i = 0; i < size; i++) + { + if(tempNode.innerValue == value) + { + return i; + } + tempNode = tempNode.nextNode(); + } + return -1; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + @Override + public int size() { + return size; + } + + @Override + public void clear() + { + headNode = null; + tailNode = null; + size = 0; + } + + @Override + public String toString() + { + StringBuilder outputString = new StringBuilder("["); + if(headNode!= null) + { + outputString.append(headNode.innerValue); + IntNode tempNode = headNode; + + while (tempNode.nextNode() != null) { + tempNode = tempNode.nextNode(); + outputString.append(", ").append(tempNode.innerValue); + } + } + outputString.append("]"); + return outputString.toString(); + } + private void checkIndex(int index) throws IndexOutOfBoundsException + { + if(index >= size || index < 0) + { + throw new IndexOutOfBoundsException(index + " is outside Array."); + } + } + + @Override + public Iterator iterator() { + return new LinkedIntListIterator(); + } + private class LinkedIntListIterator implements Iterator + { + private IntNode position; + + public LinkedIntListIterator() + { + position = headNode; + } + + public boolean hasNext() + { + return position != null; + } + + public Integer next() + { + if(position != null) + { + Integer output = position.innerValue; + position = position.nextNode(); + return output; + } + throw new NoSuchElementException("No elements left."); + } + } + private static class IntNode + { + public int innerValue; + private IntNode nodePointer; + + public IntNode( int x ) + { + this(x, null); + } + public IntNode ( int x, IntNode connect) + { + innerValue = x; + nodePointer = connect; + } + public IntNode nextNode() + { + return nodePointer; + } + public void setNext(IntNode x) + { + nodePointer = x; + } + public boolean hasNext() + { + return nodePointer != null; + } + } +} diff --git a/src/LinkedIntListTest.java b/src/LinkedIntListTest.java new file mode 100644 index 0000000..ab85d13 --- /dev/null +++ b/src/LinkedIntListTest.java @@ -0,0 +1,553 @@ +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class LinkedIntListTest { + + // List of zero + @Test + void addEmptyFront() + { + // List of one + LinkedIntList testList1 = new LinkedIntList(); + testList1.addFront(-42); + String out = testList1.toString(); + assertEquals(out, "[-42]"); + + LinkedIntList testList2 = new LinkedIntList(); + testList2.addFront(0); + out = testList2.toString(); + assertEquals(out, "[0]"); + + LinkedIntList testList3 = new LinkedIntList(); + testList3.addFront(420000); + out = testList3.toString(); + assertEquals(out, "[420000]"); + } + // List of one + @Test + void addToListOfOneFront() + { + LinkedIntList testList1 = new LinkedIntList(); + testList1.addFront(0); + testList1.addFront(-42); + String out = testList1.toString(); + assertEquals(out, "[-42, 0]"); + + LinkedIntList testList2 = new LinkedIntList(); + testList2.addFront(0); + testList2.addFront(0); + out = testList2.toString(); + assertEquals(out, "[0, 0]"); + + LinkedIntList testList3 = new LinkedIntList(); + testList3.addFront(0); + testList3.addFront(420000); + out = testList3.toString(); + assertEquals(out, "[420000, 0]"); + } + // List of large + @Test + void addToListOfManyFront() + { + LinkedIntList testList1 = new LinkedIntList(); + testList1.addFront(0); + testList1.addFront(-42); + testList1.addFront(-51); + testList1.addFront(-41); + testList1.addFront(-31); + testList1.addFront(-21); + testList1.addFront(-11); + testList1.addFront(-1); + testList1.addFront(0); + testList1.addFront(1); + testList1.addFront(11); + testList1.addFront(21); + testList1.addFront(31); + testList1.addFront(41); + testList1.addFront(51); + testList1.addFront(61); + testList1.addFront(71); + testList1.addFront(81); + testList1.addFront(91); + testList1.addFront(101); + testList1.addFront(1000); + testList1.addFront(10000); + testList1.addFront(100000); + testList1.addFront(1000000); + testList1.addFront(10000000); + String out = testList1.toString(); + assertEquals(out, "[10000000, 1000000, 100000, 10000, 1000, 101, 91, 81, 71, 61, 51, 41, 31, 21, 11, 1, 0, -1, -11, -21, -31, -41, -51, -42, 0]"); + } + + @Test + void addEmptyBack() + { + LinkedIntList testList1 = new LinkedIntList(); + testList1.addBack(-42); + String out = testList1.toString(); + assertEquals(out, "[-42]"); + + LinkedIntList testList2 = new LinkedIntList(); + testList2.addBack(0); + out = testList2.toString(); + assertEquals(out, "[0]"); + + LinkedIntList testList3 = new LinkedIntList(); + testList3.addBack(420000); + out = testList3.toString(); + assertEquals(out, "[420000]"); + } + + @Test + void addToListOfOneBack() + { + LinkedIntList testList1 = new LinkedIntList(); + testList1.addBack(0); + testList1.addBack(-42); + String out = testList1.toString(); + assertEquals(out, "[0, -42]"); + + LinkedIntList testList2 = new LinkedIntList(); + testList2.addBack(0); + testList2.addBack(0); + out = testList2.toString(); + assertEquals(out, "[0, 0]"); + + LinkedIntList testList3 = new LinkedIntList(); + testList3.addBack(0); + testList3.addBack(420000); + out = testList3.toString(); + assertEquals(out, "[0, 420000]"); + } + @Test + void addToListOfManyBack() { + LinkedIntList testList1 = new LinkedIntList(); + testList1.addBack(0); + testList1.addBack(-42); + testList1.addBack(-51); + testList1.addBack(-41); + testList1.addBack(-31); + testList1.addBack(-21); + testList1.addBack(-11); + testList1.addBack(-1); + testList1.addBack(0); + testList1.addBack(1); + testList1.addBack(11); + testList1.addBack(21); + testList1.addBack(31); + testList1.addBack(41); + testList1.addBack(51); + testList1.addBack(61); + testList1.addBack(71); + testList1.addBack(81); + testList1.addBack(91); + testList1.addBack(101); + testList1.addBack(1000); + testList1.addBack(10000); + testList1.addBack(100000); + testList1.addBack(1000000); + testList1.addBack(10000000); + String out = testList1.toString(); + assertEquals(out, "[0, -42, -51, -41, -31, -21, -11, -1, 0, 1, 11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 1000, 10000, 100000, 1000000, 10000000]"); + } + + @Test + void addToEmpty() { + LinkedIntList testList1 = new LinkedIntList(); + testList1.add(0,-42); + String out = testList1.toString(); + assertEquals(out, "[-42]"); + + LinkedIntList testList2 = new LinkedIntList(); + testList2.add(0,0); + out = testList2.toString(); + assertEquals(out, "[0]"); + + LinkedIntList testList3 = new LinkedIntList(); + testList3.add(0,420000); + out = testList3.toString(); + assertEquals(out, "[420000]"); + } + + @Test + void removeFrontEmptyList() { + LinkedIntList testList1 = new LinkedIntList(); + try + { + testList1.removeFront(); + } + catch(Exception e) + { + assertEquals(e.getClass(), IndexOutOfBoundsException.class); + assertEquals(e.getMessage(), "Attempt to remove from empty arrayList."); + } + } + @Test + void removeFrontListOfOne() { + LinkedIntList testList1 = new LinkedIntList(); + testList1.add(0,-42); + testList1.removeFront(); + String out = testList1.toString(); + assertEquals(out, "[]"); + + LinkedIntList testList2 = new LinkedIntList(); + testList2.add(0,0); + testList2.removeFront(); + out = testList2.toString(); + assertEquals(out, "[]"); + + LinkedIntList testList3 = new LinkedIntList(); + testList3.add(0,420000); + testList3.removeFront(); + out = testList3.toString(); + assertEquals(out, "[]"); + + } + @Test + void removeFrontLargeList() { + LinkedIntList testList1 = new LinkedIntList(); + testList1.addBack(0); + testList1.addBack(-42); + testList1.addBack(-51); + testList1.addBack(-41); + testList1.addBack(-31); + testList1.addBack(-21); + testList1.addBack(-11); + testList1.addBack(-1); + testList1.addBack(0); + testList1.addBack(1); + testList1.addBack(11); + testList1.addBack(21); + testList1.addBack(31); + testList1.addBack(41); + testList1.addBack(51); + testList1.addBack(61); + testList1.addBack(71); + testList1.addBack(81); + testList1.addBack(91); + testList1.addBack(101); + testList1.addBack(1000); + testList1.addBack(10000); + testList1.addBack(100000); + testList1.addBack(1000000); + testList1.addBack(10000000); + testList1.removeFront(); + String out = testList1.toString(); + assertEquals(out, "[-42, -51, -41, -31, -21, -11, -1, 0, 1, 11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 1000, 10000, 100000, 1000000, 10000000]"); + } + + @Test + void removeBackEmptyList() + { + LinkedIntList testList1 = new LinkedIntList(); + try + { + testList1.removeBack(); + } + catch(Exception e) + { + assertEquals(e.getClass(), IndexOutOfBoundsException.class); + assertEquals(e.getMessage(), "Attempt to remove from empty arrayList."); + } + } + @Test + void removeBackListOfOne() + { + LinkedIntList testList1 = new LinkedIntList(); + testList1.add(0,-42); + testList1.removeBack(); + String out = testList1.toString(); + assertEquals(out, "[]"); + + LinkedIntList testList2 = new LinkedIntList(); + testList2.add(0,0); + testList2.removeBack(); + out = testList2.toString(); + assertEquals(out, "[]"); + + LinkedIntList testList3 = new LinkedIntList(); + testList3.add(0,420000); + testList3.removeBack(); + out = testList3.toString(); + assertEquals(out, "[]"); + + } + @Test + void removeBackLargeList() + { + LinkedIntList testList1 = new LinkedIntList(); + testList1.addBack(0); + testList1.addBack(-42); + testList1.addBack(-51); + testList1.addBack(-41); + testList1.addBack(-31); + testList1.addBack(-21); + testList1.addBack(-11); + testList1.addBack(-1); + testList1.addBack(0); + testList1.addBack(1); + testList1.addBack(11); + testList1.addBack(21); + testList1.addBack(31); + testList1.addBack(41); + testList1.addBack(51); + testList1.addBack(61); + testList1.addBack(71); + testList1.addBack(81); + testList1.addBack(91); + testList1.addBack(101); + testList1.addBack(1000); + testList1.addBack(10000); + testList1.addBack(100000); + testList1.addBack(1000000); + testList1.addBack(10000000); + testList1.removeBack(); + String out = testList1.toString(); + assertEquals(out, "[0, -42, -51, -41, -31, -21, -11, -1, 0, 1, 11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 1000, 10000, 100000, 1000000]"); + } + + @Test + void remove() + { + LinkedIntList testList1 = new LinkedIntList(); + testList1.addBack(0); + testList1.addBack(-42); + testList1.addBack(-51); + testList1.addBack(-41); + testList1.addBack(-31); + testList1.addBack(-21); + testList1.addBack(-11); + testList1.addBack(-1); + testList1.addBack(0); + testList1.addBack(1); + testList1.addBack(11); + testList1.addBack(21); + testList1.addBack(31); + testList1.addBack(41); + testList1.addBack(51); + testList1.addBack(61); + testList1.addBack(71); + testList1.addBack(81); + testList1.addBack(91); + testList1.addBack(101); + testList1.addBack(1000); + testList1.addBack(10000); + testList1.addBack(100000); + testList1.addBack(1000000); + testList1.addBack(10000000); + assertEquals(0, testList1.remove(0)); + String out = testList1.toString(); + assertEquals(out, "[-42, -51, -41, -31, -21, -11, -1, 0, 1, 11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 1000, 10000, 100000, 1000000, 10000000]"); + + assertEquals(10000000, testList1.remove(23)); + out = testList1.toString(); + assertEquals(out, "[-42, -51, -41, -31, -21, -11, -1, 0, 1, 11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 1000, 10000, 100000, 1000000]"); + + assertEquals(21, testList1.remove(10)); + out = testList1.toString(); + assertEquals(out, "[-42, -51, -41, -31, -21, -11, -1, 0, 1, 11, 31, 41, 51, 61, 71, 81, 91, 101, 1000, 10000, 100000, 1000000]"); + } + + @Test + void get() { + LinkedIntList testList1 = new LinkedIntList(); + testList1.addBack(0); + testList1.addBack(-42); + testList1.addBack(-51); + testList1.addBack(-41); + testList1.addBack(-31); + testList1.addBack(-21); + testList1.addBack(-11); + testList1.addBack(-1); + testList1.addBack(0); + testList1.addBack(1); + testList1.addBack(11); + testList1.addBack(21); + testList1.addBack(31); + testList1.addBack(41); + testList1.addBack(51); + testList1.addBack(61); + testList1.addBack(71); + testList1.addBack(81); + testList1.addBack(91); + testList1.addBack(101); + testList1.addBack(1000); + testList1.addBack(10000); + testList1.addBack(100000); + testList1.addBack(1000000); + testList1.addBack(10000000); + assertEquals(0, testList1.get(0)); + assertEquals(10000000, testList1.get(24)); + assertEquals(-11, testList1.get(6)); + String out = testList1.toString(); + assertEquals(out, "[0, -42, -51, -41, -31, -21, -11, -1, 0, 1, 11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 1000, 10000, 100000, 1000000, 10000000]"); + } + + @Test + void contains() { + LinkedIntList testList1 = new LinkedIntList(); + testList1.addBack(0); + testList1.addBack(-42); + testList1.addBack(-51); + testList1.addBack(-41); + testList1.addBack(-31); + testList1.addBack(-21); + testList1.addBack(-11); + testList1.addBack(-1); + testList1.addBack(0); + testList1.addBack(1); + testList1.addBack(11); + testList1.addBack(21); + testList1.addBack(31); + testList1.addBack(41); + testList1.addBack(51); + testList1.addBack(61); + testList1.addBack(71); + testList1.addBack(81); + testList1.addBack(91); + testList1.addBack(101); + testList1.addBack(1000); + testList1.addBack(10000); + testList1.addBack(100000); + testList1.addBack(1000000); + testList1.addBack(10000000); + assertTrue(testList1.contains(0)); + assertFalse(testList1.contains(24)); + assertTrue(testList1.contains(-31)); + assertTrue(testList1.contains(10000000)); + + String out = testList1.toString(); + assertEquals(out, "[0, -42, -51, -41, -31, -21, -11, -1, 0, 1, 11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 1000, 10000, 100000, 1000000, 10000000]"); + } + + @Test + void indexOf() { + LinkedIntList testList1 = new LinkedIntList(); + testList1.addBack(0); + testList1.addBack(-42); + testList1.addBack(-51); + testList1.addBack(-41); + testList1.addBack(-31); + testList1.addBack(-21); + testList1.addBack(-11); + testList1.addBack(-1); + testList1.addBack(0); + testList1.addBack(1); + testList1.addBack(11); + testList1.addBack(21); + testList1.addBack(31); + testList1.addBack(41); + testList1.addBack(51); + testList1.addBack(61); + testList1.addBack(71); + testList1.addBack(81); + testList1.addBack(91); + testList1.addBack(101); + testList1.addBack(1000); + testList1.addBack(10000); + testList1.addBack(100000); + testList1.addBack(1000000); + testList1.addBack(10000000); + assertEquals(0, testList1.indexOf(0)); + assertEquals(-1, testList1.indexOf(24)); + assertEquals(4, testList1.indexOf(-31)); + assertEquals(24, testList1.indexOf(10000000)); + + String out = testList1.toString(); + assertEquals(out, "[0, -42, -51, -41, -31, -21, -11, -1, 0, 1, 11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 1000, 10000, 100000, 1000000, 10000000]"); + } + + @Test + void isEmpty() { + LinkedIntList testList1 = new LinkedIntList(); + assertTrue(testList1.isEmpty()); + testList1.add(0,-42); + assertFalse(testList1.isEmpty()); + testList1.removeBack(); + assertTrue(testList1.isEmpty()); + String out = testList1.toString(); + assertEquals(out, "[]"); + + LinkedIntList testList2 = new LinkedIntList(); + assertTrue(testList2.isEmpty()); + testList2.add(0,-42); + assertFalse(testList2.isEmpty()); + testList2.removeFront(); + assertTrue(testList2.isEmpty()); + out = testList2.toString(); + assertEquals(out, "[]"); + } + + @Test + void size() { + LinkedIntList testList1 = new LinkedIntList(); + assertEquals(0, testList1.size()); + testList1.addBack(0); + assertEquals(1, testList1.size()); + testList1.addBack(-42); + testList1.addBack(-51); + testList1.addBack(-41); + testList1.addBack(-31); + testList1.addBack(-21); + testList1.addBack(-11); + testList1.addBack(-1); + testList1.addBack(0); + testList1.addBack(1); + testList1.addBack(11); + testList1.addBack(21); + testList1.addBack(31); + testList1.addBack(41); + testList1.addBack(51); + testList1.addBack(61); + testList1.addBack(71); + testList1.addBack(81); + testList1.addBack(91); + testList1.addBack(101); + testList1.addBack(1000); + testList1.addBack(10000); + testList1.addBack(100000); + testList1.addBack(1000000); + testList1.addBack(10000000); + assertEquals(25, testList1.size()); + + String out = testList1.toString(); + assertEquals(out, "[0, -42, -51, -41, -31, -21, -11, -1, 0, 1, 11, 21, 31, 41, 51, 61, 71, 81, 91, 101, 1000, 10000, 100000, 1000000, 10000000]"); + testList1.clear(); + assertEquals(0, testList1.size()); + } + + @Test + void clear() { + LinkedIntList testList1 = new LinkedIntList(); + testList1.addBack(0); + testList1.addBack(-42); + testList1.addBack(-51); + testList1.addBack(-41); + testList1.addBack(-31); + testList1.addBack(-21); + testList1.addBack(-11); + testList1.addBack(-1); + testList1.addBack(0); + testList1.addBack(1); + testList1.addBack(11); + testList1.addBack(21); + testList1.addBack(31); + testList1.addBack(41); + testList1.addBack(51); + testList1.addBack(61); + testList1.addBack(71); + testList1.addBack(81); + testList1.addBack(91); + testList1.addBack(101); + testList1.addBack(1000); + testList1.addBack(10000); + testList1.addBack(100000); + testList1.addBack(1000000); + testList1.addBack(10000000); + testList1.clear(); + + String out = testList1.toString(); + assertEquals(out, "[]"); + } +} \ No newline at end of file diff --git a/src/Main.java b/src/Main.java index 930198c..c12a751 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,15 +1,62 @@ //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!"); + public static void main(String[] args) + { + //IntList list1 = new ArrayIntList(); + IntList list1 = new LinkedIntList(); - 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); + list1.addBack(1); + list1.addBack(2); + list1.addBack(3); + list1.addBack(4); + list1.add(2, 5); + list1.addFront(9); + list1.removeBack(); + list1.removeFront(); + list1.clear(); + list1.addBack(1); + list1.addBack(2); + list1.addBack(3); + list1.addBack(1); + list1.addBack(2); + list1.addBack(3); + list1.addBack(1); + list1.addBack(2); + list1.addBack(3); + list1.addBack(1); + list1.addBack(2); + list1.addBack(3); + for(int i : list1) + { + System.out.printf(i + ", "); } + System.out.println(); + System.out.println(list1); + + System.out.println(list1.contains(2)); + System.out.println(list1.indexOf(2)); + System.out.println(list1.contains(4)); + System.out.println(list1.indexOf(4)); + System.out.println(list1.size()); + System.out.println(list1.isEmpty()); + list1.clear(); + System.out.println(list1.isEmpty()); + list1.addBack(5); + System.out.println(list1.isEmpty()); + list1.removeFront(); + System.out.println(list1.isEmpty()); + + list1.addBack(1); + list1.addBack(2); + list1.addBack(3); + list1.addBack(4); + list1.add(2, 5); + list1.addFront(9); + System.out.println(list1); + System.out.println(list1.get(2)); + System.out.println(list1); + list1.remove(2); + System.out.println(list1); } } \ No newline at end of file