Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions src/main/java/algorithms/CircularLinkedList.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public void enqueue(int value) {
} else {
this.first = Optional.of(node);
this.last = Optional.of(node);
node.setNext(node);
}
// END STRIP
}
Expand All @@ -78,14 +79,35 @@ public int remove(int index) {
// STUDENT return -1;
// BEGIN STRIP
if (index >= this.size) {

return -1;
}

int value;
this.size -= 1;
if (index == 0) {
int value = this.first.get().value;
value = this.first.get().value;
Node newFirst = this.first.get().next.get();
this.first = Optional.of(newFirst);
this.last.get().setNext(newFirst);
} else {
Node current = this.first.get();
for (int i = 0; i < index - 1; i++) {
current = current.next.get();
}
value = current.next.get().value;
Node nextNode = current.next.get().next.get();
current.setNext(nextNode);
if (index == this.size) {
this.last = Optional.of(current);
}
}

if (this.size == 0) {
this.first = Optional.empty();
this.last = Optional.empty();
}
Node first = this.first.get();

return 0;
return value;
// END STRIP
}
}
111 changes: 105 additions & 6 deletions src/test/java/algorithms/CircularLinkedListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,32 @@

@Grade
public class CircularLinkedListTest {

// BEGIN STRIP
private void assertLinkedListState(CircularLinkedList list, int[] expectedValues) {
assertEquals(expectedValues.length, list.size);
if (expectedValues.length == 0) {
assertFalse(list.getFirst().isPresent());
assertFalse(list.getLast().isPresent());
return;
}

Optional<CircularLinkedList.Node> currentOpt = list.getFirst();
for (int expectedValue : expectedValues) {
assertTrue(currentOpt.isPresent());
CircularLinkedList.Node current = currentOpt.get();
assertEquals(expectedValue, current.value);
currentOpt = current.next;
}

// Check circularity
if (list.getLast().isPresent()) {
CircularLinkedList.Node lastNode = list.getLast().get();
assertTrue(lastNode.next.isPresent());
assertEquals(list.getFirst().get(), lastNode.next.get());
}
}
// END STRIP

@Test
@Grade(value = 1)
Expand Down Expand Up @@ -36,11 +62,84 @@ public void testSimple() {

assertEquals(3, list.size);
int[] array = new int[]{0, 1, 2};
CircularLinkedList.Node current = list.first.get();
for (int i = 0; i < array.length; i++) {
assertEquals(array[i], current.value);
current = current.next.get();
}
// STUDENT CircularLinkedList.Node current = list.first.get();
// STUDENT for (int i = 0; i < array.length; i++) {
// STUDENT assertEquals(array[i], current.value);
// STUDENT current = current.next.get();
// STUDENT }
// BEGIN STRIP
assertLinkedListState(list, array);
// END STRIP
}

// BEGIN STRIP
@Test
@Grade(value = 1)
public void testLength0() {
CircularLinkedList list = new CircularLinkedList();
assertTrue(list.isEmpty());
int[] array = new int[]{};
assertLinkedListState(list, array);
}

@Test
@Grade(value = 1)
public void testLength1() {
CircularLinkedList list = new CircularLinkedList();
list.enqueue(0);
assertFalse(list.isEmpty());
int[] array = new int[]{0};
assertLinkedListState(list, array);
}

@Test
@Grade(value = 1)
public void testRemove() {
CircularLinkedList list = new CircularLinkedList();
list.enqueue(0);
list.enqueue(1);
list.enqueue(2);
list.enqueue(3);

assertEquals(4, list.size);
int[] array = new int[]{0, 1, 2, 3};
assertLinkedListState(list, array);

int result = list.remove(2);
assertEquals(2, result);
array = new int[]{0, 1, 3};
assertLinkedListState(list, array);

result = list.remove(2);
assertEquals(3, result);
array = new int[]{0, 1};
assertLinkedListState(list, array);
}

@Test
@Grade(value = 1)
public void testRemoveToEmpty() {
CircularLinkedList list = new CircularLinkedList();
list.enqueue(0);

assertEquals(1, list.size);
int[] array = new int[]{0};
assertLinkedListState(list, array);

int result = list.remove(0);
assertEquals(0, result);
array = new int[]{};
assertLinkedListState(list, array);
}

@Test
@Grade(value = 1)
public void testRemoveOutOfBounds() {
CircularLinkedList list = new CircularLinkedList();

int result = list.remove(0); // Should not crash, should return -1
assertEquals(-1, result);
}
// END STRIP

}
}