简介
1 .先进先出
2 .
class Queue{
constructor(){
this.dataStore=[]
}
enqueue(el){
return this.dataStore.push(el)
}
// 添加一个到队列
dequeue(){
return this.dataStore.shift()
}
// 删除队首的元素
front(){
return this.dataStore[0]
}
// 返回队首
back(){
return this.dataStore[this.dataStore.length-1]
}
toString(){
return this.dataStore.join(" ")
}
isEmpty(){
if(this.dataStore.length==0){
return true
}else{
return false
}
}
}
var q = new Queue();
q.enqueue("Meredith");
q.enqueue("Cynthia");
q.enqueue("Jennifer");
console.log(q.toString())//Meredith Cynthia Jennifer
q.dequeue();
console.log(q.toString())//Cynthia Jennifer
console.log(q.front())//Cynthia
console.log(q.back())//Jennifer
console.log(q.enqueue('Luckfine'))
console.log(q.toString()) //Cynthia Jennifer Luckfine
网友评论