Skip to content

Commit

Permalink
Fixed the code as per review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
radhe-sravan committed Nov 27, 2019
1 parent 83d8ebe commit 399aaad
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}
Expand All @@ -46,24 +43,16 @@ public boolean containsNode(int searchValue) {

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;
Expand All @@ -77,12 +66,11 @@ public void deleteNode(int valueToDelete) {

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);
}
Expand All @@ -99,4 +87,4 @@ public Node(int value) {
this.value = value;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 399aaad

Please sign in to comment.