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:
Keramot UL Islam
2022-02-18 20:43:45 +06:00
committed by GitHub
parent 68ca0ceeef
commit 29a3ab73bc
5 changed files with 206 additions and 10984 deletions

View File

@ -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 }