mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 08:16:50 +08:00
merge: Iterator and log methods added for linked lists (#891)
* iterator, log methods added in SL * iterator, log methods added in DL * test file added for DoublyLL * format issue fix
This commit is contained in:
@ -211,6 +211,25 @@ class LinkedList {
|
||||
|
||||
return list
|
||||
}
|
||||
|
||||
// Method to iterate over the LinkedList
|
||||
iterator () {
|
||||
let currentNode = this.headNode
|
||||
if (currentNode === null) return -1
|
||||
|
||||
const iterate = function * () {
|
||||
while (currentNode) {
|
||||
yield currentNode.data
|
||||
currentNode = currentNode.next
|
||||
}
|
||||
}
|
||||
return iterate()
|
||||
}
|
||||
|
||||
// Method to log the LinkedList
|
||||
log () {
|
||||
console.log(JSON.stringify(this.headNode, null, 2))
|
||||
}
|
||||
}
|
||||
|
||||
export { Node, LinkedList }
|
||||
|
Reference in New Issue
Block a user