Skip to content

Commit 9dc001a

Browse files
committed
BAEL-3453 - Circular linked list Java implementation
1 parent 7a19706 commit 9dc001a

2 files changed

Lines changed: 162 additions & 0 deletions

File tree

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package com.baeldung.list;
2+
3+
public class CircularLinkedList {
4+
5+
Node head = null;
6+
Node tail = null;
7+
8+
public void addNode(int value) {
9+
10+
Node newNode = new Node(value);
11+
12+
// If no elements are present, make the newly addNodeed node as head
13+
if (head == null) {
14+
head = newNode;
15+
}
16+
// If there are elements already present, the existing tail should point to new node
17+
else {
18+
tail.nextNode = newNode;
19+
}
20+
21+
// Irrespective of whether or not elements are addNodeed, assign the
22+
// tail to newNode and the nextNode for tail as head
23+
tail = newNode;
24+
tail.nextNode = head;
25+
}
26+
27+
public boolean containsNode(int searchValue) {
28+
29+
// Start traversing from the head
30+
Node currentNode = head;
31+
32+
// If list is empty no need of traversal and can return false
33+
if (head == null) {
34+
return false;
35+
} else {
36+
do {
37+
// Compares the search value with each node value present in the list
38+
if (currentNode.value == searchValue) {
39+
return true;
40+
}
41+
currentNode = currentNode.nextNode;
42+
} while (currentNode != head);
43+
return false;
44+
}
45+
}
46+
47+
public void deleteNode(int valueToDelete) {
48+
49+
// Start traversing from the head
50+
Node currentNode = head;
51+
52+
// If list is non empty
53+
if (head != null) {
54+
// If the node to delete is the head node itself,
55+
// update the head as the next node of current head
56+
// and the nextNode of tail as new head
57+
if (currentNode.value == valueToDelete) {
58+
head = head.nextNode;
59+
tail.nextNode = head;
60+
currentNode = null;
61+
} else {
62+
do {
63+
// Fetch the next node of current node
64+
Node nextNode = currentNode.nextNode;
65+
// If the value to delete matches the next node's value,
66+
// update the next node of current node as the next node of present next node
67+
if (nextNode.value == valueToDelete) {
68+
currentNode.nextNode = nextNode.nextNode;
69+
nextNode = null;
70+
break;
71+
}
72+
currentNode = currentNode.nextNode;
73+
} while (currentNode != head);
74+
}
75+
}
76+
}
77+
78+
public void traverseList() {
79+
80+
// Start traversing from the head
81+
Node currentNode = head;
82+
83+
if (head != null) {
84+
do {
85+
System.out.print(currentNode.value + " ");
86+
currentNode = currentNode.nextNode;
87+
} while (currentNode != head);
88+
}
89+
}
90+
91+
}
92+
93+
class Node {
94+
95+
int value;
96+
Node nextNode;
97+
98+
public Node(int value) {
99+
this.value = value;
100+
}
101+
102+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.baeldung.list;
2+
3+
import static org.junit.Assert.assertFalse;
4+
import static org.junit.Assert.assertTrue;
5+
6+
import org.junit.Test;
7+
8+
public class CircularLinkedListUnitTest {
9+
10+
@Test
11+
public void givenACircularLinkedList_WhenAddingElements_ThenListContainsThoseElements() {
12+
13+
CircularLinkedList cll = createCircularLinkedList();
14+
15+
assertTrue(cll.containsNode(8));
16+
assertTrue(cll.containsNode(37));
17+
}
18+
19+
@Test
20+
public void givenACircularLinkedList_WhenLookingForNonExistingElement_ThenReturnsFalse() {
21+
22+
CircularLinkedList cll = createCircularLinkedList();
23+
24+
assertFalse(cll.containsNode(11));
25+
}
26+
27+
@Test
28+
public void givenACircularLinkedList_WhenDeletingElements_ThenListDoesNotContainThoseElements() {
29+
30+
CircularLinkedList cll = createCircularLinkedList();
31+
32+
assertTrue(cll.containsNode(13));
33+
cll.deleteNode(13);
34+
assertFalse(cll.containsNode(13));
35+
36+
assertTrue(cll.containsNode(1));
37+
cll.deleteNode(1);
38+
assertFalse(cll.containsNode(1));
39+
40+
assertTrue(cll.containsNode(46));
41+
cll.deleteNode(46);
42+
assertFalse(cll.containsNode(46));
43+
}
44+
45+
private CircularLinkedList createCircularLinkedList() {
46+
47+
CircularLinkedList cll = new CircularLinkedList();
48+
49+
cll.addNode(13);
50+
cll.addNode(7);
51+
cll.addNode(24);
52+
cll.addNode(1);
53+
cll.addNode(8);
54+
cll.addNode(37);
55+
cll.addNode(46);
56+
57+
return cll;
58+
}
59+
60+
}

0 commit comments

Comments
 (0)