Skip to content

File tree

3 files changed

+198
-0
lines changed

3 files changed

+198
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package com.baeldung.customlinkedlist;
2+
3+
public class CustomLinkedList<T> {
4+
private Node<T> head;
5+
private Node<T> tail;
6+
private int size;
7+
8+
public CustomLinkedList() {
9+
head = null;
10+
tail = null;
11+
size = 0;
12+
}
13+
14+
public void insertTail(T value) {
15+
if (size == 0) {
16+
head = tail = new Node<>(value);
17+
} else {
18+
tail.next = new Node<>(value);
19+
tail = tail.next;
20+
}
21+
size++;
22+
}
23+
24+
public void insertHead(T value) {
25+
26+
Node<T> newNode = new Node<>(value);
27+
newNode.next = head;
28+
head = newNode;
29+
30+
if (size == 0) {
31+
tail = newNode;
32+
}
33+
34+
size++;
35+
}
36+
37+
public T get(int index) {
38+
if (index < 0 || index >= size) {
39+
throw new IndexOutOfBoundsException("Index out of bounds");
40+
}
41+
42+
Node<T> current = head;
43+
for (int i = 0; i < index; i++) {
44+
current = current.next;
45+
}
46+
47+
return current.value;
48+
}
49+
50+
public void removeHead() {
51+
if (head == null)
52+
return;
53+
head = head.next;
54+
if (head == null) {
55+
tail = null;
56+
}
57+
size--;
58+
}
59+
60+
public void removeTail() {
61+
if (head == null) {
62+
return;
63+
}
64+
65+
if (head.next == null) {
66+
head = null;
67+
tail = null;
68+
} else {
69+
Node<T> current = head;
70+
while (current.next != tail) {
71+
current = current.next;
72+
}
73+
current.next = null;
74+
tail = current;
75+
}
76+
77+
size--;
78+
}
79+
80+
public void removeAtIndex(int index) {
81+
if (index < 0 || index >= size) {
82+
throw new IndexOutOfBoundsException("Index out of bounds");
83+
}
84+
85+
if (index == 0) {
86+
removeHead();
87+
return;
88+
}
89+
90+
Node<T> current = head;
91+
for (int i = 0; i < index - 1; i++) {
92+
current = current.next;
93+
}
94+
95+
if (current.next == tail) {
96+
tail = current;
97+
}
98+
99+
current.next = current.next.next;
100+
size--;
101+
}
102+
103+
public int size() {
104+
return size;
105+
}
106+
107+
@Override
108+
public String toString() {
109+
StringBuilder builder = new StringBuilder();
110+
Node<T> temp = head;
111+
while (temp != null) {
112+
builder.append(temp.value)
113+
.append("->");
114+
if (temp == tail)
115+
builder.append("End");
116+
temp = temp.next;
117+
}
118+
return builder.toString();
119+
}
120+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.customlinkedlist;
2+
3+
public class Node<T> {
4+
5+
T value;
6+
Node<T> next;
7+
8+
public Node(T value) {
9+
this.value = value;
10+
this.next = null;
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.baeldung.customlinkedlist;
2+
3+
import org.junit.Test;
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
6+
public class CustomLinkedListUnitTest {
7+
8+
@Test
9+
public void givenLinkedList_whenBuildingReferenceBetweenNodes_thenCorrect() {
10+
Node<Integer> node0 = new Node<>(1);
11+
Node<Integer> node1 = new Node<>(2);
12+
Node<Integer> node2 = new Node<>(3);
13+
node0.next = node1;
14+
node1.next = node2;
15+
16+
assertEquals(1, node0.value);
17+
assertEquals(2, node0.next.value);
18+
assertEquals(3, node0.next.next.value);
19+
}
20+
21+
@Test
22+
public void givenAnEmptyCustomLinkedList_whenInsertingNodes_thenReturnValueOfSpecifiedIndex() {
23+
CustomLinkedList<String> fruit = new CustomLinkedList<>();
24+
fruit.insertTail("Avocado");
25+
fruit.insertTail("Banana");
26+
fruit.insertTail("Apple");
27+
assertEquals("Apple", fruit.get(2));
28+
}
29+
30+
@Test
31+
public void givenLinkedList_whenGettingElementByIndex_thenCorrect() {
32+
CustomLinkedList<String> fruit = new CustomLinkedList<>();
33+
fruit.insertTail("Avocado");
34+
fruit.insertTail("Banana");
35+
fruit.insertTail("Apple");
36+
assertEquals("Avocado", fruit.get(0));
37+
}
38+
39+
@Test
40+
public void givenEmptyCustomLinkedList_whenInsertingElementAtTheHead_thenReturnCorrectSize() {
41+
CustomLinkedList<String> fruit = new CustomLinkedList<>();
42+
fruit.insertHead("Avocado");
43+
fruit.insertHead("Banana");
44+
fruit.insertHead("Apple");
45+
assertEquals(3, fruit.size());
46+
}
47+
48+
@Test
49+
public void givenCustomLinkedList_whenRetrievingTheSizeOfTheList_thenCorrect() {
50+
CustomLinkedList<String> fruit = new CustomLinkedList<>();
51+
fruit.insertHead("Avocado");
52+
fruit.insertHead("Banana");
53+
fruit.insertHead("Apple");
54+
assertEquals(3, fruit.size());
55+
}
56+
57+
@Test
58+
public void givenCustomLinkedList_whenRemovingANodeBaseOnIndex_thenReturnNewSize() {
59+
CustomLinkedList<String> fruit = new CustomLinkedList<>();
60+
fruit.insertHead("Avocado");
61+
fruit.insertHead("Banana");
62+
fruit.insertHead("Apple");
63+
fruit.removeAtIndex(2);
64+
assertEquals(2, fruit.size());
65+
}
66+
}

0 commit comments

Comments
 (0)