Skip to content

Instantly share code, notes, and snippets.

@IPRIT
Last active August 3, 2019 20:31
Show Gist options
  • Select an option

  • Save IPRIT/1dcbd35fc5c17a2a4e8a9695dfec1949 to your computer and use it in GitHub Desktop.

Select an option

Save IPRIT/1dcbd35fc5c17a2a4e8a9695dfec1949 to your computer and use it in GitHub Desktop.
class ListNode {
constructor (val) {
this.val = val;
this.next = this.prev = null;
}
}
class Queue {
constructor () {
this._head = null;
this._tail = null;
this._cur = null;
}
push (fn) {
const oldTail = this._tail;
const newTail = new ListNode( fn );
this._tail = newTail;
if (oldTail) {
oldTail.prev = newTail;
}
newTail.next = oldTail;
if (!this._head) {
this._head = newTail;
}
if (!this._cur) {
this.next();
}
}
next () {
if (!this._head) {
return ( this._cur = null );
}
const oldHead = this._head;
const newHead = oldHead.prev;
if (newHead) {
newHead.next = null;
} else {
this._tail = null;
}
this._head = newHead;
oldHead.prev = null;
oldHead.val();
this._cur = oldHead;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment