Skip to content

Instantly share code, notes, and snippets.

@Damimd10
Created February 6, 2019 01:16
Show Gist options
  • Save Damimd10/db10b14d65028d9484d435425ddd00e8 to your computer and use it in GitHub Desktop.
Save Damimd10/db10b14d65028d9484d435425ddd00e8 to your computer and use it in GitHub Desktop.
import { createQueue } from './index';
function createPriorityQueue() {
const lowPriorityQueue = createQueue();
const highPriorityQueue = createQueue();
return {
enqueue(item, isHighPriority = false) {
isHighPriority
? highPriorityQueue.enqueue(item)
: lowPriorityQueue.enqueue(item)
},
dequeue() {
if (!highPriorityQueue.isEmpty()) {
return highPriorityQueue.dequeue()
}
return lowPriorityQueue.dequeue()
},
peek() {
if (!highPriorityQueue.isEmpty()) {
return highPriorityQueue.peek()
}
return lowPriorityQueue.peek()
},
length() {
return (highPriorityQueue.length + lowPriorityQueue.length)
},
isEmpty() {
return (highPriorityQueue.isEmpty() && lowPriorityQueue.isEmpty())
}
}
}
const myQueue = createPriorityQueue()
myQueue.enqueue('A fix here')
myQueue.enqueue('A bug there')
myQueue.enqueue('A new feature')
console.log(myQueue.peek()) // A fix here
myQueue.dequeue()
console.log(myQueue.peek()) // A bug there
myQueue.enqueue('Emergency task!', true)
console.log(myQueue.peek()) // Emergency task!
myQueue.dequeue()
console.log(myQueue.peek()) // A bug there
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment