From cb933c7943e6a6a2ed222087ca0cf4085b8f386a Mon Sep 17 00:00:00 2001 From: Jennifer McNiel Date: Tue, 13 Feb 2024 10:53:01 -0800 Subject: [PATCH 1/2] Added stubs for Arraylist and LinkedList --- src/ArrayIntList.java | 208 +++++++++++++++++++++++++++++++++++++++++ src/LinkedIntList.java | 147 +++++++++++++++++++++++++++++ src/Main.java | 43 ++++++++- 3 files changed, 393 insertions(+), 5 deletions(-) create mode 100644 src/ArrayIntList.java create mode 100644 src/LinkedIntList.java diff --git a/src/ArrayIntList.java b/src/ArrayIntList.java new file mode 100644 index 0000000..e339152 --- /dev/null +++ b/src/ArrayIntList.java @@ -0,0 +1,208 @@ +import java.util.Iterator; + +public class ArrayIntList implements IntList{ + + /** + * 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 + */ + + private int[] buffer; + private int size; + private final static int INITIAL_CAPACITY = 5; + + + public ArrayIntList(){ + buffer = new int[INITIAL_CAPACITY]; + size = 0; + } + @Override + public void addFront(int value) { + if (size == buffer.length ){ + resize(buffer.length+10); + } + for (int i = size; i >0; i--){ + buffer[i] = buffer[i-1]; + } + buffer[0] = value; + size ++; + + } +private void resize(int newSize){ + // create a new larger array of the new size + int[] newBuffer = new int[newSize]; + //copy values from existing values + for(int i = 0; i< size; i++){ + newBuffer[i] = buffer[i]; + } + //make switchover + buffer = newBuffer; +} + + /** + * 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 ){ + resize(buffer.length+10); + } + 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) { + + } + + /** + * 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() { + size--; + for (int i = 0; i iterator() { + return null; + } + + @Override + public String toString() { + if(size == 0){ + return "[]"; + } + + StringBuilder sb = new StringBuilder(); + sb.append("["); + sb.append(buffer[0]); + for(int i = 1; i iterator() { + return null; + } +} diff --git a/src/Main.java b/src/Main.java index 930198c..b9f9c47 100644 --- a/src/Main.java +++ b/src/Main.java @@ -5,11 +5,44 @@ 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!"); + System.out.println(); +// 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); +// } + + //arrays in Java are fixed size + int[] arrayOfNumbers = new int[10]; + + IntList list1 = new ArrayIntList(); + IntList list2 = new LinkedIntList(); + + list1.addBack(42); + list1.addBack(82); + list1.addBack(97); + list1.addBack(97); + list1.addBack(97); + list1.addBack(97); + list1.addBack(97); + list1.addBack(97); + list1.addBack(97); + list1.addBack(97); + list1.addBack(97); + list1.addBack(97); + list1.addBack(1); + System.out.println(list1); + + list1.addFront(55); + System.out.println(list1); + + list1.removeBack(); + System.out.println(list1); + + list1.removeFront(); + System.out.println(list1); + + - 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 From fc356eba353629d265b9cdd37f87eafacf1728c2 Mon Sep 17 00:00:00 2001 From: Jennifer McNiel Date: Tue, 19 Mar 2024 21:19:33 -0700 Subject: [PATCH 2/2] Completed all methods and Unit tests --- IntListReview.iml | 32 +++ src/ArrayIntList.java | 114 ++++++++- src/ArrayIntListTest.java | 506 +++++++++++++++++++++++++++++++++++++ src/LinkedIntList.java | 177 ++++++++++++- src/LinkedIntListTest.java | 66 +++++ src/Main.java | 72 +++++- 6 files changed, 936 insertions(+), 31 deletions(-) create mode 100644 src/ArrayIntListTest.java create mode 100644 src/LinkedIntListTest.java diff --git a/IntListReview.iml b/IntListReview.iml index c90834f..ff79d70 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 index e339152..8ec6ffe 100644 --- a/src/ArrayIntList.java +++ b/src/ArrayIntList.java @@ -1,4 +1,5 @@ import java.util.Iterator; +import java.util.NoSuchElementException; public class ArrayIntList implements IntList{ @@ -19,6 +20,19 @@ public ArrayIntList(){ buffer = new int[INITIAL_CAPACITY]; size = 0; } + + /** + * Prepends (inserts) the specified value at the front of the list (at index) + * Shifts the value currently at the front of the list (if any) and any + * subsequent values to the right + * + * This is a relatively slow operation + * Linear time - O(size) + * all values in the buffer have to be shifted to the right + * resize will also increase the linear time + * + * @param value value to be inserted + */ @Override public void addFront(int value) { if (size == buffer.length ){ @@ -68,6 +82,19 @@ public void addBack(int value) { @Override public void add(int index, int value) { + if (index < 0 || index > size){ + throw new IndexOutOfBoundsException("Index out of Range."); + }else{ + if (size == buffer.length ){ + resize(buffer.length+10); + } + for (int i = size; i >index; i--){ + buffer[i] = buffer[i-1]; + } + buffer[index]= value; + size ++; + } + } /** @@ -77,6 +104,10 @@ public void add(int index, int value) { */ @Override public void removeFront() { + if (size ==0){ + throw new IllegalStateException("Already Empty!"); + } + size--; for (int i = 0; i size - 1) { + throw new IndexOutOfBoundsException("Index out of Range."); + } else { + int removedValue = buffer[index]; + size--; + for (int i = index; i < size; i++){ + buffer[i] = buffer[i+1]; + } + buffer[size]=0; + return removedValue; + } } /** @@ -122,7 +163,7 @@ public int remove(int index) { */ @Override public int get(int index) { - return 0; + return buffer[index]; } /** @@ -133,7 +174,14 @@ public int get(int index) { */ @Override public boolean contains(int value) { - return false; + boolean result = false; + for(int num : buffer){ + if(num == value){ + result = true; + break; + } + } + return result; } /** @@ -146,7 +194,12 @@ public boolean contains(int value) { */ @Override public int indexOf(int value) { - return 0; + for(int i = 0; i < size; i++){ + if(buffer[i] == value){ + return i; + } + } + return -1; } /** @@ -156,7 +209,8 @@ public int indexOf(int value) { */ @Override public boolean isEmpty() { - return false; + + return !(size > 0); } /** @@ -166,7 +220,7 @@ public boolean isEmpty() { */ @Override public int size() { - return 0; + return size; } /** @@ -175,7 +229,8 @@ public int size() { */ @Override public void clear() { - + buffer = new int[0]; + size = 0; } /** @@ -185,7 +240,7 @@ public void clear() { */ @Override public Iterator iterator() { - return null; + return new ArrayIntListIterator(); } @Override @@ -205,4 +260,47 @@ public String toString() { sb.append("]"); return sb.toString(); } + + // nested / inner/ helper class + public class ArrayIntListIterator implements Iterator{ + private int currentPosition; + + public ArrayIntListIterator() { + currentPosition = 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() { +// if (currentPosition < size) { +// return true; +// }else { +// return false; +// } + return (currentPosition < 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 (!hasNext()){ + throw new NoSuchElementException(); + } + int value = get(currentPosition); + currentPosition ++; + return value; + } + } + } diff --git a/src/ArrayIntListTest.java b/src/ArrayIntListTest.java new file mode 100644 index 0000000..c83a8c3 --- /dev/null +++ b/src/ArrayIntListTest.java @@ -0,0 +1,506 @@ +import java.util.Iterator; + +import static org.junit.jupiter.api.Assertions.*; + +class ArrayIntListTest { + + + @org.junit.jupiter.api.Test + void addFrontEmptyList() { + ArrayIntList theList = new ArrayIntList(); + theList.addFront(42); + assertEquals(theList.get(0), 42); + } + @org.junit.jupiter.api.Test + void addFront() { + ArrayIntList theList = new ArrayIntList(); + theList.addFront(25); + theList.addFront(42); + assertEquals(theList.get(0), 42); + } + + @org.junit.jupiter.api.Test + void addFrontResize() { + ArrayIntList theList = new ArrayIntList(); + theList.addFront(1); + theList.addFront(2); + theList.addFront(3); + theList.addFront(4); + theList.addFront(5); + theList.addFront(6); + theList.addFront(7); + assertEquals(theList.get(0), 7); + } + + @org.junit.jupiter.api.Test + void addFrontMultipleResize() { + ArrayIntList theList = new ArrayIntList(); + for (int i =0; i < 1000; i++){ + theList.addFront(i); + } + String testString = theList.get(0) + ", " + theList.size() + ", " + theList.get(theList.size()-1); + assertEquals(testString, "999, 1000, 0"); + } + + @org.junit.jupiter.api.Test + void addBackEmptyList() { + ArrayIntList theList = new ArrayIntList(); + theList.addBack(42); + assertEquals(theList.get(0), 42); + + } + @org.junit.jupiter.api.Test + void addBackNonEmptyList() { + ArrayIntList theList = new ArrayIntList(); + theList.addBack(25); + theList.addBack(42); + assertEquals(theList.get(1), 42); + + } + + @org.junit.jupiter.api.Test + void addBackResize() { + ArrayIntList theList = new ArrayIntList(); + theList.addBack(1); + theList.addBack(2); + theList.addBack(3); + theList.addBack(4); + theList.addBack(5); + theList.addBack(6); + theList.addBack(7); + assertEquals(theList.get(6), 7); + } + + @org.junit.jupiter.api.Test + void addBackMultipleResize() { + ArrayIntList theList = new ArrayIntList(); + for (int i =0; i < 1000; i++){ + theList.addBack(i); + } + + assertEquals(theList.get(999), 999); + } + @org.junit.jupiter.api.Test + void addToEmpty() { + ArrayIntList theList = new ArrayIntList(); + theList.add(0,42); + assertEquals(theList.get(0), 42); + } + + @org.junit.jupiter.api.Test + void addToFront() { + ArrayIntList theList = new ArrayIntList(); + theList.add(0,42); + theList.add(0,43); + theList.add(0,45); + assertEquals(theList.get(0), 45); + } + + @org.junit.jupiter.api.Test + void addToMiddle() { + ArrayIntList theList = new ArrayIntList(); + theList.add(0,42); + theList.add(0,43); + theList.add(0,45); + theList.add(1,100); + assertEquals(theList.get(1), 100); + } + + @org.junit.jupiter.api.Test + void addToBack() { + ArrayIntList theList = new ArrayIntList(); + theList.add(0,42); + theList.add(0,43); + theList.add(0,45); + theList.add(3,100); + assertEquals(theList.get(3), 100); + } + + @org.junit.jupiter.api.Test + void addWithResize() { + ArrayIntList theList = new ArrayIntList(); + theList.add(0,42); + theList.add(0,43); + theList.add(0,45); + theList.add(3,100); + theList.add(0,33); + theList.add(0,3); + theList.add(3,75); + assertEquals(theList.get(3), 75); + } + @org.junit.jupiter.api.Test + void addOutOfRange() { + ArrayIntList theList = new ArrayIntList(); + assertThrowsExactly(IndexOutOfBoundsException.class, ()->theList.add(2,5)); + } + + + @org.junit.jupiter.api.Test + void removeFrontFromEmpty() { + ArrayIntList theList = new ArrayIntList(); + + assertThrowsExactly(IllegalStateException.class, theList::removeFront); + } + + @org.junit.jupiter.api.Test + void removeFrontFrom1ItemList() { + ArrayIntList theList = new ArrayIntList(); + theList.addFront(1); + theList.removeFront(); + String out = theList.toString(); + assertEquals(out, "[]"); + } + + @org.junit.jupiter.api.Test + void removeFrontFromManyList() { + ArrayIntList theList = new ArrayIntList(); + for (int i = 1; i <=10; i++){ + theList.addBack(i); + } + + theList.removeFront(); + + String out = theList.toString(); + + assertEquals(out, "[2, 3, 4, 5, 6, 7, 8, 9, 10]"); + } + + @org.junit.jupiter.api.Test + void removeBackFromEmpty() { + ArrayIntList theList = new ArrayIntList(); + + assertThrowsExactly(IllegalStateException.class, theList::removeBack); + } + @org.junit.jupiter.api.Test + void removeBackFrom1ItemList() { + ArrayIntList theList = new ArrayIntList(); + theList.addBack(1); + theList.removeBack(); + String out = theList.toString(); + assertEquals(out, "[]"); + } + @org.junit.jupiter.api.Test + void removeBackFromManyList() { + ArrayIntList theList = new ArrayIntList(); + for (int i = 1; i <=10; i++){ + theList.addBack(i); + } + + theList.removeBack(); + + String out = theList.toString(); + + assertEquals(out, "[1, 2, 3, 4, 5, 6, 7, 8, 9]"); + } + + + @org.junit.jupiter.api.Test + void removeFromEmpty() { + ArrayIntList theList = new ArrayIntList(); + + assertThrowsExactly(IndexOutOfBoundsException.class, ()->theList.remove(0)); + } + + @org.junit.jupiter.api.Test + void removeOutOfRange() { + ArrayIntList theList = new ArrayIntList(); + assertThrowsExactly(IndexOutOfBoundsException.class, ()->theList.remove(2)); + } + @org.junit.jupiter.api.Test + void remove1ItemList() { + ArrayIntList theList = new ArrayIntList(); + theList.addFront(1); + theList.remove(0); + String out = theList.toString(); + assertEquals(out, "[]"); + } + + @org.junit.jupiter.api.Test + void removeFromFront() { + ArrayIntList theList = new ArrayIntList(); + for (int i = 1; i <=10; i++){ + theList.addBack(i); + } + + theList.remove(0); + + String out = theList.toString(); + + assertEquals(out, "[2, 3, 4, 5, 6, 7, 8, 9, 10]"); + } + + @org.junit.jupiter.api.Test + void removeFromMiddle() { + ArrayIntList theList = new ArrayIntList(); + for (int i = 1; i <=10; i++){ + theList.addBack(i); + } + + theList.remove(3); + + String out = theList.toString(); + + assertEquals(out, "[1, 2, 3, 5, 6, 7, 8, 9, 10]"); + } + + @org.junit.jupiter.api.Test + void removeFromBack() { + ArrayIntList theList = new ArrayIntList(); + for (int i = 1; i <=10; i++){ + theList.addBack(i); + } + + theList.remove(9); + + String out = theList.toString(); + + assertEquals(out, "[1, 2, 3, 4, 5, 6, 7, 8, 9]"); + } + @org.junit.jupiter.api.Test + void getBeginning() { + ArrayIntList theList = new ArrayIntList(); + for (int i = 1; i <=10; i++){ + theList.addBack(i); + } + + assertEquals(theList.get(0), 1); + } + + @org.junit.jupiter.api.Test + void getMiddle() { + ArrayIntList theList = new ArrayIntList(); + for (int i = 1; i <=10; i++){ + theList.addBack(i); + } + + assertEquals(theList.get(5), 6); + } + + @org.junit.jupiter.api.Test + void getEnd() { + ArrayIntList theList = new ArrayIntList(); + for (int i = 1; i <=10; i++){ + theList.addBack(i); + } + + assertEquals(theList.get(9), 10); + } + + @org.junit.jupiter.api.Test + void getOORange() { + ArrayIntList theList = new ArrayIntList(); + for (int i = 1; i <=10; i++){ + theList.addBack(i); + } + + assertThrowsExactly(ArrayIndexOutOfBoundsException.class, ()->theList.get(20)); + } + + @org.junit.jupiter.api.Test + void containsBeginning() { + ArrayIntList theList = new ArrayIntList(); + for (int i = 1; i <=10; i++){ + theList.addBack(i); + } + + assertEquals(theList.contains(1), true); + } + + @org.junit.jupiter.api.Test + void containsMiddle() { + ArrayIntList theList = new ArrayIntList(); + for (int i = 1; i <=10; i++){ + theList.addBack(i); + } + + assertEquals(theList.contains(5), true); + } + + @org.junit.jupiter.api.Test + void containsEnd() { + ArrayIntList theList = new ArrayIntList(); + for (int i = 1; i <=10; i++){ + theList.addBack(i); + } + + assertEquals(theList.contains(10), true); + } + + @org.junit.jupiter.api.Test + void containsMissing() { + ArrayIntList theList = new ArrayIntList(); + for (int i = 1; i <=10; i++){ + theList.addBack(i); + } + + assertEquals(theList.contains(90), false); + } + + @org.junit.jupiter.api.Test + void indexOfBeginning() { + ArrayIntList theList = new ArrayIntList(); + for (int i = 1; i <=10; i++){ + theList.addBack(i); + } + + assertEquals(theList.indexOf(1), 0); + + } + + @org.junit.jupiter.api.Test + void indexOfMiddle() { + ArrayIntList theList = new ArrayIntList(); + for (int i = 1; i <=10; i++){ + theList.addBack(i); + } + + assertEquals(theList.indexOf(5), 4); + + } + + @org.junit.jupiter.api.Test + void indexOfEnd() { + ArrayIntList theList = new ArrayIntList(); + for (int i = 1; i <=10; i++){ + theList.addBack(i); + } + + assertEquals(theList.indexOf(10), 9); + + } + + @org.junit.jupiter.api.Test + void indexOfMissing() { + ArrayIntList theList = new ArrayIntList(); + for (int i = 1; i <=10; i++){ + theList.addBack(i); + } + + assertEquals(theList.indexOf(90), -1); + + } + + @org.junit.jupiter.api.Test + void isEmptyFull() { + ArrayIntList theList = new ArrayIntList(); + for (int i = 1; i <=10; i++){ + theList.addBack(i); + } + + assertEquals(theList.isEmpty(), false); + } + + @org.junit.jupiter.api.Test + void isEmptyEmpty() { + ArrayIntList theList = new ArrayIntList(); + + assertEquals(theList.isEmpty(), true); + } + + @org.junit.jupiter.api.Test + void sizeEmpty() { + ArrayIntList theList = new ArrayIntList(); + + + assertEquals(theList.size(), 0); + } + + @org.junit.jupiter.api.Test + void sizeNotEmpty() { + ArrayIntList theList = new ArrayIntList(); + for (int i = 1; i <=10; i++){ + theList.addBack(i); + } + + assertEquals(theList.size(), 10); + } + + @org.junit.jupiter.api.Test + void clear() { + ArrayIntList theList = new ArrayIntList(); + for (int i = 1; i <=10; i++){ + theList.addBack(i); + } + + theList.clear(); + + assertEquals(theList.isEmpty(), true); + } + + @org.junit.jupiter.api.Test + void clearLarge() { + ArrayIntList theList = new ArrayIntList(); + for (int i = 1; i <=100; i++){ + theList.addBack(i); + } + + theList.clear(); + + assertEquals(theList.isEmpty(), true); + } + @org.junit.jupiter.api.Test + void iterator() { + ArrayIntList theList = new ArrayIntList(); + for (int i = 1; i <=10; i++){ + theList.addBack(i); + } + + String out =""; + Iterator itr2 = theList.iterator(); + + while (itr2.hasNext()) { + out = out + itr2.next() + " "; + } + + assertEquals(out, "1 2 3 4 5 6 7 8 9 10 "); + } + + @org.junit.jupiter.api.Test + void iteratorEmpty() { + ArrayIntList theList = new ArrayIntList(); + + String out =""; + Iterator itr2 = theList.iterator(); + + while (itr2.hasNext()) { + out = out + itr2.next() + " "; + } + + assertEquals(out, ""); + } + + @org.junit.jupiter.api.Test + void testToString() { + ArrayIntList theList = new ArrayIntList(); + for (int i = 1; i <=10; i++){ + theList.addBack(i); + } + + String out = theList.toString(); + + assertEquals(out, "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"); + } + + @org.junit.jupiter.api.Test + void testToStringEmpty() { + ArrayIntList theList = new ArrayIntList(); + + String out = theList.toString(); + + assertEquals(out, "[]"); + } + + @org.junit.jupiter.api.Test + void testToStringLong() { + ArrayIntList theList = new ArrayIntList(); + for (int i = 1; i <=50; i++){ + theList.addBack(i); + } + + String out = theList.toString(); + + assertEquals(out, "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23" + + ", 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, " + + "48, 49, 50]"); + } +} \ No newline at end of file diff --git a/src/LinkedIntList.java b/src/LinkedIntList.java index 66c0994..ab8a56a 100644 --- a/src/LinkedIntList.java +++ b/src/LinkedIntList.java @@ -1,6 +1,31 @@ import java.util.Iterator; +import java.util.function.Consumer; public class LinkedIntList implements IntList{ + + public class Node { + int data; + Node next; + + public Node() { + data = 0; + next = null; + } + + public Node(int data, Node next){ + this.data = data; + this.next = next; + } + } + + private Node head; + private int size; + + 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 @@ -10,17 +35,35 @@ public class LinkedIntList implements IntList{ */ @Override public void addFront(int value) { +// Node temp = head; +// head = new Node(value, temp); + Node front = new Node(value, head); + head = front; + size ++; } /** * Appends (inserts) the specified value at the back of the list (at index size()-1). * + *if list is empty runtime is T=5 O(1) constant, if list not empty, T=2n+6 (n = size) O(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 ++; } /** @@ -34,7 +77,20 @@ public void addBack(int value) { */ @Override public void add(int index, int value) { - + if (index < 0 || index > size){ + throw new IndexOutOfBoundsException("Index out of Range."); + }else{ + Node current = head; + if(index == 0){ + head = new Node(value, current); + }else{ + for (int i = 0; i < index-1; i++) { + current = current.next; + } + current.next = new Node(value, current.next); + } + size++; + } } /** @@ -44,7 +100,8 @@ public void add(int index, int value) { */ @Override public void removeFront() { - + head = head.next; + size--; } /** @@ -53,7 +110,13 @@ public void removeFront() { */ @Override public void removeBack() { + Node current = head; + while (current.next.next != null) { + current = current.next; + } + current.next = null; + size --; } /** @@ -67,7 +130,24 @@ public void removeBack() { */ @Override public int remove(int index) { - return 0; + int value = 0; + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("Index out of Range."); + } else { + Node current = head; + if(index == 0){ + value = head.data; + head = head.next; + }else{ + for (int i = 0; i < index-1; i++) { + current = current.next; + } + value = current.next.data; + current.next = current.next.next; + } + size--; + } + return value; } /** @@ -79,7 +159,15 @@ public int remove(int index) { */ @Override public int get(int index) { - return 0; + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("Index out of Range."); + } else { + Node current = head; + for (int i = 0; i < index; i++) { + current = current.next; + } + return current.data; + } } /** @@ -90,6 +178,13 @@ public int get(int index) { */ @Override public boolean contains(int value) { + Node current = head; + while(current!=null){ + if(current.data == value){ + return true; + } + current = current.next; + } return false; } @@ -103,7 +198,16 @@ public boolean contains(int value) { */ @Override public int indexOf(int value) { - return 0; + Node current = head; + int index = 0; + while(current!=null){ + if(current.data == value){ + return index; + } + current = current.next; + index ++; + } + return -1; } /** @@ -113,7 +217,7 @@ public int indexOf(int value) { */ @Override public boolean isEmpty() { - return false; + return head == null; } /** @@ -123,7 +227,7 @@ public boolean isEmpty() { */ @Override public int size() { - return 0; + return size; } /** @@ -132,7 +236,9 @@ public int size() { */ @Override public void clear() { - + head.next = null; + head.data = 0; + size = 0; } /** @@ -142,6 +248,59 @@ public void clear() { */ @Override public Iterator iterator() { - return null; + return new LinkedIterator(); + } + + public void print() { + Node current = head; + + while (current != null) { + System.out.println(current.data); + + current = current.next; + } + + } + + @Override + public String toString() { + if (head == null) { + return "[]"; + } else { + 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(){ + current = head; + } + @Override + public boolean hasNext() { + return current != null; + } + + @Override + public Integer next() { + int result = current.data; + current = current.next; + return result; + } + } } + + diff --git a/src/LinkedIntListTest.java b/src/LinkedIntListTest.java new file mode 100644 index 0000000..1c3c9ae --- /dev/null +++ b/src/LinkedIntListTest.java @@ -0,0 +1,66 @@ +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class LinkedIntListTest { + + @Test + void addFront() { + } + + @Test + void addBack() { + } + + @Test + void add() { + } + + @Test + void removeFront() { + } + + @Test + void removeBack() { + } + + @Test + void remove() { + } + + @Test + void get() { + } + + @Test + void contains() { + } + + @Test + void indexOf() { + } + + @Test + void isEmpty() { + } + + @Test + void size() { + } + + @Test + void clear() { + } + + @Test + void iterator() { + } + + @Test + void print() { + } + + @Test + void testToString() { + } +} \ No newline at end of file diff --git a/src/Main.java b/src/Main.java index b9f9c47..e497e23 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,3 +1,5 @@ +import java.util.Iterator; + //TIP To Run code, press or // click the icon in the gutter. public class Main { @@ -16,24 +18,25 @@ public static void main(String[] args) { int[] arrayOfNumbers = new int[10]; IntList list1 = new ArrayIntList(); + IntList listA = new ArrayIntList(); IntList list2 = new LinkedIntList(); - list1.addBack(42); - list1.addBack(82); - list1.addBack(97); - list1.addBack(97); - list1.addBack(97); - list1.addBack(97); - list1.addBack(97); - list1.addBack(97); - list1.addBack(97); - list1.addBack(97); - list1.addBack(97); - list1.addBack(97); - list1.addBack(1); + list1.addBack(11); + list1.addBack(12); + list1.addBack(13); + list1.addBack(14); + list1.addBack(15); + list1.addBack(16); + list1.addBack(17); + list1.addBack(18); + list1.addBack(19); + list1.addBack(20); + list1.addBack(21); + list1.addBack(22); + list1.addBack(23); System.out.println(list1); - list1.addFront(55); + list1.addFront(24); System.out.println(list1); list1.removeBack(); @@ -42,7 +45,48 @@ public static void main(String[] args) { list1.removeFront(); System.out.println(list1); + list1.add(3, 99); + System.out.println(list1); + +// for (int value : list1) +// System.out.println(value); + + System.out.println(listA.isEmpty()); + + list1.remove(3); + System.out.println(list1); + + System.out.println(list1.contains(13)); + + System.out.println(list1.indexOf(10)); + + list1.clear(); + System.out.println(list1); + list2.addFront(9); + list2.addBack(7); + list2.addBack(4); + + list2.add(3,8); + + System.out.println(list2.toString()); + + System.out.println( list2.isEmpty()); + + System.out.println(list2.toString()); + + System.out.println(list2.indexOf(7)); + + System.out.println(); + +// for (int value : list2) { +// System.out.println(value); +// } +// + Iterator itr2 = list2.iterator(); + while (itr2.hasNext()) { + System.out.println(itr2.next()); + } } } \ No newline at end of file