From ea15f2bd98c24150b590fac800e386d2f98b203d Mon Sep 17 00:00:00 2001 From: Piotr Idzik <65706193+vil02@users.noreply.github.com> Date: Mon, 28 Aug 2023 09:11:07 +0200 Subject: [PATCH] Make `SinglyLinkedList` `Iterable` (#4334) --- .../lists/SinglyLinkedList.java | 52 ++++++++++++++----- .../lists/SinglyLinkedListTest.java | 49 ++++++++++++++++- 2 files changed, 86 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/lists/SinglyLinkedList.java b/src/main/java/com/thealgorithms/datastructures/lists/SinglyLinkedList.java index f74f74fc..f379e9ec 100644 --- a/src/main/java/com/thealgorithms/datastructures/lists/SinglyLinkedList.java +++ b/src/main/java/com/thealgorithms/datastructures/lists/SinglyLinkedList.java @@ -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 { /** * 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 iterator() { + return new SinglyLinkedListIterator(); + } + + private class SinglyLinkedListIterator implements Iterator { + 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; + } + } } /** diff --git a/src/test/java/com/thealgorithms/datastructures/lists/SinglyLinkedListTest.java b/src/test/java/com/thealgorithms/datastructures/lists/SinglyLinkedListTest.java index e7c20071..bef02e62 100644 --- a/src/test/java/com/thealgorithms/datastructures/lists/SinglyLinkedListTest.java +++ b/src/test/java/com/thealgorithms/datastructures/lists/SinglyLinkedListTest.java @@ -3,6 +3,7 @@ package com.thealgorithms.datastructures.lists; import static org.junit.jupiter.api.Assertions.*; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import org.junit.jupiter.api.Test; @@ -207,4 +208,50 @@ public class SinglyLinkedListTest { i--; } } -} \ No newline at end of file + + @Test + void readWithEnhancedForLoopTest() { + final var expeced = new ArrayList(Arrays.asList(10, 20, 30)); + + SinglyLinkedList list = new SinglyLinkedList(); + for (final var x : expeced) { + list.insert(x); + } + + var readElements = new ArrayList(); + 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()); + } +}