Queue is a linear data structure, which follows the order of FIFO(first in first out). The real life example of queue is the waiting line in front of a cinema, first come first served. Also when it comes to printing or test processing, we process the first added item. In queues, insertion(enqueue) and deletion(dequeue) happen on different ends, insert at the end and delete at the beginning.
队列是线性数据结构,遵循FIFO(先进先出)的顺序。 现实生活中的排队示例是电影院前的排队等候,先到先得。 同样,在打印或测试处理时,我们也会处理第一个添加的项目。 在队列中,insert( enqueue )和delete( dequeue )发生在不同的末端,在末端插入,在开始处删除。

JavaScript doesn’t have queue data structure, but we could use array and linked list classes to implement it.
JavaScript没有队列数据结构,但是我们可以使用数组和链接列表类来实现它。
Array Implementation
阵列实施
Because insertion and deletion happen on different ends, with the built-in array methods in JavaScript, we use push() with shift() — add element to the end, remove the first element. and unshift() with pop() — add element to the beginning, remove the last element. To understand the array implementation, let’s apply it to a leetcode question.
由于插入和删除发生在不同的末端,因此使用JavaScript中的内置数组方法,我们将push()与shift()结合使用 -在末尾添加元素,删除第一个元素。 和unshift()与pop()一起使用-将元素添加到开头,删除最后一个元素。 为了理解数组的实现,让我们将其应用于leetcode问题。
Write a class It has only one method: Return the number of Any ping with time in It is guaranteed that every call to Example 1:Input: inputs = ["RecentCounter","ping","ping","ping","ping"], inputs = [[],[1],[100],[3001],[3002]]Output: [null,1,2,3,3]Note:
1.Each test case will have at most 10000 calls to ping.
2.Each test case will call ping with strictly increasing values of t.
3.Each call to ping will have 1 <= t <= 10^9.
The solution for this problem is the following.
以下是该问题的解决方案。
class RecentCounter{
//initialize the class with queue property,
//which is an empty array
constructor(){
this.queue = []
}
//ping method receive integer t
//which represents some time in milliseconds
ping(t){
//for every ping we push it to the end of the queue
this.queue.push(t)
/*
To exclude the times that are not included in [t - 3000, t],
we remove the first element from the queue
while it is less than the calculated range
*/
while(this.queue[0] < t - 3000) {
this.queue.shift()
}
// return the count of calls, the array length
return this.queue.length
}
};
Linked List Implementation
链表实现
To implement it with linked list, we need to create a Node class and a Queue class.
要使用链表实现它,我们需要创建一个Node类和一个Queue类。
class Node {
constructor(value){
this.value = value
this.next = null
}
}
class Queue {
constructor(){
this.first = null
this.last = null
this.size = 0
}
}
How can we enqueue(insert) the queue? We accept a value and create a new node. If there is no node in the queue, set this new node as the first and the last property of the queue. Otherwise, set the next property on the current last to be the new node, and then set the last property of the queue to be the new node. Don’t forget to increment the size of the queue.
我们如何排队(插入)队列? 我们接受一个值并创建一个新节点。 如果队列中没有节点,则将此新节点设置为队列的第一个和最后一个属性。 否则,将当前的最后一个属性的下一个属性设置为新节点,然后将队列的最后一个属性设置为新节点。 不要忘记增加队列的大小。
class Node {
constructor(value){
this.value = value
this.next = null
}
}
class Queue {
constructor(){
this.first = null
this.last = null
this.size = 0
}
enqueue(value){
let newNode = new Node(value)
if(!this.first){
this.first = newNode
this.last = newNode
}else{
this.last.next = newNode
this.last = newNode
}
this.size++
return this.size
}
}
How can we dequeue(delete) the queue? If there is no first property, just return null. Otherwise, store the first property in a variable, see if the first is as same as the last (check if there is only one node in the queue). If so, set the first and the last to be null. If there are more than one node, set the first property to be the next property of the first. Return the value of the node dequeued. Don’t forget to decrement the size of the queue.
我们如何使队列出队(删除) ? 如果没有第一个属性,则返回null。 否则,将第一个属性存储在变量中,查看第一个属性是否与最后一个属性相同(检查队列中是否只有一个节点)。 如果是这样,请将第一个和最后一个设置为null。 如果有多个节点,请将第一个属性设置为第一个属性的下一个属性。 返回出队节点的值。 不要忘记减小队列的大小。
class Node {
constructor(value){
this.value = value
this.next = null
}
}
class Queue {
constructor(){
this.first = null
this.last = null
this.size = 0
}
enqueue(val){
let newNode = new Node(val)
if(!this.first){
this.first = newNode
this.last = newNode
}else{
this.last.next = newNode
this.last = newNode
}
this.size++
return this.size
}
dequeue(){
if(!this.first) return null
let temp = this.first
if(this.first === this.last){
this.last = null
}else{
this.first = this.first.next
this.size--
}
return temp
}
}
Big O of Queue
排队大O

翻译自: https://medium.com/datadriveninvestor/queue-in-javascript-e77ab51f6de0