双端队列和普通队列的区别就是 双端队列允许从后方移除或者添加数据,最常见的就是存储一些指令时使用,比如购票时,从前方的用户在购物完成后可以无需排队去问一些简单的问题,最后方的用户如果不想等待可以直接离开。
这是昨天的简单队列
class Queue {
constructor(){
this.queue= {},
this.index = 0,
this.top = 0
}
return_one() {
if (this.index > this.top) {
return this.queue[this.top]
}
return undefined
}
shift() {
if (this.index > this.top) {
let up = this.queue[this.top]
delete this.queue[this.top]
this.top++
return up
}
return undefined
}
add(value) {
this.queue[this.index] = value
this.index++
return value
}
toString(){
let x = []
for(let i in this.queue ){
x.push(this.queue[i])
}
return x
}
toNumber(){
return this.index-this.top
}
}
双端队列就要在其基础上添加返回最后一项和删除最后一项的功能
删除功能
pop(){
this.index-=1
let x = this.queue[this.index]
delete this.queue[this.index]
return x
}
返回最后一项功能
lastint(){
return this.queue[this.index-1]
}
所有代码
class Queue {
constructor() {
this.queue = {},
this.index = 0,
this.top = 0
}
return_one() {
if (this.index > this.top) {
return this.queue[this.top]
}
return undefined
}
shift() {
if (this.index > this.top) {
let up = this.queue[this.top]
delete this.queue[this.top]
this.top++
return up
}
return undefined
}
add(value) {
this.queue[this.index] = value
this.index++
return value
}
toString() {
let x = []
for (let i in this.queue) {
x.push(this.queue[i])
}
return x
}
toNumber() {
return this.index - this.top
}
pop(){
this.index-=1
let x = this.queue[this.index]
delete this.queue[this.index]
return x
}
lastint(){
return this.queue[this.index-1]
}
}
ok 队列完成! 后边我会找时间把平时做的算法题分享出来,恳请各位大佬能够给一些建议。