Skip to content

Commit d436910

Browse files
authored
Sorted Linked List Added (#5519)
* Sorted Linked List added with Javadoc and tests * "Added comments to SortedLinkedList.java to describe the implementation and provide a reference link." * Upgraded test from junit 4 to junit 5 * Added space before braces of functions * Rename SortedlinkedListTest.java to SortedLinkedListTest.java * made to string null safe * Updated tail * "Added assignment of `this.tail` to `newNode` in `SortedLinkedList` class." * Remove assertions for minValue and maxValue in empty list test * tried to get link updated * "Fixed whitespace and formatting issues in SortedLinkedList.java and SortedLinkedListTest.java" * formatting of test file corrected * Removed few whitespaces * Addressed comments by alxkm * "Updated toString method to include brackets and removed default Node constructor." * tests updated
1 parent 013d122 commit d436910

File tree

2 files changed

+208
-0
lines changed

2 files changed

+208
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
package com.thealgorithms.datastructures.lists;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* A SortedLinkedList is a data structure that maintains a sorted list of elements.
8+
* Elements are ordered based on their natural ordering or by a Comparator provided at the time of creation.
9+
* This implementation uses a singly linked list to store the elements.
10+
* Further details can be found on this link
11+
* https://runestone.academy/ns/books/published/cppds/LinearLinked/ImplementinganOrderedList.html
12+
*/
13+
public class SortedLinkedList {
14+
private Node head;
15+
private Node tail;
16+
17+
public SortedLinkedList() {
18+
this.head = null;
19+
this.tail = null;
20+
}
21+
22+
/**
23+
* Inserts a new element into the sorted linked list.
24+
* @param value the value to be inserted
25+
*/
26+
public void insert(int value) {
27+
Node newNode = new Node(value);
28+
if (head == null) {
29+
this.head = newNode;
30+
this.tail = newNode;
31+
} else if (value < head.value) {
32+
newNode.next = this.head;
33+
this.head = newNode;
34+
} else if (value > tail.value) {
35+
this.tail.next = newNode;
36+
this.tail = newNode;
37+
} else {
38+
Node temp = head;
39+
while (temp.next != null && temp.next.value < value) {
40+
temp = temp.next;
41+
}
42+
newNode.next = temp.next;
43+
temp.next = newNode;
44+
if (newNode.next == null) {
45+
this.tail = newNode;
46+
}
47+
}
48+
}
49+
50+
/**
51+
* Displays the elements of the sorted linked list.
52+
*/
53+
public void display() {
54+
System.out.println(this.toString());
55+
}
56+
57+
/**
58+
* Deletes the first occurrence of the specified element in the sorted linked list.
59+
* @param value the value to be deleted
60+
* @return true if the element is found and deleted, false otherwise
61+
*/
62+
public boolean delete(int value) {
63+
if (this.head == null) {
64+
return false;
65+
} else if (this.head.value == value) {
66+
if (this.head.next == null) {
67+
this.head = null;
68+
this.tail = null;
69+
} else {
70+
this.head = this.head.next;
71+
}
72+
return true;
73+
} else {
74+
Node temp = this.head;
75+
while (temp.next != null) {
76+
if (temp.next.value == value) {
77+
if (temp.next == this.tail) {
78+
this.tail = temp;
79+
}
80+
temp.next = temp.next.next;
81+
return true;
82+
}
83+
temp = temp.next;
84+
}
85+
return false;
86+
}
87+
}
88+
89+
/**
90+
* Searches for the specified element in the sorted linked list.
91+
* @param value the value to be searched
92+
* @return true if the element is found, false otherwise
93+
*/
94+
public boolean search(int value) {
95+
Node temp = this.head;
96+
while (temp != null) {
97+
if (temp.value == value) {
98+
return true;
99+
}
100+
temp = temp.next;
101+
}
102+
return false;
103+
}
104+
105+
/**
106+
* Checks if the sorted linked list is empty.
107+
* @return true if the list is empty, false otherwise
108+
*/
109+
public boolean isEmpty() {
110+
return head == null;
111+
}
112+
/**
113+
* Returns a string representation of the sorted linked list.
114+
* @return a string representation of the sorted linked list
115+
*/
116+
@Override
117+
public String toString() {
118+
if (this.head != null) {
119+
List<String> elements = new ArrayList<>();
120+
Node temp = this.head;
121+
while (temp != null) {
122+
elements.add(String.valueOf(temp.value));
123+
temp = temp.next;
124+
}
125+
return "[" + String.join(", ", elements) + "]";
126+
127+
} else {
128+
return "[]";
129+
}
130+
}
131+
132+
public final class Node {
133+
public final int value;
134+
public Node next;
135+
136+
public Node(int value) {
137+
this.value = value;
138+
this.next = null;
139+
}
140+
}
141+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.thealgorithms.datastructures.lists;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
7+
import org.junit.jupiter.api.Test;
8+
9+
public class SortedLinkedListTest {
10+
11+
@Test
12+
public void testInsert() {
13+
SortedLinkedList list = new SortedLinkedList();
14+
list.insert(5);
15+
list.insert(3);
16+
list.insert(7);
17+
assertEquals("[3, 5, 7]", list.toString());
18+
}
19+
20+
@Test
21+
public void testDelete() {
22+
SortedLinkedList list = new SortedLinkedList();
23+
list.insert(5);
24+
list.insert(3);
25+
list.insert(7);
26+
assertTrue(list.delete(5));
27+
assertEquals("[3, 7]", list.toString());
28+
assertFalse(list.delete(10));
29+
}
30+
31+
@Test
32+
public void testSearch() {
33+
SortedLinkedList list = new SortedLinkedList();
34+
list.insert(5);
35+
list.insert(3);
36+
list.insert(7);
37+
assertTrue(list.search(5));
38+
assertFalse(list.search(10));
39+
}
40+
@Test
41+
public void testEmptyList() {
42+
SortedLinkedList list = new SortedLinkedList();
43+
assertEquals("[]", list.toString());
44+
assertFalse(list.delete(5));
45+
assertFalse(list.search(5));
46+
}
47+
@Test
48+
public void testIsEmptyOnEmptyList() {
49+
SortedLinkedList list = new SortedLinkedList();
50+
assertTrue(list.isEmpty());
51+
}
52+
53+
@Test
54+
public void testIsEmptyOnNonEmptyList() {
55+
SortedLinkedList list = new SortedLinkedList();
56+
list.insert(10);
57+
assertFalse(list.isEmpty());
58+
}
59+
60+
@Test
61+
public void testIsEmptyAfterDeletion() {
62+
SortedLinkedList list = new SortedLinkedList();
63+
list.insert(10);
64+
list.delete(10);
65+
assertTrue(list.isEmpty());
66+
}
67+
}

0 commit comments

Comments
 (0)