Merge pull request #10522 from sk1418/Circular_linked_list
fix the deleting method
This commit is contained in:
commit
1c32920b76
|
@ -5,7 +5,7 @@ import org.slf4j.LoggerFactory;
|
|||
|
||||
public class CircularLinkedList {
|
||||
|
||||
final Logger LOGGER = LoggerFactory.getLogger(CircularLinkedList.class);
|
||||
final Logger logger = LoggerFactory.getLogger(CircularLinkedList.class);
|
||||
|
||||
private Node head = null;
|
||||
private Node tail = null;
|
||||
|
@ -42,24 +42,29 @@ public class CircularLinkedList {
|
|||
}
|
||||
|
||||
public void deleteNode(int valueToDelete) {
|
||||
|
||||
Node currentNode = head;
|
||||
|
||||
if (head != null) {
|
||||
if (currentNode.value == valueToDelete) {
|
||||
head = head.nextNode;
|
||||
tail.nextNode = head;
|
||||
} else {
|
||||
do {
|
||||
Node nextNode = currentNode.nextNode;
|
||||
if (nextNode.value == valueToDelete) {
|
||||
currentNode.nextNode = nextNode.nextNode;
|
||||
break;
|
||||
}
|
||||
currentNode = currentNode.nextNode;
|
||||
} while (currentNode != head);
|
||||
}
|
||||
if (head == null) {
|
||||
return;
|
||||
}
|
||||
do {
|
||||
Node nextNode = currentNode.nextNode;
|
||||
if (nextNode.value == valueToDelete) {
|
||||
if (tail == head) {
|
||||
head = null;
|
||||
tail = null;
|
||||
} else {
|
||||
currentNode.nextNode = nextNode.nextNode;
|
||||
if (head == nextNode) {
|
||||
head = head.nextNode;
|
||||
}
|
||||
if (tail == nextNode) {
|
||||
tail = currentNode;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
currentNode = nextNode;
|
||||
} while (currentNode != head);
|
||||
}
|
||||
|
||||
public void traverseList() {
|
||||
|
@ -68,7 +73,7 @@ public class CircularLinkedList {
|
|||
|
||||
if (head != null) {
|
||||
do {
|
||||
LOGGER.info(currentNode.value + " ");
|
||||
logger.info(currentNode.value + " ");
|
||||
currentNode = currentNode.nextNode;
|
||||
} while (currentNode != head);
|
||||
}
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
package com.baeldung.circularlinkedlist;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class CircularLinkedListUnitTest {
|
||||
|
||||
@Test
|
||||
|
@ -23,7 +23,7 @@ public class CircularLinkedListUnitTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void givenACircularLinkedList_WhenDeletingElements_ThenListDoesNotContainThoseElements() {
|
||||
public void givenACircularLinkedList_WhenDeletingInOrderHeadMiddleTail_ThenListDoesNotContainThoseElements() {
|
||||
CircularLinkedList cll = createCircularLinkedList();
|
||||
|
||||
assertTrue(cll.containsNode(13));
|
||||
|
@ -39,6 +39,32 @@ public class CircularLinkedListUnitTest {
|
|||
assertFalse(cll.containsNode(46));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenACircularLinkedList_WhenDeletingInOrderTailMiddleHead_ThenListDoesNotContainThoseElements() {
|
||||
CircularLinkedList cll = createCircularLinkedList();
|
||||
|
||||
assertTrue(cll.containsNode(46));
|
||||
cll.deleteNode(46);
|
||||
assertFalse(cll.containsNode(46));
|
||||
|
||||
assertTrue(cll.containsNode(1));
|
||||
cll.deleteNode(1);
|
||||
assertFalse(cll.containsNode(1));
|
||||
|
||||
assertTrue(cll.containsNode(13));
|
||||
cll.deleteNode(13);
|
||||
assertFalse(cll.containsNode(13));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenACircularLinkedListWithOneNode_WhenDeletingElement_ThenListDoesNotContainTheElement() {
|
||||
CircularLinkedList cll = new CircularLinkedList();
|
||||
cll.addNode(1);
|
||||
cll.deleteNode(1);
|
||||
assertFalse(cll.containsNode(1));
|
||||
}
|
||||
|
||||
|
||||
private CircularLinkedList createCircularLinkedList() {
|
||||
CircularLinkedList cll = new CircularLinkedList();
|
||||
|
||||
|
|
Loading…
Reference in New Issue