From 176ee5af379ac2c97a87787cc3dae9ae40ebd13f Mon Sep 17 00:00:00 2001 From: Ming Li Date: Wed, 31 Jan 2024 02:43:12 -0800 Subject: [PATCH 1/4] Started working on ArrayIntList --- src/ArrayIntList.java | 208 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 208 insertions(+) create mode 100644 src/ArrayIntList.java diff --git a/src/ArrayIntList.java b/src/ArrayIntList.java new file mode 100644 index 0000000..90cd3d3 --- /dev/null +++ b/src/ArrayIntList.java @@ -0,0 +1,208 @@ +import java.util.Iterator; + +public class ArrayIntList implements IntList { + // Fields + private int size; + private int[] buffer; + + // Default + public ArrayIntList() { + // initialize my fields + size = 0; + buffer = new int[10]; + } + + /** + * Prepends (inserts) the specified value at the front of the list (at index 0). + * Shifts the value currently at the front of the list (if any) and any + * subsequent values to the right. + * + * @param value value to be inserted + */ + @Override + public void addFront(int value) { + for (int i = size; i > 0; i--) { + buffer[i] = buffer[i - 1]; + } + + buffer[0] = value; + size++; + } + + /** + * Appends (inserts) the specified value at the back of the list (at index size()-1). + * + * @param value value to be inserted + */ + @Override + public void addBack(int value) { + 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) { + size++; + + for (int i = size; i >= index; i--) { + buffer[i] = buffer[i - 1]; + } + + buffer[index] = 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() { + buffer[0] = 0; + + for (int i = 0; i < size; i++) { + buffer[i] = buffer[i + 1]; + } + + buffer[size] = 0; // Changes the back to 0 since we would move everything to the left + size--; + } + + /** + * Removes the value located at the back of the list + * (at index size()-1), if it is present. + */ + @Override + public void removeBack() { + buffer[size] = 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) { + int removedInt = buffer[index]; + + for (int i = index; i <= size; i++) { + buffer[i] = buffer[i + 1]; + } + + size--; + return removedInt; + } + + /** + * Returns the value at the specified position in the list. + * + * @param index index of the value to return + * @return the value at the specified position in this list + * @throws IndexOutOfBoundsException if the index is out of range + */ + @Override + public int get(int index) { + return buffer[index]; + } + + /** + * Returns true if this list contains the specified value. + * + * @param value value whose presence in this list is to be searched for + * @return true if this list contains the specified value + */ + @Override + public boolean contains(int value) { + boolean doseItContains = false; + + for (int i = 0; i <= size; i++) { + if (buffer[i] == value) { + doseItContains = true; + } + } + + return doseItContains; + } + + /** + * Returns the index of the first occurrence of the specified value + * in this list, or -1 if this list does not contain the value. + * + * @param value value to search for + * @return the index of the first occurrence of the specified value in this list + * or -1 if this list does not contain the value + */ + @Override + public int indexOf(int value) { + // if the value which is the index is greater than the size we would know that we don't have that index + if (value > size || value < 0) { + return -1; + } + else { + return buffer[value]; + } + } + + /** + * Returns true if this list contains no values. + * + * @return true if this list contains no values + */ + @Override + public boolean isEmpty() { + if (size == 0) { + return true; + } + else { + return false; + } + } + + /** + * Returns the number of values in this list. + * + * @return the number of values in this list + */ + @Override + public int size() { + return size; + } + + /** + * Removes all the values from this list. + * The list will be empty after this call returns. + */ + @Override + public void clear() { + for (int i = 0; i <= size; i++) { + buffer[size] = 0; + } + + size = 0; + } + + /** + * Returns an iterator over elements of type {@code T}. + * + * @return an Iterator. + */ + @Override + public Iterator iterator() { + return null; + } +} From 7d5cfe195a6200daccdf9e518902aad5e4ae9a21 Mon Sep 17 00:00:00 2001 From: Ming Li Date: Wed, 31 Jan 2024 02:45:16 -0800 Subject: [PATCH 2/4] Started working on ArrayIntList --- src/ArrayIntList.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ArrayIntList.java b/src/ArrayIntList.java index 90cd3d3..f4d8be5 100644 --- a/src/ArrayIntList.java +++ b/src/ArrayIntList.java @@ -7,7 +7,7 @@ public class ArrayIntList implements IntList { // Default public ArrayIntList() { - // initialize my fields + // initialize my fieldss size = 0; buffer = new int[10]; } From 17b3491ef989c7a24260527e2cd337d920ab8b6b Mon Sep 17 00:00:00 2001 From: Ming Li Date: Wed, 31 Jan 2024 02:46:15 -0800 Subject: [PATCH 3/4] Started working on LinkedIntList and DoublyLinkendIntList --- src/DoublyLinkendIntList.java | 202 ++++++++++++++++++++++++++++++++++ src/LinkedIntList.java | 194 ++++++++++++++++++++++++++++++++ src/Main.java | 17 ++- 3 files changed, 408 insertions(+), 5 deletions(-) create mode 100644 src/DoublyLinkendIntList.java create mode 100644 src/LinkedIntList.java diff --git a/src/DoublyLinkendIntList.java b/src/DoublyLinkendIntList.java new file mode 100644 index 0000000..e54120c --- /dev/null +++ b/src/DoublyLinkendIntList.java @@ -0,0 +1,202 @@ +import java.util.Iterator; + +public class DoublyLinkendIntList implements IntList { + + // private fields + private class Node { + int data; + Node next; // address of the nod "after" this one in line + Node prev; // address of the nod "before" this one in line + + // Option but helps with thinking :D + public Node() { + next = null; + prev = null; + } + } + + private Node pre; + private Node post; + private int size; + + // constructor + public DoublyLinkendIntList() { + // an empty list has 2 sentinel (dummy) nodes that serve as bookends + pre = new Node(); + post = new Node(); + pre.next = post; + post.prev = pre; + size = 0; + } + /** + * Prepends (inserts) the specified value at the front of the list (at index 0). + * Shifts the value currently at the front of the list (if any) and any + * subsequent values to the right. + * + * @param value value to be inserted + */ + @Override + public void addFront(int value) { + + } + + /** + * 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 lastRealNode = post.prev; + + // set up my new node and fill it out (data, pre, next) + Node theNewOne = new Node(); + theNewOne.data = value; + theNewOne.next = post; + theNewOne.prev = lastRealNode; + + // go the end of the list's sentinel, and update it prev + post.prev = theNewOne; + // go to the node before the new one, and update it's next + lastRealNode.next = theNewOne; + + 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 a tem variable for convenience + Node theOneToRemove = post.prev; + + theOneToRemove.prev.next = post; + post.prev = theOneToRemove.prev; + + // optional to clean up + theOneToRemove.next = null; + theOneToRemove.prev = null; + theOneToRemove.data = 0; + + size--; + } + + + } + + /** + * Removes the value at the specified position in this list. + * Shifts any subsequent values to the left. Returns the value + * that was removed from the list. + * + * @param index the index of the value to be removed + * @return the value previously at the specified position + * @throws IndexOutOfBoundsException if the index is out of range + */ + @Override + public int remove(int index) { + 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; + } + + /** + * 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 0; + } + + /** + * 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; + } +} diff --git a/src/LinkedIntList.java b/src/LinkedIntList.java new file mode 100644 index 0000000..6d6445e --- /dev/null +++ b/src/LinkedIntList.java @@ -0,0 +1,194 @@ +import java.util.Iterator; + +public class LinkedIntList implements IntList { + + // define what a node is + private class Node { + int data; + Node next; + } + + // set up the head field + private Node head; + + // set up the size field + private int size; + + // adda constructor to initialize the fields + public LinkedIntList() { + head = null; + size = 0; + } + + /** + * Prepends (inserts) the specified value at the front of the list (at index 0). + * Shifts the value currently at the front of the list (if any) and any + * subsequent values to the right. + * + * @param value value to be inserted + */ + @Override + public void addFront(int value) { + // set up a new node + Node theNewOne = new Node(); + + if (head == null) { + // the list is currently empty + head = theNewOne; + + size++; + } + else { + // the list currently has some nodes in it + theNewOne.next = head; + head = theNewOne; + + 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) { + // set up a new node + Node theNewOne = new Node(); + + if (head == null) { + // the list is currently empty + head = theNewOne; + + size++; + } + else { + head.next = theNewOne; + + 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() { + + } + + /** + * 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; + } + + /** + * 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 0; + } + + /** + * 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; + } +} diff --git a/src/Main.java b/src/Main.java index 930198c..51ecdfe 100644 --- a/src/Main.java +++ b/src/Main.java @@ -6,10 +6,17 @@ public static void main(String[] args) { // to see how IntelliJ IDEA suggests fixing it. System.out.printf("Hello and welcome!"); - for (int i = 1; i <= 5; i++) { - //TIP Press to start debugging your code. We have set one breakpoint - // for you, but you can always add more by pressing . - System.out.println("i = " + i); - } + // 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); + // } + + // IntList firstList = new IntList(); // Not allowed + IntList firstList; // This is allowed + + ArrayIntList secondList = new ArrayIntList(); + + IntList thirdList = new ArrayIntList(); } } \ No newline at end of file From 7105e6548f897e64bbcb714e41e501bcfe51997e Mon Sep 17 00:00:00 2001 From: Ming Li Date: Wed, 31 Jan 2024 03:31:05 -0800 Subject: [PATCH 4/4] Completed LinkedIntList --- src/LinkedIntList.java | 112 +++++++++++++++++++++++++++++++++++------ 1 file changed, 96 insertions(+), 16 deletions(-) diff --git a/src/LinkedIntList.java b/src/LinkedIntList.java index 6d6445e..4f8cc44 100644 --- a/src/LinkedIntList.java +++ b/src/LinkedIntList.java @@ -54,20 +54,16 @@ public void addFront(int value) { */ @Override public void addBack(int value) { - // set up a new node Node theNewOne = new Node(); + Node currentNode = head; - if (head == null) { - // the list is currently empty - head = theNewOne; - - size++; + // For each size, we will move the currentNode next until we reach the size so that we can set the currenNode.next to theNewOne which means that we added a new node in the back + for (int i = 0; i < size; i++) { + currentNode = currentNode.next; } - else { - head.next = theNewOne; - size++; - } + currentNode.next = theNewOne; + size++; } /** @@ -81,7 +77,31 @@ public void addBack(int value) { */ @Override public void add(int index, int value) { + Node currentNode = head; + int lastData; + + // If the index is less than or equal to size since it won't makes sense to work if it's bigger than the size + if (index <= size) { + for (int i = 0; i < size - index; i++) { + currentNode = currentNode.next; + } + // First time + lastData = currentNode.next.data; // Grabs the next data so it won't be lost + currentNode.next = currentNode; // The next node is equal to the current node + currentNode.data = value; // Replaces the current data to the value since we are adding the index here + currentNode = currentNode.next; // Then we move to the next node + + // The rest of the time + for (int i = size + 1; i > index; i--) { + currentNode.data = lastData; + lastData = currentNode.next.data; // Grabs the next data so it won't be lost + currentNode.next = currentNode; // The next node is equal to the current node + currentNode = currentNode.next; // Then we move to the next node + } + + size++; + } } /** @@ -91,7 +111,14 @@ public void add(int index, int value) { */ @Override public void removeFront() { + Node currentNode = head; + for (int i = 0; i < size - 1; i++) { + currentNode.data = currentNode.next.data; + currentNode = currentNode.next; + } + + size--; } /** @@ -100,7 +127,14 @@ public void removeFront() { */ @Override public void removeBack() { + Node currentNode = head; + + for (int i = 0; i < size; i++) { + currentNode = currentNode.next; + } + currentNode.data = 0; + size--; } /** @@ -114,7 +148,13 @@ public void removeBack() { */ @Override public int remove(int index) { - return 0; + Node currentNode = head; + + for (int i = 0; i < index; i++) { + currentNode = currentNode.next; + } + + return currentNode.data; } /** @@ -126,7 +166,13 @@ public int remove(int index) { */ @Override public int get(int index) { - return 0; + Node currentNode = head; + + for (int i = 0; i < index; i++) { + currentNode = currentNode.next; + } + + return currentNode.data; } /** @@ -137,7 +183,18 @@ public int get(int index) { */ @Override public boolean contains(int value) { - return false; + Node currentNode = head; + boolean containValue = false; + + for (int i = 0; i < size; i++) { + if (currentNode.data == value) { + containValue = true; + } + + currentNode = currentNode.next; + } + + return containValue; } /** @@ -150,7 +207,18 @@ public boolean contains(int value) { */ @Override public int indexOf(int value) { - return 0; + Node currentNode = head; + int indexOfValue = -1; + + for (int i = 0; i < size; i++) { + if (currentNode.data == value) { + indexOfValue = i; + } + + currentNode = currentNode.next; + } + + return indexOfValue; } /** @@ -160,7 +228,12 @@ public int indexOf(int value) { */ @Override public boolean isEmpty() { - return false; + if (size > 0) { + return false; + } + else { + return true; + } } /** @@ -170,7 +243,7 @@ public boolean isEmpty() { */ @Override public int size() { - return 0; + return size; } /** @@ -179,7 +252,14 @@ public int size() { */ @Override public void clear() { + Node currentNode = head; + + for (int i = 0; i < size; i++) { + currentNode.data = 0; + currentNode = currentNode.next; + } + size = 0; } /**