-
Notifications
You must be signed in to change notification settings - Fork 20.5k
Sorted Linked List Added #5519
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Sorted Linked List Added #5519
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
9b5ced4
Sorted Linked List added with Javadoc and tests
mjk22071998 1863e6b
Merge branch 'TheAlgorithms:master' into master
mjk22071998 e1382c1
"Added comments to SortedLinkedList.java to describe the implementati…
mjk22071998 3ec7627
Upgraded test from junit 4 to junit 5
mjk22071998 6ff74f0
Added space before braces of functions
mjk22071998 dd91a15
Rename SortedlinkedListTest.java to SortedLinkedListTest.java
mjk22071998 2c33f89
made to string null safe
mjk22071998 58390c1
Updated tail
mjk22071998 028727f
"Added assignment of `this.tail` to `newNode` in `SortedLinkedList` c…
mjk22071998 3dce7f0
Remove assertions for minValue and maxValue in empty list test
mjk22071998 9e0cdf3
tried to get link updated
mjk22071998 1c2bb10
"Fixed whitespace and formatting issues in SortedLinkedList.java and …
mjk22071998 25be88e
formatting of test file corrected
mjk22071998 0a5495d
Removed few whitespaces
mjk22071998 5693923
Merge branch 'master' into master
mjk22071998 16cdb60
Addressed comments by alxkm
mjk22071998 90c9782
"Updated toString method to include brackets and removed default Node…
mjk22071998 ebc8d33
tests updated
mjk22071998 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
141 changes: 141 additions & 0 deletions
141
src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
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. | ||
* 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 | ||
*/ | ||
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; | ||
this.tail = newNode; | ||
} else { | ||
Node temp = head; | ||
while (temp.next != null && temp.next.value < value) { | ||
temp = temp.next; | ||
} | ||
newNode.next = temp.next; | ||
temp.next = newNode; | ||
if (newNode.next == null) { | ||
this.tail = 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) { | ||
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; | ||
} | ||
} | ||
|
||
/** | ||
* 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 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) { | ||
List<String> 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 "[]"; | ||
} | ||
} | ||
|
||
public final class Node { | ||
public final int value; | ||
public Node next; | ||
|
||
public Node(int value) { | ||
this.value = value; | ||
this.next = null; | ||
} | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
src/test/java/com/thealgorithms/datastructures/lists/SortedLinkedListTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.thealgorithms.datastructures.lists; | ||
|
||
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.jupiter.api.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 testEmptyList() { | ||
SortedLinkedList list = new SortedLinkedList(); | ||
assertEquals("[]", list.toString()); | ||
assertFalse(list.delete(5)); | ||
assertFalse(list.search(5)); | ||
} | ||
@Test | ||
public void testIsEmptyOnEmptyList() { | ||
SortedLinkedList list = new SortedLinkedList(); | ||
assertTrue(list.isEmpty()); | ||
} | ||
|
||
@Test | ||
public void testIsEmptyOnNonEmptyList() { | ||
SortedLinkedList list = new SortedLinkedList(); | ||
list.insert(10); | ||
assertFalse(list.isEmpty()); | ||
} | ||
|
||
@Test | ||
public void testIsEmptyAfterDeletion() { | ||
SortedLinkedList list = new SortedLinkedList(); | ||
list.insert(10); | ||
list.delete(10); | ||
assertTrue(list.isEmpty()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.