Skip to content

Commit f82bf1a

Browse files
committed
Add singly linked list
1 parent 9086252 commit f82bf1a

File tree

2 files changed

+162
-0
lines changed

2 files changed

+162
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package com.dataStructures;
2+
3+
4+
5+
6+
public class SinglyLinkedList<T> {
7+
8+
/**
9+
* Define a node in the singly linked list
10+
*/
11+
private static class Node<T> {
12+
T data;
13+
Node<T> next;
14+
15+
Node(T data) {
16+
this.data = data;
17+
}
18+
}
19+
20+
/**
21+
* Reference to the first node of the singly linked list
22+
*/
23+
private Node<T> head;
24+
25+
/**
26+
* Method to return the size of the singly linked list
27+
*/
28+
public int size() {
29+
int size = 0;
30+
Node<T> node = head;
31+
while (node != null) {
32+
++size;
33+
node = node.next;
34+
}
35+
return size;
36+
}
37+
38+
/**
39+
* Method to add a node at the end of the singly linked list
40+
*/
41+
public void add(T data) {
42+
Node<T> newNode = new Node<>(data);
43+
if (head == null) {
44+
head = newNode;
45+
} else {
46+
Node<T> last = head;
47+
while (last.next != null) {
48+
last = last.next;
49+
}
50+
last.next = newNode;
51+
}
52+
}
53+
54+
/**
55+
* Method to add a node at the beginning of the singly linked list
56+
*/
57+
public void insertAtStart (T data) {
58+
Node<T> node = new Node<>(data);
59+
node.data = data;
60+
node.next = null;
61+
node.next = head;
62+
head = node;
63+
64+
}
65+
66+
/**
67+
* Method to remove a node at any position in the singly linked list
68+
*/
69+
public void remove(int position) {
70+
Node<T> prev = null, node = head;
71+
while (position > 0 && node != null) {
72+
--position;
73+
prev = node;
74+
node = node.next;
75+
}
76+
if (node != null) {
77+
if (prev == null) {
78+
head = node.next;
79+
} else {
80+
prev.next = node.next;
81+
}
82+
}
83+
}
84+
85+
/**
86+
* Method to print the content of the singly linked list for better
87+
* understanding
88+
*/
89+
public void printContent() {
90+
StringBuilder sb = new StringBuilder();
91+
Node<T> node = head;
92+
while (node != null) {
93+
sb.append(node.data).append('-').append('>');
94+
node = node.next;
95+
}
96+
int lastComma = sb.lastIndexOf(",");
97+
if (lastComma != -1) {
98+
sb.deleteCharAt(lastComma);
99+
}
100+
System.out.println(sb);
101+
}
102+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.dataStructures;
2+
3+
import org.junit.Test;
4+
import org.junit.jupiter.api.Assertions;
5+
6+
7+
public class SinglyLinkedListTest {
8+
9+
private SinglyLinkedList<Integer> MySinglyLinkedList = new SinglyLinkedList<>();
10+
11+
@Test
12+
public void emptyTest() {
13+
Assertions.assertEquals(0, MySinglyLinkedList.size());
14+
MySinglyLinkedList.printContent();
15+
}
16+
17+
@Test
18+
public void addTest() {
19+
MySinglyLinkedList.add(1);
20+
Assertions.assertEquals(1, MySinglyLinkedList.size());
21+
MySinglyLinkedList.printContent();
22+
23+
MySinglyLinkedList.add(42);
24+
Assertions.assertEquals(2, MySinglyLinkedList.size());
25+
MySinglyLinkedList.printContent();
26+
27+
MySinglyLinkedList.add(2);
28+
MySinglyLinkedList.add(3);
29+
Assertions.assertEquals(4, MySinglyLinkedList.size());
30+
MySinglyLinkedList.printContent();
31+
}
32+
33+
@Test
34+
public void removeTest() {
35+
MySinglyLinkedList.add(1);
36+
MySinglyLinkedList.add(2);
37+
MySinglyLinkedList.add(3);
38+
MySinglyLinkedList.add(4);
39+
MySinglyLinkedList.printContent();
40+
41+
MySinglyLinkedList.remove(3);
42+
Assertions.assertEquals(3, MySinglyLinkedList.size());
43+
MySinglyLinkedList.printContent();
44+
45+
MySinglyLinkedList.remove(0);
46+
Assertions.assertEquals(2, MySinglyLinkedList.size());
47+
MySinglyLinkedList.printContent();
48+
49+
MySinglyLinkedList.remove(0);
50+
Assertions.assertEquals(1, MySinglyLinkedList.size());
51+
MySinglyLinkedList.printContent();
52+
53+
MySinglyLinkedList.remove(0);
54+
Assertions.assertEquals(0, MySinglyLinkedList.size());
55+
MySinglyLinkedList.printContent();
56+
}
57+
58+
59+
60+
}

0 commit comments

Comments
 (0)