Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save rxluz/c39b91cce3a36a7440500648fa2b46a9 to your computer and use it in GitHub Desktop.

Select an option

Save rxluz/c39b91cce3a36a7440500648fa2b46a9 to your computer and use it in GitHub Desktop.
JS Data Structures: Queues, see more at: https://medium.com/p/17f714057817
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