Created
December 6, 2017 08:28
-
-
Save CMCDragonkai/159a99c760a28c2c177f9d1df5167ca4 to your computer and use it in GitHub Desktop.
Doubly Linked List #js
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
// @flow | |
class DLinkedList<T> { | |
_value : T; | |
_prev: DLinkedList<T>|void; | |
_next: DLinkedList<T>|void; | |
constructor (value: T, prev: DLinkedList<T>|void, next: DLinkedList<T>|void) { | |
this._value = value; | |
this._prev = prev; | |
this._next = next; | |
} | |
static fromArray<T> ([value, ...values]: Array<T>): DLinkedList<T> { | |
const first = new DLinkedList(value); | |
let prev = first; | |
for (const value of values) { | |
const current = new DLinkedList(value, prev); | |
prev._next = current; | |
prev = current; | |
} | |
return first; | |
} | |
get () { | |
return this._value; | |
} | |
set (value) { | |
this._value = value; | |
} | |
// setup iteration protocol | |
// setup ability to insert into the middle | |
// and also remove itself | |
// also set the first and last | |
// as well? | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment