From aff259e83b5e79e4b56c77e04400599592bf180d Mon Sep 17 00:00:00 2001 From: Leah Konma Date: Thu, 25 Jan 2024 19:38:15 -0800 Subject: [PATCH 01/13] completed all methods --- src/ArrayIntList.java | 306 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 306 insertions(+) create mode 100644 src/ArrayIntList.java diff --git a/src/ArrayIntList.java b/src/ArrayIntList.java new file mode 100644 index 0000000..5d68925 --- /dev/null +++ b/src/ArrayIntList.java @@ -0,0 +1,306 @@ +import java.util.Iterator; +import java.util.NoSuchElementException; + +public class ArrayIntList implements IntList{ + //fields + private int size; + private int[] buffer; + + public ArrayIntList(){ + //initialize my fields + size = 0; + buffer = new int[10]; + } + + /** + * 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(size == buffer.length){ + resize(size * 2); + } + + //5,4,3,2,1 then save in [0] + for (int i = size; i >= 1 ; i--) { //count 5, 4, 3, 2, 1 + buffer[i] = buffer[i - 1]; //copy from 4, 3, 2, 1, 0 + } + + //put the value at the front of the array position [0] + buffer[0] = value; + size++; + } + + /** + * 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 buffer is full, create a larger buffer + if(size == buffer.length){ + resize(size * 2); + } + + 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 (index < 0 || index > size) { + throw new IndexOutOfBoundsException("Invalid Index"); + } + + if(size == buffer.length){ + resize(size * 2); + } + + //shift elements to the right + for (int i = size; i > index; i--) { + buffer[i] = buffer[i - 1]; + } + + //add the value at the specified index + buffer[index] = value; + size++; + + } + + /** + * Removes the value located at the front of the list + * (at index 0), if it is present. + * Shifts any subsequent values to the left. + */ + @Override + public void removeFront() { + if(!isEmpty()){ + for (int i = 0; i <= size - 2; i++) { + buffer[i] = buffer[i + 1]; + } + + //clear the last value to avoid duplicates + buffer[size - 1] = 0; + + size--; + } + + } + + /** + * Removes the value located at the back of the list + * (at index size()-1), if it is present. + */ + @Override + public void removeBack() { + if(!isEmpty()){ + buffer[size - 1] = 0; + size--; + } + } + + /** + * Removes the value at the specified position in this list. + * Shifts any subsequent values to the left. Returns the value + * that was removed from the list. + * + * @param index the index of the value to be removed + * @return the value previously at the specified position + * @throws IndexOutOfBoundsException if the index is out of range + */ + @Override + public int remove(int index) { + //check index to see if it is valid + if(index >= size || index < 0){ + throw new IndexOutOfBoundsException("Invalid index"); + } + + //save a copy of the value to removed, so we can return it later + int valueToRemove = buffer[index]; + + //shift values to the left + for (int i = index; i < size - 1; i++) { + buffer[i] = buffer[i+1]; + } + + //empty the last/redundant index + buffer[size - 1] = 0; + + //decrement size + size--; + + return valueToRemove; + } + + /** + * 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) { + if (index < 0 || index > size){ + throw new IndexOutOfBoundsException("Invalid Index"); + } + System.out.println(buffer[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 < buffer.length; 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) { + for (int i = 0; i <= size; i++) { + if (value == buffer[i]){ + return i; + }else{ + i++; + } + } + return -1; + } + + /** + * @return true if this list contains no values + */ + @Override + public boolean isEmpty() { + if (size == 0){ + return true; + } + return false; + } + + /** + * @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() { +// //go through each index of the array and set it to 0 +// for (int i = 0; i < size; i++) { +// buffer[i] = 0; +// i++; +// } + + //create a new array and overwrite the old + buffer = new int[10]; + size = 0; + } + + private void resize(int newSize){ + //create new space, separate form old space (buffer) + int[] newBuffer = new int[newSize]; + + //copy everything over from buffer into newBuffer + for (int i = 0; i < buffer.length; i++) { + newBuffer[i] = buffer[i]; + } + + //set the new space into buffer + buffer = newBuffer; + + //the old buffer space is no longer "pointed to" and will + //eventually be cleaned up by the garbage collector + } + + /** + * Returns an iterator over elements of type {@code T}. + * + * @return an Iterator. + */ + @Override + public Iterator iterator() { + return new IntListIterator(); + } + + + //create a private helper Iterator class + private class IntListIterator implements Iterator{ + //private fields + private int i; + + //constructor + private IntListIterator(){ + i = 0; + } + + /** + * Returns {@code true} if the iteration has more elements. + * (In other words, returns {@code true} if {@link #next} would + * return an element rather than throwing an exception.) + * + * @return {@code true} if the iteration has more elements + */ + @Override + public boolean hasNext() { + return i < size; + } + + /** + * Returns the next element in the iteration. + * + * @return the next element in the iteration + * @throws NoSuchElementException if the iteration has no more elements + */ + @Override + public Integer next() { + if(i >= size){ + throw new NoSuchElementException("i is now out of bounds"); + } + int currentValue = buffer[i]; + i++; + return currentValue; + } + } + + //iterators are what enables main/ client to use a for-each loop + //on my IntList +} From 5bda24bcc385252fbb7151430e3212a7ba7a3d1c Mon Sep 17 00:00:00 2001 From: Leah Konma Date: Thu, 25 Jan 2024 19:39:09 -0800 Subject: [PATCH 02/13] completed all methods --- src/Main.java | 106 +++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 97 insertions(+), 9 deletions(-) diff --git a/src/Main.java b/src/Main.java index 930198c..9067d75 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,15 +1,103 @@ -//TIP To Run code, press or -// click the icon in the gutter. +import java.util.Iterator; + + 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!"); + IntList firstList; + + IntList thirdList = new ArrayIntList(); + thirdList.addFront(15); + thirdList.addFront(12); + thirdList.addFront(8); + + //where an iterator gets used: + for (int value: thirdList) { + System.out.println(value); + } + + //alternate way to use an iterator + Iterator itr = thirdList.iterator(); + while (itr.hasNext()){ + int value = itr.next(); + System.out.println(value); + } + + arrayIntListTest(); + + + } + + public static void arrayIntListTest(){ + ArrayIntList arrayTest = new ArrayIntList(); + System.out.println("Testing ArrayIntList"); + + System.out.println("\naddFront()"); + arrayTest.addFront(1); + arrayTest.addFront(2); + arrayTest.addFront(3); + arrayTest.addFront(4); + arrayTest.addFront(5); + for (int value : arrayTest){ + System.out.print(value + ", "); + //should be: 5,4,3,2,1 + } + + System.out.println("\naddBack()"); + arrayTest.addBack(20); + for (int value : arrayTest){ + System.out.print(value + ", "); + //should be: 5,4,3,2,1,20 + } + + System.out.println("\nadd()"); + arrayTest.add(1,13); + for (int value : arrayTest){ + System.out.print(value + ", "); + //should be: 5,13,4,3,2,1,20 + } + + System.out.println("\nremoveFront()"); + arrayTest.removeFront(); + for (int value : arrayTest){ + System.out.print(value + ", "); + //should be: 13,4,2,1,20 + } + + System.out.println("\nremoveBack()"); + arrayTest.removeBack(); + for (int value : arrayTest){ + System.out.print(value + ", "); + //should be: 13,4,3,2,1 + } - 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); + System.out.println("\nremove()"); + arrayTest.remove(1); + for (int value : arrayTest){ + System.out.print(value + ", "); + //should be: 13,3,2,1 } + + System.out.println("\nget()"); + arrayTest.get(1); + //should be: 3 + + System.out.println("contains()"); + System.out.println(arrayTest.contains(1)); //true + + System.out.println("indexOf()"); + System.out.println(arrayTest.indexOf(2)); //2 + + System.out.println("isEmpty()"); + System.out.println(arrayTest.isEmpty()); //false + + System.out.println("size()"); + System.out.println(arrayTest.size()); //4 + + System.out.println("clear()"); + arrayTest.clear(); + for (int value : arrayTest){ + System.out.print(value + ", "); //empty + } + } } \ No newline at end of file From 947826d2c7a3b10fa531b05e020297f2ac1fee4f Mon Sep 17 00:00:00 2001 From: Leah Konma Date: Thu, 25 Jan 2024 19:41:39 -0800 Subject: [PATCH 03/13] tested methods for ArrayIntList --- src/Main.java | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/Main.java b/src/Main.java index 9067d75..8bb2bb1 100644 --- a/src/Main.java +++ b/src/Main.java @@ -23,8 +23,6 @@ public static void main(String[] args) { } arrayIntListTest(); - - } public static void arrayIntListTest(){ @@ -73,13 +71,11 @@ public static void arrayIntListTest(){ System.out.println("\nremove()"); arrayTest.remove(1); for (int value : arrayTest){ - System.out.print(value + ", "); - //should be: 13,3,2,1 + System.out.print(value + ", "); //should be: 13,3,2,1 } System.out.println("\nget()"); - arrayTest.get(1); - //should be: 3 + arrayTest.get(1); //should be: 3 System.out.println("contains()"); System.out.println(arrayTest.contains(1)); //true From 60cac565b56f63e4f8fee83d41adcdab32ebcf3a Mon Sep 17 00:00:00 2001 From: Leah Konma Date: Thu, 25 Jan 2024 19:43:57 -0800 Subject: [PATCH 04/13] tested methods for ArrayIntList --- src/Main.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Main.java b/src/Main.java index 8bb2bb1..72ac734 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,6 +1,5 @@ import java.util.Iterator; - public class Main { public static void main(String[] args) { IntList firstList; From 2179100ef2a61f9d849150e85c75a9b24e716872 Mon Sep 17 00:00:00 2001 From: Leah Konma Date: Thu, 25 Jan 2024 20:51:31 -0800 Subject: [PATCH 05/13] finished some easy methods --- src/LinkedIntList.java | 253 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 253 insertions(+) create mode 100644 src/LinkedIntList.java diff --git a/src/LinkedIntList.java b/src/LinkedIntList.java new file mode 100644 index 0000000..3ad57ca --- /dev/null +++ b/src/LinkedIntList.java @@ -0,0 +1,253 @@ +import java.util.Iterator; +import java.util.NoSuchElementException; + +public class LinkedIntList implements IntList{ + + // define what a node is + private class Node { + int data; + Node next; + } + + // set up the head + private Node head; + + //set up the size field + private int size; + + // add a constructor to initialize the fields + LinkedIntList(){ + head = null; + size = 0; + } + + /** + * Prints out the values of each node in the list + */ + public void printList() { + Node current = head; + while (current != null) { + System.out.print(current.data + ", "); + current = current.next; + } + } + + /** + * Prepends (inserts) the specified value at the front of the list (at index 0). + * Shifts the value currently at the front of the list (if any) and any + * subsequent values to the right. + * + * @param value value to be inserted + */ + @Override + public void addFront(int value) { + Node newOne = new Node(); + newOne.data = value; + if(head == null) { + // the list is currently empty + newOne.next = null; + head = newOne; + size++; + return; + } + // the list currently has some nodes in it + newOne.next = head; + head = newOne; + size++; + } + + /** + * Appends (inserts) the specified value at the back of the list (at index size()-1). + * + * @param value value to be inserted + */ + @Override + public void addBack(int value) { + Node newNode = new Node(); + newNode.data = value; + + Node current = head; + + //go to end of list + while (current.next != null){ + current = current.next; + } + + //add it to the back + current.next = newNode; + + } + + /** + * 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 < 0 || index > size){ + throw new IndexOutOfBoundsException("Invalid Index"); + } + + Node current = head; + + Node newNode = new Node(); + newNode.data = 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() { + Node current = head; + + } + + /** + * Removes the value located at the back of the list + * (at index size()-1), if it is present. + */ + @Override + public 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 + */ + @Override + public int remove(int index) { + return 0; + } + + /** + * 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 0; + } + + /** + * 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) { + 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) { + return 0; + } + + /** + * @return true if this list contains no values + */ + @Override + public boolean isEmpty() { + return false; + } + + /** + * @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() { + + } + + /** + * Returns an iterator over elements of type {@code T}. + * + * @return an Iterator. + */ + @Override + public Iterator iterator() { + SinglyLinkedIterator theIterator = new SinglyLinkedIterator(); + return theIterator; + } + + // helper class/type that defines how the iterator works + private class SinglyLinkedIterator implements Iterator { + private Node current; + + public SinglyLinkedIterator() { + current = head; + } + + + /** + * Returns {@code true} if the iteration has more elements. + * (In other words, returns {@code true} if {@link #next} would + * return an element rather than throwing an exception.) + * + * @return {@code true} if the iteration has more elements + */ + @Override + public boolean hasNext() { + if(current != null) + { + return true; + } + return false; + + //return current != null; + } + + /** + * Returns the next element in the iteration. + * + * @return the next element in the iteration + * @throws NoSuchElementException if the iteration has no more elements + */ + @Override + public Integer next() { + if(current == null){ + throw new NoSuchElementException("There is no next one to go to"); + } + int item = current.data; + current = current.next; + return item; + } + } +} \ No newline at end of file From 9a04e08ec5a46115710009b2d69cff6a2d0c1191 Mon Sep 17 00:00:00 2001 From: Leah Konma Date: Thu, 25 Jan 2024 20:52:02 -0800 Subject: [PATCH 06/13] created a test linked list --- src/Main.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Main.java b/src/Main.java index 72ac734..25eb207 100644 --- a/src/Main.java +++ b/src/Main.java @@ -21,7 +21,8 @@ public static void main(String[] args) { System.out.println(value); } - arrayIntListTest(); +// arrayIntListTest(); + singlyLinkedIntListTest(); } public static void arrayIntListTest(){ @@ -95,4 +96,18 @@ public static void arrayIntListTest(){ } } + + public static void singlyLinkedIntListTest(){ + LinkedIntList linkedListTest = new LinkedIntList(); + + linkedListTest.addFront(1); + linkedListTest.addFront(2); + linkedListTest.addFront(3); + linkedListTest.addFront(4); + System.out.println("Base list: "); + linkedListTest.printList(); + + System.out.println("\naddBacK()"); + linkedListTest.addBack(13); + linkedListTest.printList();} } \ No newline at end of file From 022cefefd2b79de0db6c4ea07824fdc23991115e Mon Sep 17 00:00:00 2001 From: Leah Konma Date: Fri, 26 Jan 2024 20:48:46 -0800 Subject: [PATCH 07/13] finished a few more methods --- src/LinkedIntList.java | 58 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 53 insertions(+), 5 deletions(-) diff --git a/src/LinkedIntList.java b/src/LinkedIntList.java index 3ad57ca..9da94df 100644 --- a/src/LinkedIntList.java +++ b/src/LinkedIntList.java @@ -63,6 +63,7 @@ public void addFront(int value) { */ @Override public void addBack(int value) { + //create a new a node with the specified value Node newNode = new Node(); newNode.data = value; @@ -76,6 +77,8 @@ public void addBack(int value) { //add it to the back current.next = newNode; + size++; + } /** @@ -93,23 +96,36 @@ public void add(int index, int value) { throw new IndexOutOfBoundsException("Invalid Index"); } - Node current = head; - + //create a new node with the specified data Node newNode = new Node(); newNode.data = value; + //temporary node to traverse list + Node current = head; + int currIndex = 0; + + while (currIndex != index - 1){ + current = current.next; + currIndex++; + } + newNode.next = current.next; + current.next = newNode; + + 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() { - Node current = head; + if (size > 0){ + head = head.next; + } + size--; } /** @@ -118,7 +134,17 @@ public void removeFront() { */ @Override public void removeBack() { + Node current = head; + int currIndex = 1; + while (currIndex != size()- 1){ + current = current.next; + currIndex++; + } + + current.next = null; + + size--; } /** @@ -132,7 +158,29 @@ public void removeBack() { */ @Override public int remove(int index) { - return 0; + if (index < 0 || index > size){ + throw new IndexOutOfBoundsException("Invalid index"); + } + + Node current = head; + Node prev = null; + int currIndex = 0; + + while(currIndex != index){ + prev = current; + current = current.next; + currIndex++; + } + + //remove node + if (prev != null){ + prev.next = current.next; + }else{ //if removal of head node + head = current.next; + } + + size--; + return current.data; } /** From e016eaac5fb7d371ec0f2557721ca99fedd90864 Mon Sep 17 00:00:00 2001 From: Leah Konma Date: Fri, 26 Jan 2024 20:48:59 -0800 Subject: [PATCH 08/13] added more tests --- src/Main.java | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/Main.java b/src/Main.java index 25eb207..6b4d0e1 100644 --- a/src/Main.java +++ b/src/Main.java @@ -104,10 +104,28 @@ public static void singlyLinkedIntListTest(){ linkedListTest.addFront(2); linkedListTest.addFront(3); linkedListTest.addFront(4); - System.out.println("Base list: "); + System.out.println("Base linked list: "); linkedListTest.printList(); System.out.println("\naddBacK()"); linkedListTest.addBack(13); - linkedListTest.printList();} + linkedListTest.printList(); //4,3,2,1,13 + + System.out.println("\nadd()"); + linkedListTest.add(3,10); + linkedListTest.printList(); //4,3,2,10,1,13 + + System.out.println("\nremoveFront()"); + linkedListTest.removeFront(); + linkedListTest.printList(); // 3,2,10,1,13 + + System.out.println("\nremoveBack()"); + linkedListTest.removeBack(); + linkedListTest.printList(); // 3,2,10,1 + + System.out.println("\nremove()"); + linkedListTest.remove(1); + linkedListTest.printList(); // 3,10,1 + + } } \ No newline at end of file From 9bb1ef9ac211e58d24ca5bcc0db9e314d4ff2aa1 Mon Sep 17 00:00:00 2001 From: Leah Konma Date: Fri, 26 Jan 2024 22:43:53 -0800 Subject: [PATCH 09/13] added all tests for LinkedIntList and ArrayIntList --- src/Main.java | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/Main.java b/src/Main.java index 6b4d0e1..8a9ae43 100644 --- a/src/Main.java +++ b/src/Main.java @@ -21,7 +21,7 @@ public static void main(String[] args) { System.out.println(value); } -// arrayIntListTest(); + arrayIntListTest(); singlyLinkedIntListTest(); } @@ -127,5 +127,24 @@ public static void singlyLinkedIntListTest(){ linkedListTest.remove(1); linkedListTest.printList(); // 3,10,1 + System.out.println("\nget()"); + System.out.println(linkedListTest.get(1)); // 10 + + System.out.println("\ncontains()"); + System.out.println(linkedListTest.contains(10)); //true + + System.out.println("\nindexOf()"); + System.out.println(linkedListTest.indexOf(1)); //2 + + System.out.println("\nisEmpty()"); + System.out.println(linkedListTest.isEmpty()); //false + + System.out.println("\nsize()"); + System.out.println(linkedListTest.size()); //3 + + System.out.println("\nclear"); + linkedListTest.clear(); + linkedListTest.printList(); + } } \ No newline at end of file From ea299507a27731748cf7f82ff50b92446438a39c Mon Sep 17 00:00:00 2001 From: Leah Konma Date: Fri, 26 Jan 2024 22:44:09 -0800 Subject: [PATCH 10/13] finished all methods --- src/LinkedIntList.java | 56 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 53 insertions(+), 3 deletions(-) diff --git a/src/LinkedIntList.java b/src/LinkedIntList.java index 9da94df..4eea1ef 100644 --- a/src/LinkedIntList.java +++ b/src/LinkedIntList.java @@ -192,7 +192,24 @@ public int remove(int index) { */ @Override public int get(int index) { - return 0; + if (index < 0 || index > size){ + throw new IndexOutOfBoundsException("Invalid index"); + } + + Node current = head; + int currIndex = 0; + + while(currIndex != index){ + current = current.next; + currIndex++; + + //don't go past end of list + if (current == null){ + throw new IndexOutOfBoundsException("Invalid index"); + } + } + + return current.data; } /** @@ -203,6 +220,17 @@ public int get(int index) { */ @Override public boolean contains(int value) { + + Node current = head; + + while(current != null){ + if(current.data == value){ + return true; + }else{ + current = current.next; + } + + } return false; } @@ -216,7 +244,20 @@ public boolean contains(int value) { */ @Override public int indexOf(int value) { - return 0; + Node current = new Node(); + current = head; + int currIndex = 0; + + while (current != null){ + if (current.data == value){ + return currIndex; + } + else{ + current = current.next; + currIndex++; + } + } + return -1; } /** @@ -224,6 +265,9 @@ public int indexOf(int value) { */ @Override public boolean isEmpty() { + if(head.next == null){ + return true; + } return false; } @@ -241,7 +285,13 @@ public int size() { */ @Override public void clear() { - + Node current = head; + while (current != null){ + Node next = current.next; + current = next; + } + head = null; + size = 0; } /** From f3cc18a4dcb87c05772f265a2c05965ecb150520 Mon Sep 17 00:00:00 2001 From: Leah Konma Date: Sun, 28 Jan 2024 01:46:38 -0800 Subject: [PATCH 11/13] added a print method and addFront method --- src/DoublyLinkedIntList.java | 230 +++++++++++++++++++++++++++++++++++ 1 file changed, 230 insertions(+) create mode 100644 src/DoublyLinkedIntList.java diff --git a/src/DoublyLinkedIntList.java b/src/DoublyLinkedIntList.java new file mode 100644 index 0000000..29ded88 --- /dev/null +++ b/src/DoublyLinkedIntList.java @@ -0,0 +1,230 @@ +import java.util.Iterator; + +public class DoublyLinkedIntList implements IntList{ + //private fields + private class Node{ + int data; + Node next; // address of the node "after" this one in line + Node prev; //address of the node "before" this one in line + + public Node(){ + next = null; + prev = null; + } + } + + private Node pre; + private Node post; + private int size; + + //constructor + public DoublyLinkedIntList(){ + //an empty list has 2 sentinel (dummy) nodes that serve as bookends + pre = new Node(); + post = new Node(); + pre.next = post; //sentinel node before first data node + post.prev = pre; //sentinel node after last data node + size = 0; + } + + public void printList(){ + //start at the first node, after first sentinel + Node current = pre.next; + + //go through the list until the last sentinel node + while (current != post){ + System.out.print(current.data + ", "); + current = current.next; + } + } + + /** + * Prepends (inserts) the specified value at the front of the list (at index 0). + * Shifts the value currently at the front of the list (if any) and any + * subsequent values to the right. + * + * @param value value to be inserted + */ + @Override + public void addFront(int value) { + Node newNode = new Node(); + newNode.data = value; + + //adjust references to point to new node + newNode.prev = pre; + newNode.next = pre.next; + + //update first and end sentinels + pre.next.prev = newNode; + pre.next = newNode; + + size++; + } + + /** + * Appends (inserts) the specified value at the back of the list (at index size()-1). + * + * @param value value to be inserted + */ + @Override + public void addBack(int value) { + Node lastNode = post.prev; + + //set up my new node and fill it out (data, prev, next) + Node newNode = new Node(); + newNode.data = value; + newNode.next = post; + newNode.prev = lastNode; + + //go to the end of the list's sentinel, and update it's prev + post.prev = newNode; + + //go to the node before the new on, and update it's next + lastNode.next = newNode; + size++; + } + + /** + * Inserts the specified value at the specified position in this list. + * Shifts the value currently at that position (if any) and any subsequent + * values to the right. + * + * @param index index at which the specified value is to be inserted + * @param value value to be inserted + * @throws IndexOutOfBoundsException if the index is out of range + */ + @Override + public void add(int index, int value) { + + } + + /** + * 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() { + + } + + /** + * 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){ + //set up temporary variable + Node removedNode = post.prev; + + //go to the node before the last, and point to post node + removedNode.prev.next = post; + + //point the post node to the node before the removedNode + post.prev = removedNode.prev; + + //optional to clean up + removedNode.next = null; + removedNode.prev = null; + + 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) { + return 0; + } + + /** + * 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 0; + } + + /** + * 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) { + //if list is empty + if(pre.next == post){ + return false; + } + + 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) { + return 0; + } + + /** + * Returns true if this list contains no values. + * + * @return true if this list contains no values + */ + @Override + public boolean isEmpty() { + 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() { + + } + + /** + * Returns an iterator over elements of type {@code T}. + * + * @return an Iterator. + */ + @Override + public Iterator iterator() { + return null; + } +} From a548028604e9a6faa0d5253ac56de1685dff80e6 Mon Sep 17 00:00:00 2001 From: Leah Konma Date: Sun, 28 Jan 2024 22:43:12 -0800 Subject: [PATCH 12/13] finished writing all tests within a method --- src/Main.java | 51 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/src/Main.java b/src/Main.java index 8a9ae43..cd9c4bb 100644 --- a/src/Main.java +++ b/src/Main.java @@ -21,8 +21,10 @@ public static void main(String[] args) { System.out.println(value); } - arrayIntListTest(); - singlyLinkedIntListTest(); +// arrayIntListTest(); +// singlyLinkedIntListTest(); + doublyLinkedIntListTest(); + } public static void arrayIntListTest(){ @@ -147,4 +149,49 @@ public static void singlyLinkedIntListTest(){ linkedListTest.printList(); } + + public static void doublyLinkedIntListTest(){ + DoublyLinkedIntList test = new DoublyLinkedIntList(); + test.addFront(1); + test.addFront(2); + test.addFront(3); + test.addFront(4); + test.addFront(5); + test.addFront(6); + test.addFront(7); + test.addBack(8); + System.out.println("Base doubly linked list"); //7, 6, 5, 4, 3, 2, 1, 8 + test.printList(); + + System.out.println("\nindexOf()"); + System.out.println(test.indexOf(6)); //1 + + System.out.println("\nget()"); + System.out.println(test.get(0)); //7 + + System.out.println("\ncontains()"); + System.out.println(test.contains(3)); //true + + System.out.println("\nremove()"); + test.remove(4); + test.printList(); //7, 6, 5, 4, 2, 1, 8 + + System.out.println("\nremoveFront"); + test.removeFront(); + test.printList(); //6, 5, 4, 2, 1, 8 + + System.out.println("\nremoveBack"); + test.removeBack(); + test.printList(); //6, 5, 4, 2, 1 + + System.out.println("\nsize()"); + System.out.println(test.size()); //5 + + System.out.println("\nclear()"); + test.clear(); + + System.out.println("\nisEmpty()"); + System.out.println(test.isEmpty()); //true + + } } \ No newline at end of file From 48a5bb4fa374e8b9fbbd9b3dd93a21ad58f269bb Mon Sep 17 00:00:00 2001 From: Leah Konma Date: Sun, 28 Jan 2024 22:43:29 -0800 Subject: [PATCH 13/13] finished all methods --- src/DoublyLinkedIntList.java | 106 +++++++++++++++++++++++++++++++---- 1 file changed, 96 insertions(+), 10 deletions(-) diff --git a/src/DoublyLinkedIntList.java b/src/DoublyLinkedIntList.java index 29ded88..edf6883 100644 --- a/src/DoublyLinkedIntList.java +++ b/src/DoublyLinkedIntList.java @@ -95,7 +95,28 @@ public void addBack(int value) { */ @Override public void add(int index, int value) { + if (index < 0 || index > size){ + throw new IndexOutOfBoundsException("Invalid index"); + } + + Node newNode = new Node(); + newNode.data = value; + + Node current = pre.next; + for (int i = 0; i < index; i++) { + current = current.next; + } + + //update references to include new node + newNode.next = current.prev; + newNode.prev = current; + + //update neighboring nodes' reference + current.prev.next = newNode; + current.prev = newNode; + + size++; } /** @@ -105,6 +126,15 @@ public void add(int index, int value) { */ @Override public void removeFront() { + if (size > 0){ + //the node to be removed + Node removedNode = pre.next; + + //update references + pre.next = removedNode.next; //set pre to the node after the removed one + removedNode.next.prev = pre; //new 'first' node connects back to sentinel pre + size--; + } } @@ -130,8 +160,6 @@ public void removeBack() { size--; } - - } /** @@ -145,7 +173,33 @@ public void removeBack() { */ @Override public int remove(int index) { - return 0; + if (index < 0 || index > size){ + throw new IndexOutOfBoundsException("Invalid index"); + } + + Node removedNode = pre; + + //removing first node + if (index == 0){ + removeFront(); + } + //removing last node + else if (index == size-1){ + removeBack(); + } + //removing middle node + else{ + Node current = pre.next; + for (int i = 0; i < index; i++) { + current = current.next; + } + removedNode = current; + current.prev.next = current.next; + current.next.prev = current.prev; + } + + size--; + return removedNode.data; } /** @@ -157,7 +211,18 @@ public int remove(int index) { */ @Override public int get(int index) { - return 0; + if (index < 0 || index > size){ + throw new IndexOutOfBoundsException("Invalid index"); + } + + Node current = pre.next; + + for (int i = 0; i < index; i++) { + current = current.next; + } + + //return the data at the index + return current.data; } /** @@ -172,6 +237,15 @@ public boolean contains(int value) { if(pre.next == post){ return false; } + //new node to traverse list + Node current = pre.next; + + while (current.next != post){ + if (current.data == value){ + return true; + } + current = current.next; + } return false; } @@ -186,22 +260,33 @@ public boolean contains(int value) { */ @Override public int indexOf(int value) { - return 0; + Node current = pre.next; + int index = 0; + + //go to the end of the list + while(current != post){ + if (current.data == value){ + return index; + }else{ + index++; + current = current.next; + } + } + return -1; } /** - * Returns true if this list contains no values. - * * @return true if this list contains no values */ @Override public boolean isEmpty() { + if (pre.next == post){ + return true; + } return false; } /** - * Returns the number of values in this list. - * * @return the number of values in this list */ @Override @@ -215,7 +300,8 @@ public int size() { */ @Override public void clear() { - + pre.next = post; + size = 0; } /**