From 3eb596ad42fd5ead2a4b57fb25f7cc20b69ad614 Mon Sep 17 00:00:00 2001 From: kat-coding <83139590+kat-coding@users.noreply.github.com> Date: Wed, 8 Feb 2023 14:44:02 -0800 Subject: [PATCH 1/2] finished resizing array and singly lined deque classes --- .idea/vcs.xml | 6 +++ src/Main.java | 32 ++++++++++++++++ src/ResizingArrayDeque.java | 54 +++++++++++++++++++++++++-- src/SinglyLinkedDeque.java | 73 +++++++++++++++++++++++++++++++++++-- 4 files changed, 159 insertions(+), 6 deletions(-) create mode 100644 .idea/vcs.xml diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/Main.java b/src/Main.java index c502941..f08ed2d 100644 --- a/src/Main.java +++ b/src/Main.java @@ -8,5 +8,37 @@ public static void main(String[] args) { Deque d2 = new SinglyLinkedDeque<>(); // some test code here + d1.addFirst("This "); + d1.addLast("is "); + d1.addFirst("resizing array!"); + + d2.addFirst("Hello"); + d2.addLast("World"); + d2.addFirst("I say to you..."); + + System.out.println(d1.size()); //3 + System.out.println(d2.size()); //3 + + System.out.println(d2.removeLast()); + System.out.println(d2.removeFirst()); + System.out.println(d2.removeLast()); + System.out.println(); + + System.out.println(d1.removeLast()); + System.out.println(d1.removeFirst()); + System.out.println(d1.removeLast()); + + System.out.println(d1.size()); //0 + System.out.println(d2.size()); //0 + + d1.addLast("Add Last"); + d2.addFirst("Linked Add Last"); + +// System.out.println(d1.size()); //0 + System.out.println(d2.size()); //0 + + System.out.println(d2.removeFirst()); +// System.out.println(d1.removeFirst()); + } } \ No newline at end of file diff --git a/src/ResizingArrayDeque.java b/src/ResizingArrayDeque.java index d1bca63..fc55111 100644 --- a/src/ResizingArrayDeque.java +++ b/src/ResizingArrayDeque.java @@ -36,6 +36,16 @@ public void addFirst(ItemType item) { // There is a private helper method checkSize() defined below to check/resize // that you can call as needed to check if the array is full and resize it. + checkSize(); + if(size == 0){ + data[0] = item; + }else { + for (int i = size; i > 0; i--) { + data[i] = data[i - 1]; + } + data[0] = item; + } + size++; } /** @@ -50,6 +60,13 @@ public void addLast(ItemType item) { // There is a private helper method checkSize() defined below to check/resize // that you can call as needed to check if the array is full and resize it. + checkSize(); + if(size == 0){ + data[0] = item; + }else{ + data[size] = item; + } + size++; } /** @@ -69,8 +86,24 @@ public ItemType removeFirst() { // 1. make a variable to save a copy of the item at the front // 2. remove the item at the front // 3. return the variable that has the saved copy of the item at the front - - return null; + if(size == 0){ + return null; + } + else if (size == 1) { + ItemType returnThis = data[0]; + data[0] = null; + size--; + return returnThis; + } + else{ + ItemType returnThis = data[0]; + for(int i = 0; i< size ; i++){ + data[i] = data[i+1]; + } + data[size] = null; + size--; + return returnThis; + } } /** @@ -90,8 +123,23 @@ public ItemType removeLast() { // 1. make a variable to save a copy of the item at the back // 2. remove the item at the back // 3. return the variable that has the saved copy of the item at the back + if(size == 0){ + return null; + } + else if(size == 1){ + ItemType returnThis = data[0]; + data[0] = null; + size--; + return returnThis; + + } + else{ + ItemType returnThis = data[size -1]; + data[size -1] = null; + size--; + return returnThis; + } - return null; } // helper method to check to see if the size has reached the capacity diff --git a/src/SinglyLinkedDeque.java b/src/SinglyLinkedDeque.java index 5440edf..d10edfe 100644 --- a/src/SinglyLinkedDeque.java +++ b/src/SinglyLinkedDeque.java @@ -34,8 +34,24 @@ public int size() { */ @Override public void addFirst(ItemType item) { + if(item == null){ + throw new NullPointerException(); + } // consider the case of adding to an empty list // consider the case of adding to a non-empty list + + if(head == null){ + Node addThis = new Node(); + addThis.data = item; + addThis.next = null; + head = addThis; + }else{ + Node insert = new Node(); + insert.data = item; + insert.next = head; + head = insert; + } + size++; } /** @@ -45,8 +61,27 @@ public void addFirst(ItemType item) { */ @Override public void addLast(ItemType item) { + if(item == null){ + throw new NullPointerException(); + } // consider the case of adding to an empty list // consider the case of adding to a non-empty list + if(head == null){ + Node addThis = new Node(); + addThis.data = item; + addThis.next = null; + head = addThis; + }else{ + Node currentNode = head; + while(currentNode.next != null){ + currentNode = currentNode.next; + } + Node insert = new Node(); + insert.data = item; + insert.next = null; + currentNode.next = insert; + } + size++; } /** @@ -67,7 +102,23 @@ public ItemType removeFirst() { // 2. remove the item at the front // 3. return the variable that has the saved copy of the item at the front - return null; + if(head == null){ + return null; + } + if(head.next == null){ + ItemType returnThis = head.data; + head = null; + size--; + return returnThis; + + }else{ + ItemType returnThis = head.data; + head = head.next; + size--; + return returnThis; + + } + } /** @@ -87,7 +138,23 @@ public ItemType removeLast() { // 1. make a variable to save a copy of the item at the back // 2. remove the item at the back // 3. return the variable that has the saved copy of the item at the back - - return null; + if (head == null) { + return null; + } + if (head.next == null) { + ItemType returnThis = head.data; + head = null; + size--; + return returnThis; + } else { + Node currentNode = head; + while (currentNode.next.next != null) { + currentNode = currentNode.next; + } + ItemType returnThis = currentNode.next.data; + currentNode.next = null; + size--; + return returnThis; + } } } From 8ecb8d00668cb0cdd8159c7fbc6641b6ede77316 Mon Sep 17 00:00:00 2001 From: kat-coding <83139590+kat-coding@users.noreply.github.com> Date: Wed, 8 Feb 2023 14:53:41 -0800 Subject: [PATCH 2/2] added comments --- src/Main.java | 3 ++- src/SinglyLinkedDeque.java | 25 ++++++++++++++++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/Main.java b/src/Main.java index f08ed2d..d203447 100644 --- a/src/Main.java +++ b/src/Main.java @@ -37,7 +37,8 @@ public static void main(String[] args) { // System.out.println(d1.size()); //0 System.out.println(d2.size()); //0 - System.out.println(d2.removeFirst()); + System.out.println(d1.removeFirst()); + System.out.println(d1.removeFirst()); // System.out.println(d1.removeFirst()); } diff --git a/src/SinglyLinkedDeque.java b/src/SinglyLinkedDeque.java index d10edfe..cc2c466 100644 --- a/src/SinglyLinkedDeque.java +++ b/src/SinglyLinkedDeque.java @@ -40,17 +40,21 @@ public void addFirst(ItemType item) { // consider the case of adding to an empty list // consider the case of adding to a non-empty list + //case for empty list if(head == null){ + //create new node and have head point to it Node addThis = new Node(); addThis.data = item; addThis.next = null; head = addThis; }else{ + //otherwise create new node, have it point to old head, then make it the head Node insert = new Node(); insert.data = item; insert.next = head; head = insert; } + //track size size++; } @@ -66,21 +70,27 @@ public void addLast(ItemType item) { } // consider the case of adding to an empty list // consider the case of adding to a non-empty list + + //case for empty list: if(head == null){ + //made a new node and have head point to it Node addThis = new Node(); addThis.data = item; addThis.next = null; head = addThis; }else{ + //otherwise get to last node Node currentNode = head; while(currentNode.next != null){ currentNode = currentNode.next; } + //then have it point to a new node rather than null Node insert = new Node(); insert.data = item; insert.next = null; currentNode.next = insert; } + //keep track of size size++; } @@ -102,16 +112,20 @@ public ItemType removeFirst() { // 2. remove the item at the front // 3. return the variable that has the saved copy of the item at the front + //check empty if(head == null){ return null; } - if(head.next == null){ + //check if one item in list + else if(head.next == null){ + //take head data, make head null, track size, then return data ItemType returnThis = head.data; head = null; size--; return returnThis; }else{ + //take head data, make next item head, track size, return data ItemType returnThis = head.data; head = head.next; size--; @@ -138,15 +152,20 @@ public ItemType removeLast() { // 1. make a variable to save a copy of the item at the back // 2. remove the item at the back // 3. return the variable that has the saved copy of the item at the back + + //if empty if (head == null) { return null; } - if (head.next == null) { + //if one item take head data, make head null, track size, then return data + else if (head.next == null) { ItemType returnThis = head.data; head = null; size--; return returnThis; - } else { + } + //else get to one item before end of list, grab end item's data to return and make second to last item next = null, return data + else { Node currentNode = head; while (currentNode.next.next != null) { currentNode = currentNode.next;