Make SinglyLinkedList Iterable (#4334)

This commit is contained in:
Piotr Idzik 2023-08-28 09:11:07 +02:00 committed by GitHub
parent 80a4435038
commit ea15f2bd98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 86 additions and 15 deletions

View File

@ -1,11 +1,13 @@
package com.thealgorithms.datastructures.lists; package com.thealgorithms.datastructures.lists;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.StringJoiner; import java.util.StringJoiner;
/** /**
* https://en.wikipedia.org/wiki/Linked_list * https://en.wikipedia.org/wiki/Linked_list
*/ */
public class SinglyLinkedList extends Node { public class SinglyLinkedList implements Iterable<Integer> {
/** /**
* Head refer to the front of the list * Head refer to the front of the list
@ -213,10 +215,8 @@ public class SinglyLinkedList extends Node {
*/ */
public int count() { public int count() {
int count = 0; int count = 0;
Node cur = head; for (final var element : this) {
while (cur != null) { ++count;
cur = cur.next;
count++;
} }
return count; return count;
} }
@ -228,13 +228,11 @@ public class SinglyLinkedList extends Node {
* @return {@code true} if key is present in the list, otherwise * @return {@code true} if key is present in the list, otherwise
* {@code false}. * {@code false}.
*/ */
public boolean search(int key) { public boolean search(final int key) {
Node cur = head; for (final var element : this) {
while (cur != null) { if (element == key) {
if (cur.value == key) {
return true; return true;
} }
cur = cur.next;
} }
return false; return false;
} }
@ -242,10 +240,8 @@ public class SinglyLinkedList extends Node {
@Override @Override
public String toString() { public String toString() {
StringJoiner joiner = new StringJoiner("->"); StringJoiner joiner = new StringJoiner("->");
Node cur = head; for (final var element : this) {
while (cur != null) { joiner.add(element + "");
joiner.add(cur.value + "");
cur = cur.next;
} }
return joiner.toString(); return joiner.toString();
} }
@ -452,6 +448,34 @@ public class SinglyLinkedList extends Node {
instance.deleteDuplicates(); instance.deleteDuplicates();
instance.print(); instance.print();
} }
@Override
public Iterator<Integer> iterator() {
return new SinglyLinkedListIterator();
}
private class SinglyLinkedListIterator implements Iterator<Integer> {
private Node current;
SinglyLinkedListIterator() {
current = head;
}
@Override
public boolean hasNext() {
return current != null;
}
@Override
public Integer next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
final var value = current.value;
current = current.next;
return value;
}
}
} }
/** /**

View File

@ -3,6 +3,7 @@ package com.thealgorithms.datastructures.lists;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -207,4 +208,50 @@ public class SinglyLinkedListTest {
i--; i--;
} }
} }
@Test
void readWithEnhancedForLoopTest() {
final var expeced = new ArrayList<Integer>(Arrays.asList(10, 20, 30));
SinglyLinkedList list = new SinglyLinkedList();
for (final var x : expeced) {
list.insert(x);
}
var readElements = new ArrayList<Integer>();
for (final var x : list) {
readElements.add(x);
}
assertEquals(readElements, expeced);
}
@Test
void toStringTest() {
SinglyLinkedList list = new SinglyLinkedList();
list.insert(1);
list.insert(2);
list.insert(3);
assertEquals("1->2->3", list.toString());
}
@Test
void toStringForEmptyListTest() {
SinglyLinkedList list = new SinglyLinkedList();
assertEquals("", list.toString());
}
@Test
void countTest() {
SinglyLinkedList list = new SinglyLinkedList();
list.insert(10);
list.insert(20);
assertEquals(2, list.count());
}
@Test
void countForEmptyListTest() {
SinglyLinkedList list = new SinglyLinkedList();
assertEquals(0, list.count());
}
} }