Fixed the code as per review comments
This commit is contained in:
parent
112593b415
commit
24d805db1d
|
@ -1,40 +1,37 @@
|
|||
package com.baeldung.list;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class CircularLinkedList {
|
||||
|
||||
Node head = null;
|
||||
Node tail = null;
|
||||
final Logger LOGGER = LoggerFactory.getLogger(CircularLinkedList.class);
|
||||
|
||||
private Node head = null;
|
||||
private Node tail = null;
|
||||
|
||||
public void addNode(int value) {
|
||||
|
||||
Node newNode = new Node(value);
|
||||
|
||||
// If no elements are present, make the newly addNodeed node as head
|
||||
if (head == null) {
|
||||
head = newNode;
|
||||
}
|
||||
// If there are elements already present, the existing tail should point to new node
|
||||
else {
|
||||
} else {
|
||||
tail.nextNode = newNode;
|
||||
}
|
||||
|
||||
// Irrespective of whether or not elements are added, assign the
|
||||
// tail to newNode and the nextNode for tail as head
|
||||
tail = newNode;
|
||||
tail.nextNode = head;
|
||||
}
|
||||
|
||||
public boolean containsNode(int searchValue) {
|
||||
|
||||
// Start traversing from the head
|
||||
Node currentNode = head;
|
||||
|
||||
// If list is empty no need of traversal and can return false
|
||||
if (head == null) {
|
||||
return false;
|
||||
} else {
|
||||
do {
|
||||
// Compares the search value with each node value present in the list
|
||||
if (currentNode.value == searchValue) {
|
||||
return true;
|
||||
}
|
||||
|
@ -46,24 +43,16 @@ public class CircularLinkedList {
|
|||
|
||||
public void deleteNode(int valueToDelete) {
|
||||
|
||||
// Start traversing from the head
|
||||
Node currentNode = head;
|
||||
|
||||
// If list is non empty
|
||||
if (head != null) {
|
||||
// If the node to delete is the head node itself,
|
||||
// update the head as the next node of current head
|
||||
// and the nextNode of tail as new head
|
||||
if (currentNode.value == valueToDelete) {
|
||||
head = head.nextNode;
|
||||
tail.nextNode = head;
|
||||
currentNode = null;
|
||||
} else {
|
||||
do {
|
||||
// Fetch the next node of current node
|
||||
Node nextNode = currentNode.nextNode;
|
||||
// If the value to delete matches the next node's value,
|
||||
// update the next node of current node as the next node of present next node
|
||||
if (nextNode.value == valueToDelete) {
|
||||
currentNode.nextNode = nextNode.nextNode;
|
||||
nextNode = null;
|
||||
|
@ -77,12 +66,11 @@ public class CircularLinkedList {
|
|||
|
||||
public void traverseList() {
|
||||
|
||||
// Start traversing from the head
|
||||
Node currentNode = head;
|
||||
|
||||
if (head != null) {
|
||||
do {
|
||||
System.out.print(currentNode.value + " ");
|
||||
LOGGER.info(currentNode.value + " ");
|
||||
currentNode = currentNode.nextNode;
|
||||
} while (currentNode != head);
|
||||
}
|
||||
|
@ -99,4 +87,4 @@ class Node {
|
|||
this.value = value;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,41 +9,37 @@ public class CircularLinkedListUnitTest {
|
|||
|
||||
@Test
|
||||
public void givenACircularLinkedList_WhenAddingElements_ThenListContainsThoseElements() {
|
||||
|
||||
CircularLinkedList cll = createCircularLinkedList();
|
||||
|
||||
assertTrue(cll.containsNode(8));
|
||||
assertTrue(cll.containsNode(37));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenACircularLinkedList_WhenLookingForNonExistingElement_ThenReturnsFalse() {
|
||||
|
||||
CircularLinkedList cll = createCircularLinkedList();
|
||||
|
||||
assertFalse(cll.containsNode(11));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenACircularLinkedList_WhenDeletingElements_ThenListDoesNotContainThoseElements() {
|
||||
|
||||
CircularLinkedList cll = createCircularLinkedList();
|
||||
|
||||
assertTrue(cll.containsNode(13));
|
||||
cll.deleteNode(13);
|
||||
assertFalse(cll.containsNode(13));
|
||||
|
||||
|
||||
assertTrue(cll.containsNode(1));
|
||||
cll.deleteNode(1);
|
||||
assertFalse(cll.containsNode(1));
|
||||
|
||||
|
||||
assertTrue(cll.containsNode(46));
|
||||
cll.deleteNode(46);
|
||||
assertFalse(cll.containsNode(46));
|
||||
assertFalse(cll.containsNode(46));
|
||||
}
|
||||
|
||||
private CircularLinkedList createCircularLinkedList() {
|
||||
|
||||
CircularLinkedList cll = new CircularLinkedList();
|
||||
|
||||
cll.addNode(13);
|
||||
|
|
Loading…
Reference in New Issue