Queues operate similarly to stacks, but the only difference is that queues allow only new data to be added at the back end. This is the same as queuing to buy buns. You buy buns first and then queue up. This is very disciplined. There will be no bad phenomenon of people jumping in the queue.
First of all, we will talk about the common operations of queues:
Queue() // Create an empty queue object enqueue() // Entry operation, placing elements at the end of the queue dequeue() // Out-of-queue operation, removing the elements of the head of the queue and returning front() // Returns the header element and does not operate on the queue isEmpty() // Determine whether the queue is empty size() // Returns the number of elements in the queue clear() // ClearQueue
Using javascript to simulate queue implementation:
function Queue () { let items = [] this.enqueue = function (element) { items.push(element) } this.dequeue = function () { return items.shift() } this.front = function () { return items[0] } this.isEmpty = function () { return items.length === 0 } this.size = function () { // Number of elements returned to the queue return items.length } this.clear = function () { // Remove all elements from the queue items = [] } this.print = function () { console.log(items.toString()) } this.toString = function () { return items.toString() } }
Simple queue is achieved, so to speak of priority queue, at the beginning, it is a disciplined queue, but there will also be special circumstances, such as queuing for medical treatment in the hospital, when a pregnant woman is about to give birth, can not always let others follow the queue, then pregnant women have priority, can handle faster. So how to use javascript to achieve it, first of all, we have to give the element that joins the queue a flag, marking the priority of this element:
function QueueElement (element, key) { this.element = element this.key = key }
When joining the queue, a flag should also be given and inserted into the queue according to the flag:
this.enqueue = function (element, key) { let queueElement = new QueueElement(element, key) if (this.isEmpty()) { items.push(queueElement) } else { let added = false for (let i = 0; i < items.length; i++) { if (items[i].key > key) { items.splice(i, 0, queueElement) added = true break } } if (!added) { items.push(queueElement) } } }
Others are the same as regular queues. The code is as follows:
function PriorityQueue () { let items = [] function QueueElement (element, key) { this.element = element this.key = key } this.enqueue = function (element, key) { let queueElement = new QueueElement(element, key) if (this.isEmpty()) { items.push(queueElement) } else { let added = false for (let i = 0; i < items.length; i++) { if (items[i].key > key) { items.splice(i, 0, queueElement) added = true break } } if (!added) { items.push(queueElement) } } } this.dequeue = function () { return items.shift() } this.front = function () { return items[0] } this.isEmpty = function () { return items.length === 0 } this.size = function () { // Number of elements returned to the queue return items.length } this.clear = function () { // Remove all elements from the queue items = [] } this.print = function () { console.log(items.toString()) } this.toString = function () { return items.toString() } }
A Subject
In the game of drum beating and flower passing, the children form a circle and pass the flowers to the people next to them as soon as possible. At a certain moment, when the flower stops spreading, whoever falls in the hand of the flower will quit the circle to end the game. Repeat the process until only one child is left.
Answer:
function hotPotato (nameList) { let childrenQueue = new Queue() for(let i = 0; i < nameList.length; i++) { childrenQueue.enqueue(nameList[i]) } while (childrenQueue.size() > 1) { let ring = Math.round(Math.random() * childrenQueue.size() * 2 + childrenQueue.size()) for(let i = 0; i < ring; i++) { let out = childrenQueue.dequeue() childrenQueue.enqueue(out) // Put the elements of the queue back at the end of the queue to form a circular queue. let ring = Math.round(Math.random() * childrenQueue.size() * 2 + childrenQueue.size()) // Random elimination of competitors } console.log(childrenQueue.dequeue() + ' is out') // childrenQueue.dequeue() } console.log(childrenQueue.dequeue() + ' is winner') } hotPotato(['lili', 'aki', 'xiao', 'long', 'yuw', 'ray', 'wiess'])