Skip to content

Instantly share code, notes, and snippets.

@CMCDragonkai
Created December 6, 2017 08:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CMCDragonkai/159a99c760a28c2c177f9d1df5167ca4 to your computer and use it in GitHub Desktop.
Save CMCDragonkai/159a99c760a28c2c177f9d1df5167ca4 to your computer and use it in GitHub Desktop.
Doubly Linked List #js
// @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