Skip to content

Commit ea15f2b

Browse files
authored
Make SinglyLinkedList Iterable (TheAlgorithms#4334)
1 parent 80a4435 commit ea15f2b

File tree

2 files changed

+86
-15
lines changed

2 files changed

+86
-15
lines changed

src/main/java/com/thealgorithms/datastructures/lists/SinglyLinkedList.java

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.thealgorithms.datastructures.lists;
22

3+
import java.util.Iterator;
4+
import java.util.NoSuchElementException;
35
import java.util.StringJoiner;
46

57
/**
68
* https://en.wikipedia.org/wiki/Linked_list
79
*/
8-
public class SinglyLinkedList extends Node {
10+
public class SinglyLinkedList implements Iterable<Integer> {
911

1012
/**
1113
* Head refer to the front of the list
@@ -213,10 +215,8 @@ public void setHead(Node head) {
213215
*/
214216
public int count() {
215217
int count = 0;
216-
Node cur = head;
217-
while (cur != null) {
218-
cur = cur.next;
219-
count++;
218+
for (final var element : this) {
219+
++count;
220220
}
221221
return count;
222222
}
@@ -228,24 +228,20 @@ public int count() {
228228
* @return {@code true} if key is present in the list, otherwise
229229
* {@code false}.
230230
*/
231-
public boolean search(int key) {
232-
Node cur = head;
233-
while (cur != null) {
234-
if (cur.value == key) {
231+
public boolean search(final int key) {
232+
for (final var element : this) {
233+
if (element == key) {
235234
return true;
236235
}
237-
cur = cur.next;
238236
}
239237
return false;
240238
}
241239

242240
@Override
243241
public String toString() {
244242
StringJoiner joiner = new StringJoiner("->");
245-
Node cur = head;
246-
while (cur != null) {
247-
joiner.add(cur.value + "");
248-
cur = cur.next;
243+
for (final var element : this) {
244+
joiner.add(element + "");
249245
}
250246
return joiner.toString();
251247
}
@@ -452,6 +448,34 @@ public static void main(String[] arg) {
452448
instance.deleteDuplicates();
453449
instance.print();
454450
}
451+
452+
@Override
453+
public Iterator<Integer> iterator() {
454+
return new SinglyLinkedListIterator();
455+
}
456+
457+
private class SinglyLinkedListIterator implements Iterator<Integer> {
458+
private Node current;
459+
460+
SinglyLinkedListIterator() {
461+
current = head;
462+
}
463+
464+
@Override
465+
public boolean hasNext() {
466+
return current != null;
467+
}
468+
469+
@Override
470+
public Integer next() {
471+
if (!hasNext()) {
472+
throw new NoSuchElementException();
473+
}
474+
final var value = current.value;
475+
current = current.next;
476+
return value;
477+
}
478+
}
455479
}
456480

457481
/**

src/test/java/com/thealgorithms/datastructures/lists/SinglyLinkedListTest.java

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static org.junit.jupiter.api.Assertions.*;
44

55
import java.util.ArrayList;
6+
import java.util.Arrays;
67
import java.util.List;
78
import org.junit.jupiter.api.Test;
89

@@ -207,4 +208,50 @@ void RecursiveReverseListTest() {
207208
i--;
208209
}
209210
}
210-
}
211+
212+
@Test
213+
void readWithEnhancedForLoopTest() {
214+
final var expeced = new ArrayList<Integer>(Arrays.asList(10, 20, 30));
215+
216+
SinglyLinkedList list = new SinglyLinkedList();
217+
for (final var x : expeced) {
218+
list.insert(x);
219+
}
220+
221+
var readElements = new ArrayList<Integer>();
222+
for (final var x : list) {
223+
readElements.add(x);
224+
}
225+
226+
assertEquals(readElements, expeced);
227+
}
228+
229+
@Test
230+
void toStringTest() {
231+
SinglyLinkedList list = new SinglyLinkedList();
232+
list.insert(1);
233+
list.insert(2);
234+
list.insert(3);
235+
assertEquals("1->2->3", list.toString());
236+
}
237+
238+
@Test
239+
void toStringForEmptyListTest() {
240+
SinglyLinkedList list = new SinglyLinkedList();
241+
assertEquals("", list.toString());
242+
}
243+
244+
@Test
245+
void countTest() {
246+
SinglyLinkedList list = new SinglyLinkedList();
247+
list.insert(10);
248+
list.insert(20);
249+
assertEquals(2, list.count());
250+
}
251+
252+
@Test
253+
void countForEmptyListTest() {
254+
SinglyLinkedList list = new SinglyLinkedList();
255+
assertEquals(0, list.count());
256+
}
257+
}

0 commit comments

Comments
 (0)