Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:linked list add new method #832

Open
lxhyl opened this issue Jan 10, 2022 · 0 comments · May be fixed by #1032
Open

feat:linked list add new method #832

lxhyl opened this issue Jan 10, 2022 · 0 comments · May be fixed by #1032

Comments

@lxhyl
Copy link

lxhyl commented Jan 10, 2022

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)
} 
@lxhyl lxhyl changed the title feat:lined list add new method feat:linked list add new method Jan 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant