Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 9d545c2

Browse filesBrowse files
authored
Merge pull request eugenp#8231 from radhe-sravan/master
BAEL-3453 - Circular linked list Java implementation
2 parents 2b9a239 + 399aaad commit 9d545c2
Copy full SHA for 9d545c2

File tree

Expand file treeCollapse file tree

2 files changed

+146
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+146
-0
lines changed
Open diff view settings
Collapse file
+90Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.baeldung.list;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
public class CircularLinkedList {
7+
8+
final Logger LOGGER = LoggerFactory.getLogger(CircularLinkedList.class);
9+
10+
private Node head = null;
11+
private Node tail = null;
12+
13+
public void addNode(int value) {
14+
15+
Node newNode = new Node(value);
16+
17+
if (head == null) {
18+
head = newNode;
19+
} else {
20+
tail.nextNode = newNode;
21+
}
22+
23+
tail = newNode;
24+
tail.nextNode = head;
25+
}
26+
27+
public boolean containsNode(int searchValue) {
28+
29+
Node currentNode = head;
30+
31+
if (head == null) {
32+
return false;
33+
} else {
34+
do {
35+
if (currentNode.value == searchValue) {
36+
return true;
37+
}
38+
currentNode = currentNode.nextNode;
39+
} while (currentNode != head);
40+
return false;
41+
}
42+
}
43+
44+
public void deleteNode(int valueToDelete) {
45+
46+
Node currentNode = head;
47+
48+
if (head != null) {
49+
if (currentNode.value == valueToDelete) {
50+
head = head.nextNode;
51+
tail.nextNode = head;
52+
currentNode = null;
53+
} else {
54+
do {
55+
Node nextNode = currentNode.nextNode;
56+
if (nextNode.value == valueToDelete) {
57+
currentNode.nextNode = nextNode.nextNode;
58+
nextNode = null;
59+
break;
60+
}
61+
currentNode = currentNode.nextNode;
62+
} while (currentNode != head);
63+
}
64+
}
65+
}
66+
67+
public void traverseList() {
68+
69+
Node currentNode = head;
70+
71+
if (head != null) {
72+
do {
73+
LOGGER.info(currentNode.value + " ");
74+
currentNode = currentNode.nextNode;
75+
} while (currentNode != head);
76+
}
77+
}
78+
79+
}
80+
81+
class Node {
82+
83+
int value;
84+
Node nextNode;
85+
86+
public Node(int value) {
87+
this.value = value;
88+
}
89+
90+
}
Collapse file
+56Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
CircularLinkedList cll = createCircularLinkedList();
13+
14+
assertTrue(cll.containsNode(8));
15+
assertTrue(cll.containsNode(37));
16+
}
17+
18+
@Test
19+
public void givenACircularLinkedList_WhenLookingForNonExistingElement_ThenReturnsFalse() {
20+
CircularLinkedList cll = createCircularLinkedList();
21+
22+
assertFalse(cll.containsNode(11));
23+
}
24+
25+
@Test
26+
public void givenACircularLinkedList_WhenDeletingElements_ThenListDoesNotContainThoseElements() {
27+
CircularLinkedList cll = createCircularLinkedList();
28+
29+
assertTrue(cll.containsNode(13));
30+
cll.deleteNode(13);
31+
assertFalse(cll.containsNode(13));
32+
33+
assertTrue(cll.containsNode(1));
34+
cll.deleteNode(1);
35+
assertFalse(cll.containsNode(1));
36+
37+
assertTrue(cll.containsNode(46));
38+
cll.deleteNode(46);
39+
assertFalse(cll.containsNode(46));
40+
}
41+
42+
private CircularLinkedList createCircularLinkedList() {
43+
CircularLinkedList cll = new CircularLinkedList();
44+
45+
cll.addNode(13);
46+
cll.addNode(7);
47+
cll.addNode(24);
48+
cll.addNode(1);
49+
cll.addNode(8);
50+
cll.addNode(37);
51+
cll.addNode(46);
52+
53+
return cll;
54+
}
55+
56+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.