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