zoukankan      html  css  js  c++  java
  • JavaScript实现基于对象的队列

     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 undefined;
        }
        const result = this.items[this.lowestCount];
        delete this.items[this.lowestCount];
        this.lowestCount++;
        return result;
      }
    
      peek() {
        if (this.isEmpty()) {
          return undefined;
        }
        return this.items[this.lowestCount];
      }
    
      isEmpty() {
        return this.size() === 0;
      }
    
      clear() {
        this.items = {};
        this.count = 0;
        this.lowestCount = 0;
      }
    
      size() {
        return this.count - this.lowestCount;
      }
    
      toString() {
        if (this.isEmpty()) {
          return '';
        }
        let objString = `${this.items[this.lowestCount]}`;
        for (let i = this.lowestCount + 1; i < this.count; i++) {
          objString = `${objString},${this.items[i]}`;
        }
        return objString;
      }
    }
  • 相关阅读:
    java.io.Serializable浅析
    SSH和SSM的区别
    [转]github详细教程
    GITHUB的使用
    常用端口-小结
    DNS的解析原理
    windows快捷键-小结
    ip地址0.0.0.0是什么意思
    windows插件框架下载地址
    redis和mongodb
  • 原文地址:https://www.cnblogs.com/WP-WangPin/p/13888025.html
Copyright © 2011-2022 走看看