Last active
January 29, 2019 04:47
-
-
Save rxluz/c39b91cce3a36a7440500648fa2b46a9 to your computer and use it in GitHub Desktop.
JS Data Structures: Queues, see more at: https://medium.com/p/17f714057817
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
| function PriorityQueue() { | |
| let priority = [] | |
| let nonPriority = [] | |
| class PublicPriorityQueue { | |
| size() { | |
| return priority.length + nonPriority.length | |
| } | |
| isEmpty() { | |
| return this.size() === 0 | |
| } | |
| enqueue(data, isPriority = 0) { | |
| const element = isPriority === 1 ? priority : nonPriority | |
| element.push(data) | |
| } | |
| dequeue() { | |
| const element = priority.length > 0 ? priority : nonPriority | |
| return element.shift() | |
| } | |
| peek() { | |
| const element = priority.length > 0 ? priority : nonPriority | |
| return element[0] | |
| } | |
| print() { | |
| return [...priority, ...nonPriority].toString() | |
| } | |
| } | |
| return new PublicPriorityQueue() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment