Skip to content

feat:linked list add new method #832

@lxhyl

Description

@lxhyl

traverse linked list is easy but uninteresting. You must add new variable and use while.So why not add a method to return a iterator.Then we can use for of to traverse. eg:

getIterator() {
    const _this = this
    return {
      *[Symbol.iterator]() {
        let currentNode = _this.head
        if (!currentNode) yield node
        while (currentNode) {
          yield currentNode.value
          currentNode = currentNode.next
        }
      }
    }
  }

use

const iter = linkedList.getIterator()
for(const item of iter){
 console.log(item)
} 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions