From afe2bd6553fef9f22c1829f27e95663f70b2d22b Mon Sep 17 00:00:00 2001 From: Oshgnacknak Date: Sun, 5 Sep 2021 15:27:17 +0200 Subject: [PATCH] Implement check, create and iterate --- .../prep/list/DoubleListItemProcessor.java | 6 ++ .../RecursiveDoubleListItemProcessor.java | 57 ++++++++++++++++--- 2 files changed, 55 insertions(+), 8 deletions(-) diff --git a/src/aud/exam/prep/list/DoubleListItemProcessor.java b/src/aud/exam/prep/list/DoubleListItemProcessor.java index 67829ce..9283d93 100644 --- a/src/aud/exam/prep/list/DoubleListItemProcessor.java +++ b/src/aud/exam/prep/list/DoubleListItemProcessor.java @@ -3,4 +3,10 @@ package aud.exam.prep.list; import aud.exam.prep.SequenceProcessor; abstract class DoubleListItemProcessor implements SequenceProcessor> { + + @Override + public Iterable iterate(DoubleListItem list) { + return () -> + new ListItemIterator<>(list); + } } \ No newline at end of file diff --git a/src/aud/exam/prep/list/RecursiveDoubleListItemProcessor.java b/src/aud/exam/prep/list/RecursiveDoubleListItemProcessor.java index 5a0a317..c4ad6ab 100644 --- a/src/aud/exam/prep/list/RecursiveDoubleListItemProcessor.java +++ b/src/aud/exam/prep/list/RecursiveDoubleListItemProcessor.java @@ -3,6 +3,8 @@ package aud.exam.prep.list; import aud.exam.prep.Pair; import java.util.Comparator; +import java.util.HashSet; +import java.util.Iterator; public class RecursiveDoubleListItemProcessor extends DoubleListItemProcessor { @@ -176,19 +178,58 @@ public class RecursiveDoubleListItemProcessor extends DoubleListItemProcessor @Override public boolean check(DoubleListItem list) { - // TODO: Your training - throw new RuntimeException("unimplemented"); + return checkRec(list, new HashSet<>(), null); + } + + private boolean checkRec(DoubleListItem list, HashSet> seen, DoubleListItem prev) { + if (list == null) { + if (seen.isEmpty()) { + return true; + } + + if (prev == null) { + return true; + } + + return prev.next == null; + } + + if (list.prev != prev) { + return false; + } + + if (prev != null && prev.next != list) { + return false; + } + + if (list.key == null) { + return false; + } + + if (seen.contains(list)) { + return false; + } + + seen.add(list); + return checkRec(list.next, seen, list); } @Override public DoubleListItem create(Iterable iterable) { - // TODO: Your training - throw new RuntimeException("unimplemented"); + var iter = iterable.iterator(); + return createRec(iter, null); } - @Override - public Iterable iterate(DoubleListItem list) { - // TODO: Your training - throw new RuntimeException("unimplemented"); + private DoubleListItem createRec(Iterator iter, DoubleListItem prev) { + if (!iter.hasNext()) { + return null; + } + + var item = new DoubleListItem(); + item.key = iter.next(); + item.next = createRec(iter, item); + item.prev = prev; + + return item; } } \ No newline at end of file