Skip to content

Commit 9b5ced4

Browse files
committed
Sorted Linked List added with Javadoc and tests
1 parent 7f60d57 commit 9b5ced4

File tree

2 files changed

+249
-0
lines changed

2 files changed

+249
-0
lines changed
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
package com.thealgorithms.datastructures.lists;
2+
3+
import java.util.ArrayList;
4+
5+
/**
6+
* A SortedLinkedList is a data structure that maintains a sorted list of elements.
7+
* Elements are ordered based on their natural ordering or by a Comparator provided at the time of creation.
8+
*
9+
* @author Muhammad Junaid Khalid
10+
* @param int the type of elements in this list
11+
*/
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+
*
25+
* @param value the value to be inserted
26+
*/
27+
public void insert(int value){
28+
Node newNode = new Node(value);
29+
if (head == null) {
30+
this.head = newNode;
31+
this.tail = newNode;
32+
}
33+
else if (value < head.value) {
34+
newNode.next = this.head;
35+
this.head = newNode;
36+
}
37+
else if (value > tail.value) {
38+
this.tail.next = newNode;
39+
}
40+
else{
41+
Node temp=head;
42+
while (temp.next != null && temp.next.value < value) {
43+
temp = temp.next;
44+
}
45+
newNode.next = temp.next;
46+
temp.next = newNode;
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+
*
60+
* @param value the value to be deleted
61+
* @return true if the element is found and deleted, false otherwise
62+
*/
63+
public boolean delete(int value){
64+
if (this.head == null) {
65+
return false;
66+
}
67+
else if (this.head.value == value) {
68+
this.head = this.head.next;
69+
return true;
70+
}
71+
else{
72+
Node temp = this.head;
73+
while (temp.next != null) {
74+
if (temp.next.value == value) {
75+
temp.next = temp.next.next;
76+
return true;
77+
}
78+
temp = temp.next;
79+
}
80+
return false;
81+
82+
}
83+
}
84+
85+
/**
86+
* Searches for the specified element in the sorted linked list.
87+
*
88+
* @param value the value to be searched
89+
* @return true if the element is found, false otherwise
90+
*/
91+
public boolean search(int value){
92+
Node temp = this.head;
93+
while (temp != null) {
94+
if (temp.value == value) {
95+
return true;
96+
}
97+
temp = temp.next;
98+
}
99+
return false;
100+
}
101+
102+
/**
103+
* Checks if the sorted linked list is empty.
104+
*
105+
* @return true if the list is empty, false otherwise
106+
*/
107+
public boolean isEmpty() {
108+
return head == null;
109+
}
110+
111+
/**
112+
* Returns the minimum value in the sorted linked list.
113+
*
114+
* @return the minimum value
115+
*/
116+
public int minValue(){
117+
return this.head.value;
118+
}
119+
120+
/**
121+
* Returns the maximum value in the sorted linked list.
122+
*
123+
* @return the maximum value
124+
*/
125+
public int maxValue(){
126+
return this.tail.value;
127+
}
128+
129+
/**
130+
* Returns a string representation of the sorted linked list.
131+
*
132+
* @return a string representation of the sorted linked list
133+
*/
134+
@Override
135+
public String toString() {
136+
ArrayList<String> elements=new ArrayList<>();
137+
Node temp = this.head;
138+
while (temp != null) {
139+
elements.add(String.valueOf(temp.value));
140+
temp = temp.next;
141+
}
142+
return String.join(", ", elements);
143+
}
144+
145+
146+
public class Node {
147+
public int value;
148+
public Node next;
149+
150+
public Node(){
151+
this.value = 0;
152+
this.next= null;
153+
}
154+
155+
public Node(int value){
156+
this.value = value;
157+
this.next = null;
158+
}
159+
160+
}
161+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package com.thealgorithms.datastructures.lists;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertFalse;
5+
import static org.junit.Assert.assertTrue;
6+
7+
import org.junit.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+
41+
@Test
42+
public void testMinValue() {
43+
SortedLinkedList list = new SortedLinkedList();
44+
list.insert(5);
45+
list.insert(3);
46+
list.insert(7);
47+
assertEquals(3, list.minValue());
48+
}
49+
50+
@Test
51+
public void testMaxValue() {
52+
SortedLinkedList list = new SortedLinkedList();
53+
list.insert(5);
54+
list.insert(3);
55+
list.insert(7);
56+
assertEquals(7, list.maxValue());
57+
}
58+
59+
@Test
60+
public void testEmptyList() {
61+
SortedLinkedList list = new SortedLinkedList();
62+
assertEquals("", list.toString());
63+
assertFalse(list.delete(5));
64+
assertFalse(list.search(5));
65+
assertEquals(0, list.minValue());
66+
assertEquals(0, list.maxValue());
67+
}
68+
@Test
69+
public void testIsEmpty_onEmptyList() {
70+
SortedLinkedList list = new SortedLinkedList();
71+
assertTrue(list.isEmpty());
72+
}
73+
74+
@Test
75+
public void testIsEmpty_onNonEmptyList() {
76+
SortedLinkedList list = new SortedLinkedList();
77+
list.insert(10);
78+
assertFalse(list.isEmpty());
79+
}
80+
81+
@Test
82+
public void testIsEmpty_afterDeletion() {
83+
SortedLinkedList list = new SortedLinkedList();
84+
list.insert(10);
85+
list.delete(10);
86+
assertTrue(list.isEmpty());
87+
}
88+
}

0 commit comments

Comments
 (0)