当前位置:网站首页>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
边栏推荐
- D Author: import C programming in D
- DANGER! V** caught climbing over the wall!
- 想问问,券商选哪个比较好尼?本人小白不懂,现在网上开户安全么?
- Embedded RTOS
- [high concurrency] deeply analyze the callable interface
- Meso tetra (4-N, N, n-trimethylaminophenyl) porphyrin (ttmapp) /meso tetra - [4- (BOC threonine) aminophenyl] porphyrin (TAPP thr BOC) supplied by Qiyue
- Awk of shell script
- [CV] wuenda machine learning course notes Chapter 13
- Regular expressions for shell script values
- After nine years of testing, the salary for interviewing Huawei is 10000. Huawei employees: the company doesn't have such a low salary position
猜你喜欢

Why can't the article be posted?
![[chromium] win10 vs2019 environment chromium configuration and compilation.](/img/20/428e6b22ed6955a732dd14d5ff0e3d.jpg)
[chromium] win10 vs2019 environment chromium configuration and compilation.

5000+ word interpretation | Product Manager: how to do a good job in component selection?

Output various graphics and text on the console through C #

Difference between parametric continuity and geometric continuity

Modularization and modular specification commonjs

Tcapulusdb Jun · industry news collection (V)

Easy to get started naturallanguageprocessing series topic 7 text classification based on fasttext

Tcapulusdb Jun · industry news collection (VI)

2022 recommended high-speed rail industry research report investment strategy industry development prospect market analysis (the attachment is a link to the online disk, and the report is continuously
随机推荐
2022 community group buying industry research industry development planning prospect investment market analysis report (the attachment is the online disk link, and the report is continuously updated)
Tcapulusdb Jun · industry news collection (V)
机器人强化学习——第一人称 VS 第三人称
Cloud native annual technology inventory is released! Ride the wind and waves at the right time
Would like to ask, which is the better choice for securities companies? I don't understand. Is it safe to open an account online now?
Kubernetes backup disaster recovery service product experience tutorial
February 14 institutional dragon and tiger list and operation of well-known hot money
Openfpga wishes you a happy Lantern Festival!
PCI Verilog IP
Conditional test, if and case conditional test statements of shell script
How to use regex in file find
HTTP Caching Protocol practice
Research Report on the new energy industry of recommended power equipment in 2022 industry development prospect market investment analysis (the attachment is a link to the network disk, and the report
Tcapulusdb Jun · industry news collection (III)
证券开户安全么,有没有什么危险呢
Personal blog item: processing of reading number +1 after viewing article details
It turns out that the joys and sorrows of programmers are not interlinked
Creation of Arduino uno development environment
How to insert pseudo code into word documents simply and quickly?
Testing grpc service with grpcui