From f64850e63c38a0abb31bbbbb43fd09eeee9faed7 Mon Sep 17 00:00:00 2001 From: Rebecca Dang Date: Thu, 1 Jul 2021 15:39:39 -0700 Subject: [PATCH 1/4] Update and rename LinkedList.java - Rename to TestLinkedList.java - Add driver class to test methods - Update practice problem instructions to be more specific and include driver class - Add insert, clear methods - Update misc docstrings for clarity - Update toString() method to handle empty lists properly --- .../{LinkedList.java => TestLinkedList.java} | 111 ++++++++++++++++-- 1 file changed, 103 insertions(+), 8 deletions(-) rename src/com/codefortomorrow/advanced/chapter16/solutions/{LinkedList.java => TestLinkedList.java} (51%) diff --git a/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java b/src/com/codefortomorrow/advanced/chapter16/solutions/TestLinkedList.java similarity index 51% rename from src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java rename to src/com/codefortomorrow/advanced/chapter16/solutions/TestLinkedList.java index a73917e..3af705f 100644 --- a/src/com/codefortomorrow/advanced/chapter16/solutions/LinkedList.java +++ b/src/com/codefortomorrow/advanced/chapter16/solutions/TestLinkedList.java @@ -8,15 +8,64 @@ * You will need to create a LinkedListNode class, which represents each item/node in the linked list * Assume that the elements in the linked list are all of type int * Required functionality... - * - head(): get head of list - * - tail(): get tail of list - * - add(): add to tail of list - * - push(): push to head of list - * - pop(): remove head of list + * - head(): get head node of list + * - tail(): get tail node of list + * - add(): add element to the end of the list + * - insert(): insert an element at a given index position + * - push(): add element to the beginning of the list + * - pop(): remove head of list and return the node that was removed + * - clear(): remove all elements in the list * - toString(): return a String representation of the list + * + * Create a driver class called TestLinkedList. In the main method, create an instance + * of LinkedList and test all of its methods at least once. (Print the list as needed + * to prove the methods worked.) */ -public class LinkedList { +public class TestLinkedList { + public static void main(String[] args) { + // test default constructor + LinkedList list = new LinkedList(); + System.out.println("Empty linked list: " + list); + + // test 1 arg constructor + list = new LinkedList(10); + System.out.println("Linked list with 1 element: " + list); + + // test add method + list.add(5); + list.add(7); + list.add(12); + list.add(2); + System.out.println("Test add(): " + list); + + // test insert method + list.insert(13, 0); + System.out.println("Test insert(): " + list); + list.insert(1, 6); + System.out.println("Test insert(): " + list); + list.insert(4, 3); + System.out.println("Test insert(): " + list); + + // test head and tail methods + System.out.println("Head: " + list.head().value()); + System.out.println("Tail: " + list.tail().value()); + + // test push + list.push(21); + System.out.println("Test push(): " + list); + + // test pop + System.out.println("Popped element: " + list.pop().value()); + System.out.println("Test pop(): " + list); + + // test clear + list.clear(); + System.out.println("Test clear(): " + list); + } +} + +class LinkedList { private LinkedListNode head; @@ -73,7 +122,42 @@ public void add(int value) { } /** - * push (add element to head of linkedlist) + * Inserts a value at a given position in the linked list + * @param value value to insert + * @param index where to insert the value + */ + public void insert(int value, int index) { + // Verify valid index number + if (index < 0) System.out.print("Invalid position"); + + // Create new node and keep track of current node + LinkedListNode new_node = new LinkedListNode(value, null); + LinkedListNode current = head; + + // If index is 0, set new node as head + if (index == 0) { + new_node.setNext(head); + head = new_node; + } else { + while (index-- >= 0) { + if (index == 0) { + // Add new node after current position + new_node.setNext(current.next()); + current.setNext(new_node); + break; + } + // Check if index is out of bounds (reaches end) + if (current.next() == null) { + System.out.println("Out of bounds"); + break; + } + current = current.next(); + } + } + } + + /** + * Adds element to head of the linked list */ public void push(int value) { LinkedListNode newHead = new LinkedListNode(value, head); @@ -81,7 +165,7 @@ public void push(int value) { } /** - * pop (remove and return head of linkedlist) + * Removes and returns the head of the linked list * @return the node that was removed */ public LinkedListNode pop() { @@ -90,12 +174,23 @@ public LinkedListNode pop() { return popped; } + /** + * Removes all elements in the list + */ + public void clear() { + head = null; + } + /** * Returns a String version of the LinkedList * @return a String version of the LinkedList */ @Override public String toString() { + if (head == null) { + return "[]"; + } + String list = "["; LinkedListNode current = head; if (current == null) return null; From 877ee90006e8034416faff0991e3d2783970fe0a Mon Sep 17 00:00:00 2001 From: Rebecca Dang Date: Thu, 1 Jul 2021 15:40:37 -0700 Subject: [PATCH 2/4] Delete SinglyLinkedList This is a duplicate version of LinkedList.java and is implemented and documented more poorly --- .../practice/SinglyLinkedListInsert.java | 30 ----- .../solutions/SinglyLinkedListInsert.java | 111 ------------------ 2 files changed, 141 deletions(-) delete mode 100644 src/com/codefortomorrow/advanced/chapter16/practice/SinglyLinkedListInsert.java delete mode 100644 src/com/codefortomorrow/advanced/chapter16/solutions/SinglyLinkedListInsert.java diff --git a/src/com/codefortomorrow/advanced/chapter16/practice/SinglyLinkedListInsert.java b/src/com/codefortomorrow/advanced/chapter16/practice/SinglyLinkedListInsert.java deleted file mode 100644 index 1b8ec1b..0000000 --- a/src/com/codefortomorrow/advanced/chapter16/practice/SinglyLinkedListInsert.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.codefortomorrow.advanced.chapter16.practice; -/* -Adding an insert method to a SinglyLinkedList -(details in SinglyLinkedList DIY section). - -Hint: It might be necessary to have a separate case for -inserting at position 0, because that’s when you want to -insert BEFORE the head. For all other positions (1, 2, etc.), -you can simply traverse down the list, keeping track of the -current node, and when you reach the position you want, -add the node AFTER the current node. - -Alternatively, you can always add BEFORE the current node -for all position cases (insert before the head to place the -new node at pos 0, insert before node 1 to place the new -node at pos 1, etc.), but then your exception case would -be when the user wants to insert a node at the very end of the list. -In this case, you’d have to write separate code to insert AFTER -the last node. - -Make sure you have a toString() method defined! - -The following code should output “{2, 3, 4, 7}”: -SinglyLinkedList lst = new SinglyLinkedList(); -lst.add(3); -lst.add(7); -lst.insert(4, 1); -lst.insert(2, 0); -System.out.println(lst); -*/ diff --git a/src/com/codefortomorrow/advanced/chapter16/solutions/SinglyLinkedListInsert.java b/src/com/codefortomorrow/advanced/chapter16/solutions/SinglyLinkedListInsert.java deleted file mode 100644 index 7fb84c1..0000000 --- a/src/com/codefortomorrow/advanced/chapter16/solutions/SinglyLinkedListInsert.java +++ /dev/null @@ -1,111 +0,0 @@ -package com.codefortomorrow.advanced.chapter16.solutions; - -import java.util.*; - -/* -Adding an insert method to a SinglyLinkedList -(details in SinglyLinkedList DIY section). - -Hint: It might be necessary to have a separate case for -inserting at position 0, because that’s when you want to -insert BEFORE the head. For all other positions (1, 2, etc.), -you can simply traverse down the list, keeping track of the -current node, and when you reach the position you want, -add the node AFTER the current node. - -Alternatively, you can always add BEFORE the current node -for all position cases (insert before the head to place the -new node at pos 0, insert before node 1 to place the new -node at pos 1, etc.), but then your exception case would -be when the user wants to insert a node at the very end of the list. -In this case, you’d have to write separate code to insert AFTER -the last node. - -Make sure you have a toString() method defined! - -The following code should output “{2, 3, 4, 7}”: -SinglyLinkedList lst = new SinglyLinkedList(); -lst.add(3); -lst.add(7); -lst.insert(4, 1); -lst.insert(2, 0); -System.out.println(lst); -*/ - -// NOTE: You need to make a separate class with a main() -// method to use this List object. This is simply the object -// definition. - -public class SinglyLinkedListInsert { - - Node head; - - // add() method from SinglyLinkedList DIY section, adds to end of list - public void add(int data) { - // Create a new node with given data - Node new_node = new Node(data); - - // If the Linked List is empty, - // then make the new node as head - if (head == null) { - head = new_node; - } else { - // Else traverse till the last node - // and insert the new_node there - Node last = head; - while (last.next != null) { - last = last.next; - } - - // Insert the new_node at last node - last.next = new_node; - } - } - - public void insert(int data, int pos) { - // Verify valid pos number - if (pos < 0) System.out.print("Invalid position"); - - // Create new node and keep track of current node - Node new_node = new Node(data); - Node current = head; - - // If pos is 0, set new node as head - if (pos == 0) { - new_node.next = head; - head = new_node; - } else { - while (pos-- >= 0) { - if (pos == 0) { - // Add new node after current position - new_node.next = current.next; - current.next = new_node; - break; - } - // Check if pos is out of bounds (reaches end) - if (current.next == null) { - System.out.println("Out of bounds"); - break; - } - current = current.next; - } - } - } - - @Override - public String toString() { - // For empty lists - if (head == null) return "{}"; - - String str = "{"; - - // Traverse the list until the last node - Node current = head; - while (current.next != null) { - str += "" + current.data + ", "; - current = current.next; - } - str += current.data + "}"; - return str; - } -} From 12e789aa703a86a5b82c1685adffcc519236c93a Mon Sep 17 00:00:00 2001 From: Rebecca Dang Date: Thu, 1 Jul 2021 16:02:10 -0700 Subject: [PATCH 3/4] Update and rename LinkedList practice template --- .../chapter16/practice/LinkedList.java | 27 ------------ .../chapter16/practice/TestLinkedList.java | 43 +++++++++++++++++++ 2 files changed, 43 insertions(+), 27 deletions(-) delete mode 100644 src/com/codefortomorrow/advanced/chapter16/practice/LinkedList.java create mode 100644 src/com/codefortomorrow/advanced/chapter16/practice/TestLinkedList.java diff --git a/src/com/codefortomorrow/advanced/chapter16/practice/LinkedList.java b/src/com/codefortomorrow/advanced/chapter16/practice/LinkedList.java deleted file mode 100644 index 04fd4f5..0000000 --- a/src/com/codefortomorrow/advanced/chapter16/practice/LinkedList.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.codefortomorrow.advanced.chapter16.practice; - -/** - * @author ArmeetJatyani - * March 2021 - * - * Implement a simple LinkedList - * You will need to create a LinkedListNode class, which represents each item/node in the linked list - * Assume that the elements in the linked list are all of type int - * Required functionality... - * - head(): get head of list - * - tail(): get tail of list - * - add(): add to tail of list - * - push(): push to head of list - * - pop(): remove head of list - * - toString(): return a String representation of the list - */ - -public class LinkedList { - - public static void main(String[] args) { - // write your code here - - } -} - -class LinkedListNode {} diff --git a/src/com/codefortomorrow/advanced/chapter16/practice/TestLinkedList.java b/src/com/codefortomorrow/advanced/chapter16/practice/TestLinkedList.java new file mode 100644 index 0000000..8764ba7 --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter16/practice/TestLinkedList.java @@ -0,0 +1,43 @@ +package com.codefortomorrow.advanced.chapter16.practice; + +/** + * @author ArmeetJatyani + * March 2021 + * + * Implement a simple LinkedList + * You will need to create a LinkedListNode class, which represents each item/node in the linked list + * Assume that the elements in the linked list are all of type int + * Required functionality... + * - head(): get head node of list + * - tail(): get tail node of list + * - add(): add element to the end of the list + * - insert(): insert an element at a given index position + * - push(): add element to the beginning of the list + * - pop(): remove head of list and return the node that was removed + * - clear(): remove all elements in the list + * - toString(): return a String representation of the list + * + * Create a driver class called TestLinkedList. In the main method, create an instance + * of LinkedList and test all of its methods at least once. (Print the list as needed + * to prove the methods worked.) + */ + +public class TestLinkedList { + + public static void main(String[] args) { + // write your code here + + } +} + +class Node { + // write your code here +} + +class LinkedListNode extends Node { + // write your code here +} + +class LinkedList { + // write your code here +} From 1c46f7a8d079554321141da0c9de9b4e5129c1f8 Mon Sep 17 00:00:00 2001 From: Rebecca Dang Date: Thu, 1 Jul 2021 18:17:11 -0700 Subject: [PATCH 4/4] Rename LinkedList to SinglyLinkedList This is to stay consistent with the curriculum, since the built-in LinkedList is a doubly linked list. --- ...TestLinkedList.java => TestSinglyLinkedList.java} | 4 ++-- ...TestLinkedList.java => TestSinglyLinkedList.java} | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) rename src/com/codefortomorrow/advanced/chapter16/practice/{TestLinkedList.java => TestSinglyLinkedList.java} (95%) rename src/com/codefortomorrow/advanced/chapter16/solutions/{TestLinkedList.java => TestSinglyLinkedList.java} (96%) diff --git a/src/com/codefortomorrow/advanced/chapter16/practice/TestLinkedList.java b/src/com/codefortomorrow/advanced/chapter16/practice/TestSinglyLinkedList.java similarity index 95% rename from src/com/codefortomorrow/advanced/chapter16/practice/TestLinkedList.java rename to src/com/codefortomorrow/advanced/chapter16/practice/TestSinglyLinkedList.java index 8764ba7..a8edfde 100644 --- a/src/com/codefortomorrow/advanced/chapter16/practice/TestLinkedList.java +++ b/src/com/codefortomorrow/advanced/chapter16/practice/TestSinglyLinkedList.java @@ -22,7 +22,7 @@ * to prove the methods worked.) */ -public class TestLinkedList { +public class TestSinglyLinkedList { public static void main(String[] args) { // write your code here @@ -38,6 +38,6 @@ class LinkedListNode extends Node { // write your code here } -class LinkedList { +class SinglyLinkedList { // write your code here } diff --git a/src/com/codefortomorrow/advanced/chapter16/solutions/TestLinkedList.java b/src/com/codefortomorrow/advanced/chapter16/solutions/TestSinglyLinkedList.java similarity index 96% rename from src/com/codefortomorrow/advanced/chapter16/solutions/TestLinkedList.java rename to src/com/codefortomorrow/advanced/chapter16/solutions/TestSinglyLinkedList.java index 3af705f..a9d5d95 100644 --- a/src/com/codefortomorrow/advanced/chapter16/solutions/TestLinkedList.java +++ b/src/com/codefortomorrow/advanced/chapter16/solutions/TestSinglyLinkedList.java @@ -22,14 +22,14 @@ * to prove the methods worked.) */ -public class TestLinkedList { +public class TestSinglyLinkedList { public static void main(String[] args) { // test default constructor - LinkedList list = new LinkedList(); + SinglyLinkedList list = new SinglyLinkedList(); System.out.println("Empty linked list: " + list); // test 1 arg constructor - list = new LinkedList(10); + list = new SinglyLinkedList(10); System.out.println("Linked list with 1 element: " + list); // test add method @@ -65,14 +65,14 @@ public static void main(String[] args) { } } -class LinkedList { +class SinglyLinkedList { private LinkedListNode head; /** * default constructor */ - public LinkedList() { + public SinglyLinkedList() { head = null; } @@ -80,7 +80,7 @@ public LinkedList() { * constructor with first value * @param value first element in the linked list */ - public LinkedList(int value) { + public SinglyLinkedList(int value) { // create first node head = new LinkedListNode(value, null); }