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
2 changed files with 86 additions and 15 deletions

View File

@ -1,11 +1,13 @@
package com.thealgorithms.datastructures.lists;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.StringJoiner;
/**
* 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
@ -213,10 +215,8 @@ public class SinglyLinkedList extends Node {
*/
public int count() {
int count = 0;
Node cur = head;
while (cur != null) {
cur = cur.next;
count++;
for (final var element : this) {
++count;
}
return count;
}
@ -228,13 +228,11 @@ public class SinglyLinkedList extends Node {
* @return {@code true} if key is present in the list, otherwise
* {@code false}.
*/
public boolean search(int key) {
Node cur = head;
while (cur != null) {
if (cur.value == key) {
public boolean search(final int key) {
for (final var element : this) {
if (element == key) {
return true;
}
cur = cur.next;
}
return false;
}
@ -242,10 +240,8 @@ public class SinglyLinkedList extends Node {
@Override
public String toString() {
StringJoiner joiner = new StringJoiner("->");
Node cur = head;
while (cur != null) {
joiner.add(cur.value + "");
cur = cur.next;
for (final var element : this) {
joiner.add(element + "");
}
return joiner.toString();
}
@ -452,6 +448,34 @@ public class SinglyLinkedList extends Node {
instance.deleteDuplicates();
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;
}
}
}
/**