[[’kitten’, 2], [‘dog’, 2], [‘rabbit’, 2]]
Here the second value (an integer) represents item priority. If we enqueue [‘human’, 1] with a priority of 1 (assuming lower priorities are given precedence) it would then be the first item to be dequeued. The collection would like this:
[[‘human’, 1], [’kitten’, 2], [‘dog’, 2], [‘rabbit’, 2]].
We’ve started writing a PriorityQueue in the code editor. You will need to add an enqueue method for adding items with a priority, a dequeue method for removing items, a size method to return the number of items in the queue, a front method to return the element at the front of the queue, and finally an isEmpty method that will return true if the queue is empty or false if it is not.
The enqueue should accept items with the format shown above (['human', 1]) where 1 represents the priority. The dequeue should return only the current item, not its priority.
Queue class should have a enqueue method.
testString: assert((function(){var test = new PriorityQueue(); return (typeof test.enqueue === 'function')}()), 'Your Queue class should have a enqueue method.');
- text: Your Queue class should have a dequeue method.
testString: assert((function(){var test = new PriorityQueue(); return (typeof test.dequeue === 'function')}()), 'Your Queue class should have a dequeue method.');
- text: Your Queue class should have a size method.
testString: assert((function(){var test = new PriorityQueue(); return (typeof test.size === 'function')}()), 'Your Queue class should have a size method.');
- text: Your Queue class should have an isEmpty method.
testString: assert((function(){var test = new PriorityQueue(); return (typeof test.isEmpty === 'function')}()), 'Your Queue class should have an isEmpty method.');
- text: Your PriorityQueue should correctly keep track of the current number of items using the size method as items are enqueued and dequeued.
testString: assert((function(){var test = new PriorityQueue(); test.enqueue(['David Brown', 2]); test.enqueue(['Jon Snow', 1]); var size1 = test.size(); test.dequeue(); var size2 = test.size(); test.enqueue(['A', 3]); test.enqueue(['B', 3]); test.enqueue(['C', 3]); return (size1 === 2 && size2 === 1 && test.size() === 4)}()), 'Your PriorityQueue should correctly keep track of the current number of items using the size method as items are enqueued and dequeued.');
- text: The isEmpty method should return true when the queue is empty.
testString: assert((function(){var test = new PriorityQueue(); test.enqueue(['A', 1]); test.enqueue(['B', 1]); test.dequeue(); var first = test.isEmpty(); test.dequeue(); return (!first && test.isEmpty()); }()), 'The isEmpty method should return true when the queue is empty.');
- text: The priority queue should return items with a higher priority before items with a lower priority and return items in first-in-first-out order otherwise.
testString: assert((function(){var test = new PriorityQueue(); test.enqueue(['A', 5]); test.enqueue(['B', 5]); test.enqueue(['C', 5]); test.enqueue(['D', 3]); test.enqueue(['E', 1]); test.enqueue(['F', 7]); var result = []; result.push(test.dequeue()); result.push(test.dequeue()); result.push(test.dequeue()); result.push(test.dequeue()); result.push(test.dequeue()); result.push(test.dequeue()); return result.join('') === 'EDABCF';}()), 'The priority queue should return items with a higher priority before items with a lower priority and return items in first-in-first-out order otherwise.');
```