相思资源网 Design By www.200059.com
1.队列是遵循先进先出(FIFO)原则的一组有序的项,队列在尾部添加元素,并从顶部移除元素,最新添加的元素必须排在队列的末尾。生活中常见的例子如排队等。
2.创建一个队列类
class Queue{
constructor(){
this.count = 0;//记录队列的数量
this.lowestCount = 0;//记录当前队列头部的位置
this.items = [];//用来存储元素。
}
}
3.添加元素
enqueue(element){
this.items[this.count] = element;
this.count++;
}
4.删除元素(只删除队列头部)
dequeue(){
if(this.isEmpty()){
return 'queue is null';
}
let resulte = this.items[this.lowestCount];
delete this.items[this.lowestCount];
this.lowestCount++;
return resulte;
}
5.查看队列头部元素
peek(){
return this.items[this.lowestCount];
}
6.判断队列是否为空
isEmpty(){
return this.count - this.lowestCount === 0;
}
7.清除队列的元素
clear(){
this.count = 0;
this.lowestCount = 0;
this.items = [];
}
8.查看队列的长度
size(){
return this.count - this.lowestCount;
}
9.查看队列的所有内容
toString(){
if(this.isEmpty())return "queue is null";
let objString = this.items[this.lowestCount];
for(let i = this.lowestCount+1; i < this.count;i++){
objString = `${objString},${this.items[i]}`;
}
return objString;
}
10.完整代码
class Queue{
constructor(){
this.count = 0;//记录队列的数量
this.lowestCount = 0;//记录当前队列顶部的位置
this.items = [];//用来存储元素。
}
enqueue(element){
this.items[this.count] = element;
this.count++;
}
dequeue(){
if(this.isEmpty()){
return 'queue is null';
}
let resulte = this.items[this.lowestCount];
delete this.items[this.lowestCount];
this.lowestCount++;
return resulte;
}
peek(){
return this.items[this.lowestCount];
}
isEmpty(){
return this.count - this.lowestCount === 0;
}
size(){
return this.count - this.lowestCount;
}
clear(){
this.count = 0;
this.lowestCount = 0;
this.items = [];
}
toString(){
if(this.isEmpty())return "queue is null";
let objString = this.items[this.lowestCount];
for(let i = this.lowestCount+1; i < this.count;i++){
objString = `${objString},${this.items[i]}`;
}
return objString;
}
}
11.运行结果
以上就是javaScript实现一个队列的方法的详细内容,更多关于javaScript实现一个队列的资料请关注其它相关文章!
相思资源网 Design By www.200059.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
相思资源网 Design By www.200059.com
暂无javaScript实现一个队列的方法的评论...
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。
