In fact, there are many similarities between queues and stacks, including some of the methods and ways of using them. Only queues use a completely different principle from stacks. Stacks use the last in first out principle, while queues use the First In First Out principle.
I. queue
//Declare Queue class
function Queue() {
//Declare and initialize an array to hold queue elements.
let items = [];
//Add queue element
this.enqueue = function (element) {
items.push(element)
};
//Remove and return the queue element
this.dequeue = function () {
return items.shift();
};
//Get queue header element
this.front = function () {
return items[0];
};
//Judge whether the queue element is empty
this.isEmpty = function () {
return items.length == 0;
};
//Get the number of queue elements
this.size = function () {
return items.length;
};
//Print the queue
this.print = function () {
console.log(items.toString())
};
}
const queue = new Queue();
console.log(queue.isEmpty()); // outputs true
queue.enqueue('John');
queue.enqueue('Jack');
queue.print(); // John,Jack
queue.enqueue('Camila');
queue.print(); // John,Jack,Camila
console.log(queue.size()); // outputs 3
console.log(queue.isEmpty()); // outputs false
queue.dequeue(); // remove John
queue.dequeue(); // remove Jack
queue.print(); // Camila
We have already implemented the data structure of queue. In the same way, can we change it a little to make the implementation of queue look more beautiful?
let Queue = (function () {
const items = new WeakMap();
class Queue {
constructor () {
//It is important to note that items here are data of type WeakMap, while WeakMap is a key value pair. There are exclusive set and get methods to get and set values.
//So [] is set here for this, i.e. this is the key name and [] is the value, so the queue formed by this method is still an operation on the array.
items.set(this,[]);
}
enqueue(element) {
let q = items.get(this);//The q here is the same as the []
q.push(element);
}
dequeue() {
let q = items.get(this);
let r = q.shift();
return r;
}
front() {
return items.get(this)[0];
}
isEmpty() {
return items.get(this).length == 0;
}
size() {
return items.get(this).length;
}
print() {
console.log(items.get(this));
}
}
return Queue;
})()
In fact, the introduction of the queue is basically finished here, but I feel that it's a little tricky. So let's finish the rest in this article.
//Declare Queue class
function PriorityQueue() {
//Declare and initialize an array to hold queue elements.
let items = [];
//Create an element class with priority
function QueueElement(element,priority) {
this.element = element;
this.priority = priority;
}
//Add queue element
this.enqueue = function (element,priority) {
let queueElement = new QueueElement(element,priority);
let added = false;
//Traverse the queue element, 1 has the highest priority, and so on. If the priority of the current element is greater than items[i], put the element in front of items[i].
//If the second parameter of the splice method is 0, the third parameter is added before i.
for(let i = 0; i < items.length; i ) {
if(queueElement.priority < items[i].priority) {
items.splice(i,0,queueElement);
added = true;break;
}
}
// Determine whether the element can be listed directly through added.
if(!added) {
items.push(queueElement);
}
};
//Remove and return the queue element
this.dequeue = function () {
return items.shift();
};
//Get queue header element
this.front = function () {
return items[0];
};
//Judge whether the queue element is empty
this.isEmpty = function () {
return items.length == 0;
};
//Get the number of queue elements
this.size = function () {
return items.length;
};
//Circular print element and its priority "`" are template strings of ES6
this.print = function () {
for(let i = 0; i < items.length; i ) {
console.log(`${items[i].element} - ${items[i].priority}`);
}
};
}
const queue = new PriorityQueue();
console.log(queue.isEmpty()); // outputs true
queue.enqueue('zaking',2);
queue.enqueue('linbo',6);
queue.enqueue('queue',5);
queue.enqueue('ada',3);
queue.enqueue('John',1);
queue.enqueue('Jack',2);
queue.enqueue('Camila',3);
queue.enqueue('zak',3);
queue.print();
The main change lies in the setting of queue elements and enqueue method. Since priority needs to be set for each cyclic queue element, the queue element is slightly changed to have two parameters (element itself and priority). Since the queue is inserted according to different priority, the enqueue method of cyclic queue also needs to be cyclic. The entire queue to determine where to insert.
function hotPotato(nameList, num) {
const queue = new Queue();
//Put all the names in order
for (let i = 0; i < nameList.length; i ) {
queue.enqueue(nameList[i]);
}
//Declare the name of the currently eliminated personnel
let eliminated = '';
//If there is more than one element in the queue, there is no final winner. If there is only one element left, the last winner will be listed.
while (queue.size() > 1) {
//Loop the current queue num times, and re-enter the "dequeue element" in the queue header.
for (let i = 0; i < num; i ) {
queue.enqueue(queue.dequeue());
}
//At the end of the loop, the elements of the current queue are listed, which is the knockout.
eliminated = queue.dequeue();
queue.print();
console.log(eliminated "Be eliminated");
}
return queue.dequeue();
}
let names = ["zak","zaking","james","lili","bole","londo","fali"]
console.log(hotPotato(names,7))
In the above method, each cycle will successively put the head element into the tail, and then a cycle is realized. After the cycle, the element at the front end of the queue is regarded as eliminated, until only one element is left at the end, which is a real simulation of the drum game.
Finally, due to my limited level, my ability is still far from that of the great God. If there is any mistake or ambiguity, I hope you will give me some advice. Thank you very much.
For more professional front-end knowledge, please go to [ape 2048] www.mk2048.com