Last active
August 3, 2019 20:31
-
-
Save IPRIT/1dcbd35fc5c17a2a4e8a9695dfec1949 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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