-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fa42c8c
commit ec536c6
Showing
6 changed files
with
310 additions
and
73 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package `03-linked-list` | ||
|
||
class LinkedListIterator<T : Any>(private val list: LinkedList<T>) : Iterator<T>, MutableIterator<T> { | ||
private var index = 0 | ||
private var lastNode: Node<T>? = null | ||
|
||
override fun hasNext(): Boolean { | ||
return index < list.size | ||
} | ||
|
||
override fun next(): T { | ||
if (index >= list.size) throw IndexOutOfBoundsException() | ||
|
||
lastNode = if (index == 0) { | ||
list.nodeAt(0) | ||
} else { | ||
lastNode?.next | ||
} | ||
index++ | ||
return lastNode!!.value | ||
} | ||
|
||
override fun remove() { | ||
if (index == 1) { | ||
list.pop() | ||
} else { | ||
val prevNode = list.nodeAt(index - 2) ?: return | ||
|
||
list.removeAfter(prevNode) | ||
lastNode = prevNode | ||
} | ||
index-- | ||
} | ||
} |
Oops, something went wrong.