当前位置:网站首页>Implementation of queue
Implementation of queue
2022-06-29 05:49:00 【Follow the road to the end】
The queue implements the following functions
enqueue() The method of entrance
dequeue() A team approach
top() Get team leader
size() Get the number of elements in the queue
clear() Clear queue
Based on the array
// Based on the array
class Queue {
constructor() {
this.queue = []
this.count = 0
}
// The team
enQueue(item) {
this.queue[this.count++] = item
}
// Out of the team
deQueue() {
if (this.isEmpty()) {
return
}
this.count--
return this.queue.shift()
}
isEmpty() {
return this.count === 0
}
// Get the value of the first element of the team
top() {
return this.queue[0]
}
// Get the number of elements
size() {
return this.count
}
// Clear queue
clear() {
this.queue = []
this.count = 0
}
}Based on the object
class Queue1 {
constructor() {
this.queue = {}
this.count = 0
this.head = 0 // The key used to record the head of the team
}
// The team
enQueue(item) {
this.queue[this.count++] = item
}
// Out of the team
deQueue() {
if (this.isEmpty()) {
return
}
const data = this.queue[this.head]
delete this.queue[this.head]
this.head++
return data
}
isEmpty() {
return this.size() === 0
}
// Get the value of the first element of the team
top() {
return this.queue[0]
}
// Get the number of elements
size() {
return this.count - this.head
}
// Clear queue
clear() {
this.queue = {}
this.count = 0
this.head = 0
}
}1
边栏推荐
- 开启生态新姿势 | 使用 WordPress 远程附件存储到 COS
- D parallel and rotator
- Leetcode theme [array] -219- duplicate Element II exists
- STI, one controller
- HTTP Caching Protocol practice
- On February 15, the market hot money operation and the dragon and tiger list
- Devops development, operation and maintenance Basics: using Jenkins to automatically build projects and notify by email
- Manual (functional) test 1
- HTTP Caching Protocol practice
- Establishing the development environment of esp8266
猜你喜欢
![[high concurrency] deeply analyze the callable interface](/img/dc/174f97fdd27180ed210d76768cc345.jpg)
[high concurrency] deeply analyze the callable interface

Top ten Devops best practices worthy of attention in 2022

Annual inventory review of Alibaba cloud's observable practices in 2021

innography

Research Report on the recommended lithography industry in 2022 industry development prospect market investment analysis (the attachment is a link to the network disk, and the report is continuously u
![[C language series] - branch and loop statements](/img/bf/656c9189b4ab4387c5acab1c4a2804.jpg)
[C language series] - branch and loop statements

2-nitro-5,10,15,20-tetra (3,5-dimethoxyphenyl) porphyrin (no2tdmpp) H2) /5,10,15,20-tetra (4-methylphenyl) porphyrin (TMPP) H2) Qiyue porphyrin products

使用VS创建静态链接库.lib并使用

HTTP Caching Protocol practice

Test content
随机推荐
How to use thread stack location
Domain name hack
Est - ce que l'ouverture d'un compte de titres est sécurisée? Y a - t - il un danger?
Quickly write MVVM code using source generators
51 lines of code, self-made TX to MySQL software!
Service grid ASM year end summary: how do end users use the service grid?
使用VS创建静态链接库.lib并使用
February 14 institutional dragon and tiger list and operation of well-known hot money
It turns out that the joys and sorrows of programmers are not interlinked
C语言用 printf 打印 《爱心》《火星撞地球》等,不断更新
5000+ 字解读 | 产品经理:如何做好元器件选型?
The first in China! CICA technology database antdb appears at the performance test tool conference of China Academy of communications technology
5- (4-benzoimide phenyl) - 10,15,20-triphenylporphyrin (battph2) and its Zn complex (battpzn) / tetra (4-aminophenyl) porphyrin (tapph2) Qiyue supply
Difference between parametric continuity and geometric continuity
Cipher
Is it safe to open a securities account? Is there any danger
Plugin
Openfpga wishes you a happy Lantern Festival!
D parallel and rotator
Embedded RTOS