javaScript实现一个队列的方法

所属分类: 网络编程 / JavaScript 阅读数: 756
收藏 0 赞 0 分享

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实现一个队列的资料请关注脚本之家其它相关文章!

更多精彩内容其他人还在看

深入解析Vue 组件命名那些事

本篇文章主要介绍了深入解析Vue 组件命名那些事,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Vue学习笔记进阶篇之vue-cli安装及介绍

这篇文章主要介绍了Vue学习笔记进阶篇之vue-cli安装及介绍,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

jquery版轮播图效果和extend扩展

这篇文章主要为大家详细介绍了jquery版轮播图效果,以及extend扩展的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

jQuery Validate格式验证功能实例代码(包括重名验证)

本文通过实例代码给大家介绍了jQuery Validate格式验证功能,代码中包括重名验证的方法,需要的的朋友参考下吧
收藏 0 赞 0 分享

Angular.js中angular-ui-router的简单实践

本篇文章主要介绍了Angular.js中angular-ui-router的简单实践,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

JavaScript实现二维坐标点排序效果

这篇文章主要为大家详细介绍了JavaScript实现二维坐标点排序效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

深入理解vue2.0路由如何配置问题

本篇文章主要介绍了vue2.0路由配置问题,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

基于bootstrap实现多个下拉框同时搜索功能

这篇文章主要为大家详细介绍了基于bootstrap实现多个下拉框同时搜索功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

JavaScript 值类型和引用类型的初次研究(推荐)

这篇文章主要介绍了JavaScript 值类型和引用类型的初次研究,需要的朋友可以参考下
收藏 0 赞 0 分享

利用jQuery异步上传文件的插件用法详解

这篇文章主要介绍了利用jQuery异步上传文件的插件用法详解,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多