From 9b5ced457c6e1e78c5d958a7b0d7ccc678f773c5 Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Wed, 2 Oct 2024 16:37:49 +0500 Subject: [PATCH 01/16] Sorted Linked List added with Javadoc and tests --- .../lists/SortedLinkedList.java | 161 ++++++++++++++++++ .../lists/SortedlinkedListTest.java | 88 ++++++++++ 2 files changed, 249 insertions(+) create mode 100644 src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java create mode 100644 src/test/java/com/thealgorithms/datastructures/lists/SortedlinkedListTest.java diff --git a/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java b/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java new file mode 100644 index 000000000000..2090fa505061 --- /dev/null +++ b/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java @@ -0,0 +1,161 @@ +package com.thealgorithms.datastructures.lists; + +import java.util.ArrayList; + +/** + * A SortedLinkedList is a data structure that maintains a sorted list of elements. + * Elements are ordered based on their natural ordering or by a Comparator provided at the time of creation. + * + * @author Muhammad Junaid Khalid + * @param int the type of elements in this list + */ + +public class SortedLinkedList { + private Node head; + private Node tail; + + public SortedLinkedList(){ + this.head=null; + this.tail=null; + } + + /** + * Inserts a new element into the sorted linked list. + * + * @param value the value to be inserted + */ + public void insert(int value){ + Node newNode = new Node(value); + if (head == null) { + this.head = newNode; + this.tail = newNode; + } + else if (value < head.value) { + newNode.next = this.head; + this.head = newNode; + } + else if (value > tail.value) { + this.tail.next = newNode; + } + else{ + Node temp=head; + while (temp.next != null && temp.next.value < value) { + temp = temp.next; + } + newNode.next = temp.next; + temp.next = newNode; + } + } + + /** + * Displays the elements of the sorted linked list. + */ + public void display(){ + System.out.println(this.toString()); + } + + /** + * Deletes the first occurrence of the specified element in the sorted linked list. + * + * @param value the value to be deleted + * @return true if the element is found and deleted, false otherwise + */ + public boolean delete(int value){ + if (this.head == null) { + return false; + } + else if (this.head.value == value) { + this.head = this.head.next; + return true; + } + else{ + Node temp = this.head; + while (temp.next != null) { + if (temp.next.value == value) { + temp.next = temp.next.next; + return true; + } + temp = temp.next; + } + return false; + + } + } + + /** + * Searches for the specified element in the sorted linked list. + * + * @param value the value to be searched + * @return true if the element is found, false otherwise + */ + public boolean search(int value){ + Node temp = this.head; + while (temp != null) { + if (temp.value == value) { + return true; + } + temp = temp.next; + } + return false; + } + + /** + * Checks if the sorted linked list is empty. + * + * @return true if the list is empty, false otherwise + */ + public boolean isEmpty() { + return head == null; + } + + /** + * Returns the minimum value in the sorted linked list. + * + * @return the minimum value + */ + public int minValue(){ + return this.head.value; + } + + /** + * Returns the maximum value in the sorted linked list. + * + * @return the maximum value + */ + public int maxValue(){ + return this.tail.value; + } + + /** + * Returns a string representation of the sorted linked list. + * + * @return a string representation of the sorted linked list + */ + @Override + public String toString() { + ArrayList elements=new ArrayList<>(); + Node temp = this.head; + while (temp != null) { + elements.add(String.valueOf(temp.value)); + temp = temp.next; + } + return String.join(", ", elements); + } + + + public class Node { + public int value; + public Node next; + + public Node(){ + this.value = 0; + this.next= null; + } + + public Node(int value){ + this.value = value; + this.next = null; + } + + } +} diff --git a/src/test/java/com/thealgorithms/datastructures/lists/SortedlinkedListTest.java b/src/test/java/com/thealgorithms/datastructures/lists/SortedlinkedListTest.java new file mode 100644 index 000000000000..f12294767466 --- /dev/null +++ b/src/test/java/com/thealgorithms/datastructures/lists/SortedlinkedListTest.java @@ -0,0 +1,88 @@ +package com.thealgorithms.datastructures.lists; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +public class SortedLinkedListTest { + + @Test + public void testInsert() { + SortedLinkedList list = new SortedLinkedList(); + list.insert(5); + list.insert(3); + list.insert(7); + assertEquals("3, 5, 7", list.toString()); + } + + @Test + public void testDelete() { + SortedLinkedList list = new SortedLinkedList(); + list.insert(5); + list.insert(3); + list.insert(7); + assertTrue(list.delete(5)); + assertEquals("3, 7", list.toString()); + assertFalse(list.delete(10)); + } + + @Test + public void testSearch() { + SortedLinkedList list = new SortedLinkedList(); + list.insert(5); + list.insert(3); + list.insert(7); + assertTrue(list.search(5)); + assertFalse(list.search(10)); + } + + @Test + public void testMinValue() { + SortedLinkedList list = new SortedLinkedList(); + list.insert(5); + list.insert(3); + list.insert(7); + assertEquals(3, list.minValue()); + } + + @Test + public void testMaxValue() { + SortedLinkedList list = new SortedLinkedList(); + list.insert(5); + list.insert(3); + list.insert(7); + assertEquals(7, list.maxValue()); + } + + @Test + public void testEmptyList() { + SortedLinkedList list = new SortedLinkedList(); + assertEquals("", list.toString()); + assertFalse(list.delete(5)); + assertFalse(list.search(5)); + assertEquals(0, list.minValue()); + assertEquals(0, list.maxValue()); + } + @Test + public void testIsEmpty_onEmptyList() { + SortedLinkedList list = new SortedLinkedList(); + assertTrue(list.isEmpty()); + } + + @Test + public void testIsEmpty_onNonEmptyList() { + SortedLinkedList list = new SortedLinkedList(); + list.insert(10); + assertFalse(list.isEmpty()); + } + + @Test + public void testIsEmpty_afterDeletion() { + SortedLinkedList list = new SortedLinkedList(); + list.insert(10); + list.delete(10); + assertTrue(list.isEmpty()); + } +} From e1382c1ac040f03e1497957a1799b542ba9b0827 Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Wed, 2 Oct 2024 16:49:14 +0500 Subject: [PATCH 02/16] "Added comments to SortedLinkedList.java to describe the implementation and provide a reference link." --- .../thealgorithms/datastructures/lists/SortedLinkedList.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java b/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java index 2090fa505061..bde6a97de84a 100644 --- a/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java +++ b/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java @@ -5,7 +5,10 @@ /** * A SortedLinkedList is a data structure that maintains a sorted list of elements. * Elements are ordered based on their natural ordering or by a Comparator provided at the time of creation. - * + * This implementation uses a singly linked list to store the elements. + * Further details can be found on this link + * https://runestone.academy/ns/books/published/cppds/LinearLinked/ImplementinganOrderedList.html + * * @author Muhammad Junaid Khalid * @param int the type of elements in this list */ From 3ec762783b4e3a96ba686e6c1771de17e834b2f7 Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Wed, 2 Oct 2024 16:56:07 +0500 Subject: [PATCH 03/16] Upgraded test from junit 4 to junit 5 --- .../datastructures/lists/SortedlinkedListTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/test/java/com/thealgorithms/datastructures/lists/SortedlinkedListTest.java b/src/test/java/com/thealgorithms/datastructures/lists/SortedlinkedListTest.java index f12294767466..5c19cd078b5d 100644 --- a/src/test/java/com/thealgorithms/datastructures/lists/SortedlinkedListTest.java +++ b/src/test/java/com/thealgorithms/datastructures/lists/SortedlinkedListTest.java @@ -1,10 +1,10 @@ package com.thealgorithms.datastructures.lists; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; -import org.junit.Test; +import org.junit.jupiter.api.Test; public class SortedLinkedListTest { From 6ff74f0ea33e0df179e3c99404a8ad0ebc158710 Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Wed, 2 Oct 2024 17:02:53 +0500 Subject: [PATCH 04/16] Added space before braces of functions --- .../datastructures/lists/SortedLinkedList.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java b/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java index bde6a97de84a..018329253518 100644 --- a/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java +++ b/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java @@ -17,7 +17,7 @@ public class SortedLinkedList { private Node head; private Node tail; - public SortedLinkedList(){ + public SortedLinkedList() { this.head=null; this.tail=null; } @@ -27,7 +27,7 @@ public SortedLinkedList(){ * * @param value the value to be inserted */ - public void insert(int value){ + public void insert(int value) { Node newNode = new Node(value); if (head == null) { this.head = newNode; @@ -53,7 +53,7 @@ else if (value > tail.value) { /** * Displays the elements of the sorted linked list. */ - public void display(){ + public void display() { System.out.println(this.toString()); } @@ -63,7 +63,7 @@ public void display(){ * @param value the value to be deleted * @return true if the element is found and deleted, false otherwise */ - public boolean delete(int value){ + public boolean delete(int value) { if (this.head == null) { return false; } @@ -91,7 +91,7 @@ else if (this.head.value == value) { * @param value the value to be searched * @return true if the element is found, false otherwise */ - public boolean search(int value){ + public boolean search(int value) { Node temp = this.head; while (temp != null) { if (temp.value == value) { @@ -116,7 +116,7 @@ public boolean isEmpty() { * * @return the minimum value */ - public int minValue(){ + public int minValue() { return this.head.value; } @@ -125,7 +125,7 @@ public int minValue(){ * * @return the maximum value */ - public int maxValue(){ + public int maxValue() { return this.tail.value; } @@ -150,12 +150,12 @@ public class Node { public int value; public Node next; - public Node(){ + public Node() { this.value = 0; this.next= null; } - public Node(int value){ + public Node(int value) { this.value = value; this.next = null; } From dd91a15949be35a10cd8916af9d76a100ebf3369 Mon Sep 17 00:00:00 2001 From: Muhammad Junaid Khalid Date: Wed, 2 Oct 2024 17:07:24 +0500 Subject: [PATCH 05/16] Rename SortedlinkedListTest.java to SortedLinkedListTest.java --- .../{SortedlinkedListTest.java => SortedLinkedListTest.java} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/test/java/com/thealgorithms/datastructures/lists/{SortedlinkedListTest.java => SortedLinkedListTest.java} (100%) diff --git a/src/test/java/com/thealgorithms/datastructures/lists/SortedlinkedListTest.java b/src/test/java/com/thealgorithms/datastructures/lists/SortedLinkedListTest.java similarity index 100% rename from src/test/java/com/thealgorithms/datastructures/lists/SortedlinkedListTest.java rename to src/test/java/com/thealgorithms/datastructures/lists/SortedLinkedListTest.java From 2c33f89f132560af95602a3c2c506f812f1fbb8b Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Wed, 2 Oct 2024 17:12:08 +0500 Subject: [PATCH 06/16] made to string null safe --- .../lists/SortedLinkedList.java | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java b/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java index 018329253518..59c34fdc021f 100644 --- a/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java +++ b/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java @@ -18,8 +18,8 @@ public class SortedLinkedList { private Node tail; public SortedLinkedList() { - this.head=null; - this.tail=null; + this.head = null; + this.tail = null; } /** @@ -41,7 +41,7 @@ else if (value > tail.value) { this.tail.next = newNode; } else{ - Node temp=head; + Node temp = head; while (temp.next != null && temp.next.value < value) { temp = temp.next; } @@ -136,13 +136,18 @@ public int maxValue() { */ @Override public String toString() { - ArrayList elements=new ArrayList<>(); - Node temp = this.head; - while (temp != null) { - elements.add(String.valueOf(temp.value)); - temp = temp.next; + if (this.head != null) { + ArrayList elements=new ArrayList<>(); + Node temp = this.head; + while (temp != null) { + elements.add(String.valueOf(temp.value)); + temp = temp.next; + } + return String.join(", ", elements); + } + else { + return ""; } - return String.join(", ", elements); } @@ -152,7 +157,7 @@ public class Node { public Node() { this.value = 0; - this.next= null; + this.next = null; } public Node(int value) { From 58390c1d9c47fa7b2a2f6160e160664050d66bb7 Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Wed, 2 Oct 2024 17:32:15 +0500 Subject: [PATCH 07/16] Updated tail --- .../datastructures/lists/SortedLinkedList.java | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java b/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java index 59c34fdc021f..948e533f085b 100644 --- a/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java +++ b/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java @@ -47,6 +47,9 @@ else if (value > tail.value) { } newNode.next = temp.next; temp.next = newNode; + if (newNode.next==null){ + this.tail=newNode; + } } } @@ -63,25 +66,33 @@ public void display() { * @param value the value to be deleted * @return true if the element is found and deleted, false otherwise */ - public boolean delete(int value) { + public boolean delete(int value){ if (this.head == null) { return false; } else if (this.head.value == value) { - this.head = this.head.next; + if (this.head.next == null) { + this.head = null; + this.tail = null; + } else { + this.head = this.head.next; + } return true; } else{ Node temp = this.head; while (temp.next != null) { if (temp.next.value == value) { + if (temp.next == this.tail) { + this.tail = temp; + } temp.next = temp.next.next; return true; } temp = temp.next; } return false; - + } } From 028727f19366d58b5bb4db83072bd7a1e06a3e1c Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Wed, 2 Oct 2024 17:38:06 +0500 Subject: [PATCH 08/16] "Added assignment of `this.tail` to `newNode` in `SortedLinkedList` class." --- .../com/thealgorithms/datastructures/lists/SortedLinkedList.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java b/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java index 948e533f085b..837697cd23f7 100644 --- a/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java +++ b/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java @@ -39,6 +39,7 @@ else if (value < head.value) { } else if (value > tail.value) { this.tail.next = newNode; + this.tail = newNode; } else{ Node temp = head; From 3dce7f0ff797dad43181b8b10f64dafbf6252914 Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Wed, 2 Oct 2024 17:48:52 +0500 Subject: [PATCH 09/16] Remove assertions for minValue and maxValue in empty list test --- .../datastructures/lists/SortedLinkedListTest.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/test/java/com/thealgorithms/datastructures/lists/SortedLinkedListTest.java b/src/test/java/com/thealgorithms/datastructures/lists/SortedLinkedListTest.java index 5c19cd078b5d..9c4a185c5155 100644 --- a/src/test/java/com/thealgorithms/datastructures/lists/SortedLinkedListTest.java +++ b/src/test/java/com/thealgorithms/datastructures/lists/SortedLinkedListTest.java @@ -62,8 +62,6 @@ public void testEmptyList() { assertEquals("", list.toString()); assertFalse(list.delete(5)); assertFalse(list.search(5)); - assertEquals(0, list.minValue()); - assertEquals(0, list.maxValue()); } @Test public void testIsEmpty_onEmptyList() { From 9e0cdf3135967e59e5cc77f7ebb4bc19b1c0543d Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Wed, 2 Oct 2024 17:56:57 +0500 Subject: [PATCH 10/16] tried to get link updated --- .../lists/SortedLinkedList.java | 31 +++++-------------- 1 file changed, 8 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java b/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java index 837697cd23f7..12a40e18d696 100644 --- a/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java +++ b/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java @@ -8,7 +8,6 @@ * This implementation uses a singly linked list to store the elements. * Further details can be found on this link * https://runestone.academy/ns/books/published/cppds/LinearLinked/ImplementinganOrderedList.html - * * @author Muhammad Junaid Khalid * @param int the type of elements in this list */ @@ -24,7 +23,6 @@ public SortedLinkedList() { /** * Inserts a new element into the sorted linked list. - * * @param value the value to be inserted */ public void insert(int value) { @@ -32,16 +30,13 @@ public void insert(int value) { if (head == null) { this.head = newNode; this.tail = newNode; - } - else if (value < head.value) { + } else if (value < head.value) { newNode.next = this.head; this.head = newNode; - } - else if (value > tail.value) { + } else if (value > tail.value) { this.tail.next = newNode; this.tail = newNode; - } - else{ + } else { Node temp = head; while (temp.next != null && temp.next.value < value) { temp = temp.next; @@ -63,15 +58,13 @@ public void display() { /** * Deletes the first occurrence of the specified element in the sorted linked list. - * * @param value the value to be deleted * @return true if the element is found and deleted, false otherwise */ - public boolean delete(int value){ + public boolean delete(int value) { if (this.head == null) { return false; - } - else if (this.head.value == value) { + } else if (this.head.value == value) { if (this.head.next == null) { this.head = null; this.tail = null; @@ -79,8 +72,7 @@ else if (this.head.value == value) { this.head = this.head.next; } return true; - } - else{ + } else{ Node temp = this.head; while (temp.next != null) { if (temp.next.value == value) { @@ -93,13 +85,11 @@ else if (this.head.value == value) { temp = temp.next; } return false; - } } /** * Searches for the specified element in the sorted linked list. - * * @param value the value to be searched * @return true if the element is found, false otherwise */ @@ -116,7 +106,6 @@ public boolean search(int value) { /** * Checks if the sorted linked list is empty. - * * @return true if the list is empty, false otherwise */ public boolean isEmpty() { @@ -125,7 +114,6 @@ public boolean isEmpty() { /** * Returns the minimum value in the sorted linked list. - * * @return the minimum value */ public int minValue() { @@ -134,7 +122,6 @@ public int minValue() { /** * Returns the maximum value in the sorted linked list. - * * @return the maximum value */ public int maxValue() { @@ -143,21 +130,19 @@ public int maxValue() { /** * Returns a string representation of the sorted linked list. - * * @return a string representation of the sorted linked list */ @Override public String toString() { if (this.head != null) { - ArrayList elements=new ArrayList<>(); + ArrayList elements = new ArrayList<>(); Node temp = this.head; while (temp != null) { elements.add(String.valueOf(temp.value)); temp = temp.next; } return String.join(", ", elements); - } - else { + } else { return ""; } } From 1c2bb10dfbd27639627741a2f4fe97b5a3c2fa1b Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Wed, 2 Oct 2024 18:05:02 +0500 Subject: [PATCH 11/16] "Fixed whitespace and formatting issues in SortedLinkedList.java and SortedLinkedListTest.java" --- .../datastructures/lists/SortedLinkedList.java | 7 +++---- .../datastructures/lists/SortedLinkedListTest.java | 6 +++--- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java b/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java index 12a40e18d696..0f084ca0e87a 100644 --- a/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java +++ b/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java @@ -43,8 +43,8 @@ public void insert(int value) { } newNode.next = temp.next; temp.next = newNode; - if (newNode.next==null){ - this.tail=newNode; + if (newNode.next == null) { + this.tail = newNode; } } } @@ -72,7 +72,7 @@ public boolean delete(int value) { this.head = this.head.next; } return true; - } else{ + } else { Node temp = this.head; while (temp.next != null) { if (temp.next.value == value) { @@ -146,7 +146,6 @@ public String toString() { return ""; } } - public class Node { public int value; diff --git a/src/test/java/com/thealgorithms/datastructures/lists/SortedLinkedListTest.java b/src/test/java/com/thealgorithms/datastructures/lists/SortedLinkedListTest.java index 9c4a185c5155..53f1faf5ff69 100644 --- a/src/test/java/com/thealgorithms/datastructures/lists/SortedLinkedListTest.java +++ b/src/test/java/com/thealgorithms/datastructures/lists/SortedLinkedListTest.java @@ -64,20 +64,20 @@ public void testEmptyList() { assertFalse(list.search(5)); } @Test - public void testIsEmpty_onEmptyList() { + public void testIsEmptyOnEmptyList() { SortedLinkedList list = new SortedLinkedList(); assertTrue(list.isEmpty()); } @Test - public void testIsEmpty_onNonEmptyList() { + public void testIsEmptyOnNonEmptyList() { SortedLinkedList list = new SortedLinkedList(); list.insert(10); assertFalse(list.isEmpty()); } @Test - public void testIsEmpty_afterDeletion() { + public void testIsEmptyAfterDeletion() { SortedLinkedList list = new SortedLinkedList(); list.insert(10); list.delete(10); From 25be88e4a9b958cd8715da86019131731fb04b3a Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Wed, 2 Oct 2024 18:09:29 +0500 Subject: [PATCH 12/16] formatting of test file corrected --- .../datastructures/lists/SortedLinkedListTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/datastructures/lists/SortedLinkedListTest.java b/src/test/java/com/thealgorithms/datastructures/lists/SortedLinkedListTest.java index 53f1faf5ff69..8d130a9731c0 100644 --- a/src/test/java/com/thealgorithms/datastructures/lists/SortedLinkedListTest.java +++ b/src/test/java/com/thealgorithms/datastructures/lists/SortedLinkedListTest.java @@ -7,7 +7,7 @@ import org.junit.jupiter.api.Test; public class SortedLinkedListTest { - + @Test public void testInsert() { SortedLinkedList list = new SortedLinkedList(); From 0a5495d07c2c07974bb4663c3cde606697dee0c7 Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Wed, 2 Oct 2024 18:22:08 +0500 Subject: [PATCH 13/16] Removed few whitespaces --- .../thealgorithms/datastructures/lists/SortedLinkedList.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java b/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java index 0f084ca0e87a..57bf9d1ff3ff 100644 --- a/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java +++ b/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java @@ -84,7 +84,7 @@ public boolean delete(int value) { } temp = temp.next; } - return false; + return false; } } @@ -160,6 +160,5 @@ public Node(int value) { this.value = value; this.next = null; } - } } From 16cdb6012253588f1d67bb395c8ac0079e77bd04 Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Thu, 3 Oct 2024 05:45:44 +0500 Subject: [PATCH 14/16] Addressed comments by alxkm --- .../lists/SortedLinkedList.java | 27 +++---------------- .../lists/SortedLinkedListTest.java | 19 ------------- 2 files changed, 4 insertions(+), 42 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java b/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java index 57bf9d1ff3ff..81f12d83bb10 100644 --- a/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java +++ b/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java @@ -1,6 +1,7 @@ package com.thealgorithms.datastructures.lists; import java.util.ArrayList; +import java.util.List; /** * A SortedLinkedList is a data structure that maintains a sorted list of elements. @@ -8,10 +9,7 @@ * This implementation uses a singly linked list to store the elements. * Further details can be found on this link * https://runestone.academy/ns/books/published/cppds/LinearLinked/ImplementinganOrderedList.html - * @author Muhammad Junaid Khalid - * @param int the type of elements in this list */ - public class SortedLinkedList { private Node head; private Node tail; @@ -111,23 +109,6 @@ public boolean search(int value) { public boolean isEmpty() { return head == null; } - - /** - * Returns the minimum value in the sorted linked list. - * @return the minimum value - */ - public int minValue() { - return this.head.value; - } - - /** - * Returns the maximum value in the sorted linked list. - * @return the maximum value - */ - public int maxValue() { - return this.tail.value; - } - /** * Returns a string representation of the sorted linked list. * @return a string representation of the sorted linked list @@ -135,7 +116,7 @@ public int maxValue() { @Override public String toString() { if (this.head != null) { - ArrayList elements = new ArrayList<>(); + List elements = new ArrayList<>(); Node temp = this.head; while (temp != null) { elements.add(String.valueOf(temp.value)); @@ -147,8 +128,8 @@ public String toString() { } } - public class Node { - public int value; + public final class Node { + public final int value; public Node next; public Node() { diff --git a/src/test/java/com/thealgorithms/datastructures/lists/SortedLinkedListTest.java b/src/test/java/com/thealgorithms/datastructures/lists/SortedLinkedListTest.java index 8d130a9731c0..7bc5006a9433 100644 --- a/src/test/java/com/thealgorithms/datastructures/lists/SortedLinkedListTest.java +++ b/src/test/java/com/thealgorithms/datastructures/lists/SortedLinkedListTest.java @@ -37,25 +37,6 @@ public void testSearch() { assertTrue(list.search(5)); assertFalse(list.search(10)); } - - @Test - public void testMinValue() { - SortedLinkedList list = new SortedLinkedList(); - list.insert(5); - list.insert(3); - list.insert(7); - assertEquals(3, list.minValue()); - } - - @Test - public void testMaxValue() { - SortedLinkedList list = new SortedLinkedList(); - list.insert(5); - list.insert(3); - list.insert(7); - assertEquals(7, list.maxValue()); - } - @Test public void testEmptyList() { SortedLinkedList list = new SortedLinkedList(); From 90c97828f81d6b712dee67f2a73a593c4b7ace96 Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Thu, 3 Oct 2024 10:29:59 +0500 Subject: [PATCH 15/16] "Updated toString method to include brackets and removed default Node constructor." --- .../datastructures/lists/SortedLinkedList.java | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java b/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java index 81f12d83bb10..4cf782679b7c 100644 --- a/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java +++ b/src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java @@ -122,9 +122,10 @@ public String toString() { elements.add(String.valueOf(temp.value)); temp = temp.next; } - return String.join(", ", elements); + return "[" + String.join(", ", elements) + "]"; + } else { - return ""; + return "[]"; } } @@ -132,11 +133,6 @@ public final class Node { public final int value; public Node next; - public Node() { - this.value = 0; - this.next = null; - } - public Node(int value) { this.value = value; this.next = null; From ebc8d33fe7796c0cef3c04ccf5d401362d6de8fd Mon Sep 17 00:00:00 2001 From: mjk22071998 Date: Thu, 3 Oct 2024 10:34:33 +0500 Subject: [PATCH 16/16] tests updated --- .../datastructures/lists/SortedLinkedListTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/com/thealgorithms/datastructures/lists/SortedLinkedListTest.java b/src/test/java/com/thealgorithms/datastructures/lists/SortedLinkedListTest.java index 7bc5006a9433..4877e6db4ec4 100644 --- a/src/test/java/com/thealgorithms/datastructures/lists/SortedLinkedListTest.java +++ b/src/test/java/com/thealgorithms/datastructures/lists/SortedLinkedListTest.java @@ -14,7 +14,7 @@ public void testInsert() { list.insert(5); list.insert(3); list.insert(7); - assertEquals("3, 5, 7", list.toString()); + assertEquals("[3, 5, 7]", list.toString()); } @Test @@ -24,7 +24,7 @@ public void testDelete() { list.insert(3); list.insert(7); assertTrue(list.delete(5)); - assertEquals("3, 7", list.toString()); + assertEquals("[3, 7]", list.toString()); assertFalse(list.delete(10)); } @@ -40,7 +40,7 @@ public void testSearch() { @Test public void testEmptyList() { SortedLinkedList list = new SortedLinkedList(); - assertEquals("", list.toString()); + assertEquals("[]", list.toString()); assertFalse(list.delete(5)); assertFalse(list.search(5)); }