From b787ce4a8430c7fa799961995358b0f1d24c03a8 Mon Sep 17 00:00:00 2001 From: Truong Nhan Nguyen Date: Wed, 13 Mar 2024 10:03:35 +0700 Subject: [PATCH 1/6] chore: remove unused imports --- src/main/java/com/thealgorithms/backtracking/MColoring.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/backtracking/MColoring.java b/src/main/java/com/thealgorithms/backtracking/MColoring.java index c9bc02008058..93b17941566a 100644 --- a/src/main/java/com/thealgorithms/backtracking/MColoring.java +++ b/src/main/java/com/thealgorithms/backtracking/MColoring.java @@ -1,6 +1,5 @@ package com.thealgorithms.backtracking; -import java.io.*; import java.util.*; /** From 9397fe6eb51b5edda364b592df07040ebe98e807 Mon Sep 17 00:00:00 2001 From: Truong Nhan Nguyen Date: Wed, 13 Mar 2024 10:48:23 +0700 Subject: [PATCH 2/6] fix: parameterize references to generic types --- .../dynamicarray/DynamicArray.java | 5 ++-- .../lists/CircleLinkedList.java | 25 +++++++++++-------- .../lists/CursorLinkedList.java | 12 ++++----- .../thealgorithms/misc/ThreeSumProblem.java | 12 ++++----- .../com/thealgorithms/searches/UnionFind.java | 2 +- .../com/thealgorithms/strings/WordLadder.java | 12 ++++----- 6 files changed, 36 insertions(+), 32 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/dynamicarray/DynamicArray.java b/src/main/java/com/thealgorithms/datastructures/dynamicarray/DynamicArray.java index fb7783575e57..dabf7ef6edbf 100644 --- a/src/main/java/com/thealgorithms/datastructures/dynamicarray/DynamicArray.java +++ b/src/main/java/com/thealgorithms/datastructures/dynamicarray/DynamicArray.java @@ -54,7 +54,7 @@ public void add(final E element) { /** * Places element of type at the desired index * - * @param index the index for the element to be placed + * @param index the index for the element to be placed * @param element the element to be inserted */ public void put(final int index, E element) { @@ -120,6 +120,7 @@ private void fastRemove(final Object[] elements, final int index) { elements[this.size = newSize] = null; } + @SuppressWarnings("unchecked") private E getElement(final int index) { return (E) this.elements[index]; } @@ -145,7 +146,7 @@ public String toString() { * @return Iterator a Dynamic Array Iterator */ @Override - public Iterator iterator() { + public Iterator iterator() { return new DynamicArrayIterator(); } diff --git a/src/main/java/com/thealgorithms/datastructures/lists/CircleLinkedList.java b/src/main/java/com/thealgorithms/datastructures/lists/CircleLinkedList.java index 5d9f0b3a1d28..308d8c7b4822 100644 --- a/src/main/java/com/thealgorithms/datastructures/lists/CircleLinkedList.java +++ b/src/main/java/com/thealgorithms/datastructures/lists/CircleLinkedList.java @@ -13,14 +13,16 @@ private Node(E value, Node next) { } } - // For better O.O design this should be private allows for better black box design + // For better O.O design this should be private + // allows for better black box design private int size; // this will point to dummy node; private Node head = null; private Node tail = null; // keeping a tail pointer to keep track of the end of list - // constructor for class.. here we will make a dummy node for circly linked list implementation - // with reduced error catching as our list will never be empty; + // constructor for class.. here we will make a dummy node for circly linked + // list implementation with reduced error catching as our list will never + // be empty; public CircleLinkedList() { // creation of the dummy node head = new Node(null, head); @@ -33,8 +35,9 @@ public int getSize() { return size; } - // for the sake of simplistiy this class will only contain the append function or addLast other - // add functions can be implemented however this is the basses of them all really. + // for the sake of simplistiy this class will only contain the append + // function or addLast other add functions can be implemented however this + // is the basses of them all really. public void append(E value) { if (value == null) { // we do not want to add null elements to the list. @@ -53,7 +56,7 @@ public void append(E value) { // utility function for traversing the list public String toString() { - Node p = head.next; + Node p = head.next; String s = "[ "; while (p != head) { s += p.value; @@ -70,16 +73,16 @@ public E remove(int pos) { // catching errors throw new IndexOutOfBoundsException("position cannot be greater than size or negative"); } - // we need to keep track of the element before the element we want to remove we can see why - // bellow. + // we need to keep track of the element before the element we want to remove we + // can see why bellow. Node before = head; for (int i = 1; i <= pos; i++) { before = before.next; } Node destroy = before.next; E saved = destroy.value; - // assigning the next reference to the element following the element we want to remove... - // the last element will be assigned to the head. + // assigning the next reference to the element following the element we + // want to remove the last element will be assigned to the head. before.next = before.next.next; // scrubbing if (destroy == tail) { @@ -91,7 +94,7 @@ public E remove(int pos) { } public static void main(String[] args) { - CircleLinkedList cl = new CircleLinkedList(); + CircleLinkedList cl = new CircleLinkedList(); cl.append(12); System.out.println(cl); cl.append(23); diff --git a/src/main/java/com/thealgorithms/datastructures/lists/CursorLinkedList.java b/src/main/java/com/thealgorithms/datastructures/lists/CursorLinkedList.java index cefc47c27169..30cf54135cc7 100644 --- a/src/main/java/com/thealgorithms/datastructures/lists/CursorLinkedList.java +++ b/src/main/java/com/thealgorithms/datastructures/lists/CursorLinkedList.java @@ -58,7 +58,7 @@ public void printList() { /** * @return the logical index of the element within the list , not the actual - * index of the [cursorSpace] array + * index of the [cursorSpace] array */ public int indexOf(T element) { Objects.requireNonNull(element); @@ -74,9 +74,9 @@ public int indexOf(T element) { } /** - * @param position , the logical index of the element , not the actual one - * within the [cursorSpace] array . this method should be used to get the - * index give by indexOf() method. + * @param position, the logical index of the element , not the actual one + * within the [cursorSpace] array . this method should be + * used to get the index give by `indexOf()` method. * @return */ public T get(int position) { @@ -134,7 +134,7 @@ public void remove(T element) { } private void free(int index) { - Node os_node = cursorSpace[os]; + Node os_node = cursorSpace[os]; int os_next = os_node.next; cursorSpace[os].next = index; cursorSpace[index].element = null; @@ -172,7 +172,7 @@ private int alloc() { throw new OutOfMemoryError(); } - // 2- make the os point to the next of the @var{availableNodeIndex} + // 2- make the os point to the next of the @var{availableNodeIndex} cursorSpace[os].next = cursorSpace[availableNodeIndex].next; // this to indicate an end of the list , helpful at testing since any err diff --git a/src/main/java/com/thealgorithms/misc/ThreeSumProblem.java b/src/main/java/com/thealgorithms/misc/ThreeSumProblem.java index 1b72996d022b..54271272e6c7 100644 --- a/src/main/java/com/thealgorithms/misc/ThreeSumProblem.java +++ b/src/main/java/com/thealgorithms/misc/ThreeSumProblem.java @@ -29,7 +29,7 @@ public List> BruteForce(int[] nums, int target) { for (int j = i + 1; j < nums.length; j++) { for (int k = j + 1; k < nums.length; k++) { if (nums[i] + nums[j] + nums[k] == target) { - List temp = new ArrayList<>(); + List temp = new ArrayList(); temp.add(nums[i]); temp.add(nums[j]); temp.add(nums[k]); @@ -54,7 +54,7 @@ public List> TwoPointer(int[] nums, int target) { end = nums.length - 1; while (start < end) { if (nums[start] + nums[end] + nums[i] == target) { - List temp = new ArrayList<>(); + List temp = new ArrayList(); temp.add(nums[i]); temp.add(nums[start]); temp.add(nums[end]); @@ -75,8 +75,8 @@ public List> TwoPointer(int[] nums, int target) { public List> Hashmap(int[] nums, int target) { Arrays.sort(nums); - Set> ts = new HashSet(); - HashMap hm = new HashMap<>(); + Set> ts = new HashSet>(); + HashMap hm = new HashMap(); for (int i = 0; i < nums.length; i++) { hm.put(nums[i], i); @@ -86,7 +86,7 @@ public List> Hashmap(int[] nums, int target) { for (int j = i + 1; j < nums.length; j++) { int t = target - nums[i] - nums[j]; if (hm.containsKey(t) && hm.get(t) > j) { - List temp = new ArrayList<>(); + List temp = new ArrayList(); temp.add(nums[i]); temp.add(nums[j]); temp.add(t); @@ -94,6 +94,6 @@ public List> Hashmap(int[] nums, int target) { } } } - return new ArrayList(ts); + return new ArrayList>(ts); } } diff --git a/src/main/java/com/thealgorithms/searches/UnionFind.java b/src/main/java/com/thealgorithms/searches/UnionFind.java index d32e4fd3ec1f..b55e8667beb0 100644 --- a/src/main/java/com/thealgorithms/searches/UnionFind.java +++ b/src/main/java/com/thealgorithms/searches/UnionFind.java @@ -45,7 +45,7 @@ public void union(int x, int y) { } public int count() { - List parents = new ArrayList(); + List parents = new ArrayList(); for (int i = 0; i < p.length; i++) { if (!parents.contains(find(i))) { parents.add(find(i)); diff --git a/src/main/java/com/thealgorithms/strings/WordLadder.java b/src/main/java/com/thealgorithms/strings/WordLadder.java index a4634ce01ee0..cc2a1ab83e18 100644 --- a/src/main/java/com/thealgorithms/strings/WordLadder.java +++ b/src/main/java/com/thealgorithms/strings/WordLadder.java @@ -44,20 +44,20 @@ class WordLadder { * This function finds the ladderLength * * @param beginWord: Starting word of the ladder - * @param endWord: Ending word of the ladder - * @param wordList: This list contains the words which needs to be included - * in ladder. + * @param endWord: Ending word of the ladder + * @param wordList: This list contains the words which needs to be included + * in ladder. * @return ladderLength: This function will return the ladderLength(level) - * if the endword is there. Otherwise, will return the length as 0. + * if the endword is there. Otherwise, will return the length as 0. */ public static int ladderLength(String beginWord, String endWord, List wordList) { - HashSet set = new HashSet(wordList); + HashSet set = new HashSet(wordList); if (!set.contains(endWord)) { return 0; } - Queue queue = new LinkedList(); + Queue queue = new LinkedList(); queue.offer(beginWord); int level = 1; From 5e87a60cd19ee23d5c25524c74b4bf141a09c62f Mon Sep 17 00:00:00 2001 From: Truong Nhan Nguyen Date: Sat, 16 Mar 2024 00:14:40 +0700 Subject: [PATCH 3/6] chore: remove unneeded suppression `@SuppressWarnings("unchecked")` --- .../thealgorithms/datastructures/dynamicarray/DynamicArray.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/datastructures/dynamicarray/DynamicArray.java b/src/main/java/com/thealgorithms/datastructures/dynamicarray/DynamicArray.java index dabf7ef6edbf..2c7cb18f0a5b 100644 --- a/src/main/java/com/thealgorithms/datastructures/dynamicarray/DynamicArray.java +++ b/src/main/java/com/thealgorithms/datastructures/dynamicarray/DynamicArray.java @@ -120,7 +120,6 @@ private void fastRemove(final Object[] elements, final int index) { elements[this.size = newSize] = null; } - @SuppressWarnings("unchecked") private E getElement(final int index) { return (E) this.elements[index]; } From bf285994a90c2026214752d691e6eeae7b8b92f9 Mon Sep 17 00:00:00 2001 From: Truong Nhan Nguyen Date: Sat, 16 Mar 2024 00:24:35 +0700 Subject: [PATCH 4/6] chore: remove boilerplate code --- .../datastructures/lists/CircleLinkedList.java | 2 +- .../java/com/thealgorithms/misc/ThreeSumProblem.java | 12 ++++++------ .../java/com/thealgorithms/searches/UnionFind.java | 2 +- .../java/com/thealgorithms/strings/WordLadder.java | 4 ++-- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/lists/CircleLinkedList.java b/src/main/java/com/thealgorithms/datastructures/lists/CircleLinkedList.java index 308d8c7b4822..e8b4390a4655 100644 --- a/src/main/java/com/thealgorithms/datastructures/lists/CircleLinkedList.java +++ b/src/main/java/com/thealgorithms/datastructures/lists/CircleLinkedList.java @@ -94,7 +94,7 @@ public E remove(int pos) { } public static void main(String[] args) { - CircleLinkedList cl = new CircleLinkedList(); + CircleLinkedList cl = new CircleLinkedList<>(); cl.append(12); System.out.println(cl); cl.append(23); diff --git a/src/main/java/com/thealgorithms/misc/ThreeSumProblem.java b/src/main/java/com/thealgorithms/misc/ThreeSumProblem.java index 54271272e6c7..a232ad986970 100644 --- a/src/main/java/com/thealgorithms/misc/ThreeSumProblem.java +++ b/src/main/java/com/thealgorithms/misc/ThreeSumProblem.java @@ -29,7 +29,7 @@ public List> BruteForce(int[] nums, int target) { for (int j = i + 1; j < nums.length; j++) { for (int k = j + 1; k < nums.length; k++) { if (nums[i] + nums[j] + nums[k] == target) { - List temp = new ArrayList(); + List temp = new ArrayList<>(); temp.add(nums[i]); temp.add(nums[j]); temp.add(nums[k]); @@ -54,7 +54,7 @@ public List> TwoPointer(int[] nums, int target) { end = nums.length - 1; while (start < end) { if (nums[start] + nums[end] + nums[i] == target) { - List temp = new ArrayList(); + List temp = new ArrayList<>(); temp.add(nums[i]); temp.add(nums[start]); temp.add(nums[end]); @@ -75,8 +75,8 @@ public List> TwoPointer(int[] nums, int target) { public List> Hashmap(int[] nums, int target) { Arrays.sort(nums); - Set> ts = new HashSet>(); - HashMap hm = new HashMap(); + Set> ts = new HashSet<>(); + HashMap hm = new HashMap<>(); for (int i = 0; i < nums.length; i++) { hm.put(nums[i], i); @@ -86,7 +86,7 @@ public List> Hashmap(int[] nums, int target) { for (int j = i + 1; j < nums.length; j++) { int t = target - nums[i] - nums[j]; if (hm.containsKey(t) && hm.get(t) > j) { - List temp = new ArrayList(); + List temp = new ArrayList<>(); temp.add(nums[i]); temp.add(nums[j]); temp.add(t); @@ -94,6 +94,6 @@ public List> Hashmap(int[] nums, int target) { } } } - return new ArrayList>(ts); + return new ArrayList<>(ts); } } diff --git a/src/main/java/com/thealgorithms/searches/UnionFind.java b/src/main/java/com/thealgorithms/searches/UnionFind.java index b55e8667beb0..4dfece0e6381 100644 --- a/src/main/java/com/thealgorithms/searches/UnionFind.java +++ b/src/main/java/com/thealgorithms/searches/UnionFind.java @@ -45,7 +45,7 @@ public void union(int x, int y) { } public int count() { - List parents = new ArrayList(); + List parents = new ArrayList<>(); for (int i = 0; i < p.length; i++) { if (!parents.contains(find(i))) { parents.add(find(i)); diff --git a/src/main/java/com/thealgorithms/strings/WordLadder.java b/src/main/java/com/thealgorithms/strings/WordLadder.java index cc2a1ab83e18..93a929e35196 100644 --- a/src/main/java/com/thealgorithms/strings/WordLadder.java +++ b/src/main/java/com/thealgorithms/strings/WordLadder.java @@ -51,13 +51,13 @@ class WordLadder { * if the endword is there. Otherwise, will return the length as 0. */ public static int ladderLength(String beginWord, String endWord, List wordList) { - HashSet set = new HashSet(wordList); + HashSet set = new HashSet<>(wordList); if (!set.contains(endWord)) { return 0; } - Queue queue = new LinkedList(); + Queue queue = new LinkedList<>(); queue.offer(beginWord); int level = 1; From 8c80f6c3027ff1acf55a5ddf828aec96971e465d Mon Sep 17 00:00:00 2001 From: Truong Nhan Nguyen Date: Sat, 16 Mar 2024 00:50:12 +0700 Subject: [PATCH 5/6] chore: revert doc-strings to original form --- .../dynamicarray/DynamicArray.java | 2 +- .../lists/CircleLinkedList.java | 21 ++++++++----------- .../lists/CursorLinkedList.java | 12 +++++------ .../com/thealgorithms/strings/WordLadder.java | 8 +++---- 4 files changed, 20 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/dynamicarray/DynamicArray.java b/src/main/java/com/thealgorithms/datastructures/dynamicarray/DynamicArray.java index 2c7cb18f0a5b..f6f0276e0c35 100644 --- a/src/main/java/com/thealgorithms/datastructures/dynamicarray/DynamicArray.java +++ b/src/main/java/com/thealgorithms/datastructures/dynamicarray/DynamicArray.java @@ -54,7 +54,7 @@ public void add(final E element) { /** * Places element of type at the desired index * - * @param index the index for the element to be placed + * @param index the index for the element to be placed * @param element the element to be inserted */ public void put(final int index, E element) { diff --git a/src/main/java/com/thealgorithms/datastructures/lists/CircleLinkedList.java b/src/main/java/com/thealgorithms/datastructures/lists/CircleLinkedList.java index e8b4390a4655..c42b10455d14 100644 --- a/src/main/java/com/thealgorithms/datastructures/lists/CircleLinkedList.java +++ b/src/main/java/com/thealgorithms/datastructures/lists/CircleLinkedList.java @@ -13,16 +13,14 @@ private Node(E value, Node next) { } } - // For better O.O design this should be private - // allows for better black box design + // For better O.O design this should be private allows for better black box design private int size; // this will point to dummy node; private Node head = null; private Node tail = null; // keeping a tail pointer to keep track of the end of list - // constructor for class.. here we will make a dummy node for circly linked - // list implementation with reduced error catching as our list will never - // be empty; + // constructor for class.. here we will make a dummy node for circly linked list implementation + // with reduced error catching as our list will never be empty; public CircleLinkedList() { // creation of the dummy node head = new Node(null, head); @@ -35,9 +33,8 @@ public int getSize() { return size; } - // for the sake of simplistiy this class will only contain the append - // function or addLast other add functions can be implemented however this - // is the basses of them all really. + // for the sake of simplistiy this class will only contain the append function or addLast other + // add functions can be implemented however this is the basses of them all really. public void append(E value) { if (value == null) { // we do not want to add null elements to the list. @@ -73,16 +70,16 @@ public E remove(int pos) { // catching errors throw new IndexOutOfBoundsException("position cannot be greater than size or negative"); } - // we need to keep track of the element before the element we want to remove we - // can see why bellow. + // we need to keep track of the element before the element we want to remove we can see why + // bellow. Node before = head; for (int i = 1; i <= pos; i++) { before = before.next; } Node destroy = before.next; E saved = destroy.value; - // assigning the next reference to the element following the element we - // want to remove the last element will be assigned to the head. + // assigning the next reference to the element following the element we want to remove... + // the last element will be assigned to the head. before.next = before.next.next; // scrubbing if (destroy == tail) { diff --git a/src/main/java/com/thealgorithms/datastructures/lists/CursorLinkedList.java b/src/main/java/com/thealgorithms/datastructures/lists/CursorLinkedList.java index 30cf54135cc7..89e4e69a7f0c 100644 --- a/src/main/java/com/thealgorithms/datastructures/lists/CursorLinkedList.java +++ b/src/main/java/com/thealgorithms/datastructures/lists/CursorLinkedList.java @@ -56,9 +56,9 @@ public void printList() { } } - /** + /** * @return the logical index of the element within the list , not the actual - * index of the [cursorSpace] array + * index of the [cursorSpace] array */ public int indexOf(T element) { Objects.requireNonNull(element); @@ -74,9 +74,9 @@ public int indexOf(T element) { } /** - * @param position, the logical index of the element , not the actual one - * within the [cursorSpace] array . this method should be - * used to get the index give by `indexOf()` method. + * @param position , the logical index of the element , not the actual one + * within the [cursorSpace] array . this method should be used to get the + * index give by indexOf() method. * @return */ public T get(int position) { @@ -172,7 +172,7 @@ private int alloc() { throw new OutOfMemoryError(); } - // 2- make the os point to the next of the @var{availableNodeIndex} + // 2- make the os point to the next of the @var{availableNodeIndex} cursorSpace[os].next = cursorSpace[availableNodeIndex].next; // this to indicate an end of the list , helpful at testing since any err diff --git a/src/main/java/com/thealgorithms/strings/WordLadder.java b/src/main/java/com/thealgorithms/strings/WordLadder.java index 93a929e35196..e88acbd18586 100644 --- a/src/main/java/com/thealgorithms/strings/WordLadder.java +++ b/src/main/java/com/thealgorithms/strings/WordLadder.java @@ -44,11 +44,11 @@ class WordLadder { * This function finds the ladderLength * * @param beginWord: Starting word of the ladder - * @param endWord: Ending word of the ladder - * @param wordList: This list contains the words which needs to be included - * in ladder. + * @param endWord: Ending word of the ladder + * @param wordList: This list contains the words which needs to be included + * in ladder. * @return ladderLength: This function will return the ladderLength(level) - * if the endword is there. Otherwise, will return the length as 0. + * if the endword is there. Otherwise, will return the length as 0. */ public static int ladderLength(String beginWord, String endWord, List wordList) { HashSet set = new HashSet<>(wordList); From 584de189fa3759fbd221bc05ad3b3b65e5a67f6b Mon Sep 17 00:00:00 2001 From: Truong Nhan Nguyen Date: Sat, 16 Mar 2024 00:53:02 +0700 Subject: [PATCH 6/6] chore: reformat `CursorLinkedList.java` file --- .../thealgorithms/datastructures/lists/CursorLinkedList.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/datastructures/lists/CursorLinkedList.java b/src/main/java/com/thealgorithms/datastructures/lists/CursorLinkedList.java index 89e4e69a7f0c..22399bd6a459 100644 --- a/src/main/java/com/thealgorithms/datastructures/lists/CursorLinkedList.java +++ b/src/main/java/com/thealgorithms/datastructures/lists/CursorLinkedList.java @@ -56,7 +56,7 @@ public void printList() { } } - /** + /** * @return the logical index of the element within the list , not the actual * index of the [cursorSpace] array */