当前位置:网站首页>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
边栏推荐
- 嵌入式RTOS
- Week 10 - task 1- fill in the blank: line class inherits point class
- Purple red solid meso tetra (o-alkoxyphenyl) porphyrin cobalt (meso-t (2-rop) PCO) / tetra (n, n-diphenyl-p-amino) phenyl porphyrin (tdpatph2)
- 5- (4-benzoimide phenyl) - 10,15,20-triphenylporphyrin (battph2) and its Zn complex (battpzn) / tetra (4-aminophenyl) porphyrin (tapph2) Qiyue supply
- Research Report on recommended specialized, special and new industries in 2022 industry development prospect and market investment analysis (the attachment is a link to the online disk, and the report
- 2022 recommended trend toy blind box industry research report industry development prospect market analysis white paper (the attachment is a link to the network disk, and the report is continuously up
- Why Houdini made the pyside2 plug-in crash
- Creation of Arduino uno development environment
- How to use thread stack location
- Hustoj SPJ example
猜你喜欢

2022 recommended property management industry research report industry development prospect market investment analysis (the attachment is the link to the online disk, and the report is continuously up

Establishing the development environment of esp8266

innography

Will the order of where conditions in MySQL affect the union index? Will where 1 =1 affect the use of the index? Does where 1 =1 affect the use of indexes?

2022 recommended prefabricated construction industry research report industry development prospect market analysis white paper (the attachment is a link to the network disk, and the report is continuo

The fresh student who was born in Ali after 2000: it's really fragrant to mend this

Conditional test, if and case conditional test statements of shell script

Love that can't be met -- what is the intimate relationship maintained by video chat

After nine years of testing, the salary for interviewing Huawei is 10000. Huawei employees: the company doesn't have such a low salary position

CCTV revealed that xumengtao won the black Technology: there was a virtual coach???
随机推荐
patent filter
How to use regex in file find
i-Teams W3: How to build a sound-bottling business
2022 recommended REITs Industry Research Report investment strategy industry development prospect market analysis (the attachment is a link to the online disk, and the report is continuously updated)
Open a new ecological posture | use WordPress remote attachment to store to Cos
Structure training camp module II operation
C language uses printf to print love, Mars strikes the earth, etc., which are constantly updated
Embedded RTOS
[C language series] - branch and loop statements
[chromium] win10 vs2019 environment chromium configuration and compilation.
Blip: conduct multimodal pre training with cleaner and more diverse data, and the performance exceeds clip! Open source code
Skills of writing test cases efficiently
AttributeError: module ‘torch. nn. Parameter 'has no attribute' uninitializedparameter 'solution
Research on heuristic intelligent task scheduling
STI, one controller
Week 10 - task 3- from point to circle to cylinder
Domain name hack
Service grid ASM year end summary: how do end users use the service grid?
Why Houdini made the pyside2 plug-in crash
51 single chip microcomputer learning notes 7 -- Ultrasonic Ranging