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;
      }
    }
  • 相关阅读:
    最大子数组
    链表插入排序
    链表求和
    有效回文串
    排球比赛计分系统
    超市收银系统
    三层架构
    Wpf+数据库代码封装+策略模式封装
    封装逻辑用策略模式实现
    代码封装
  • 原文地址:https://www.cnblogs.com/WP-WangPin/p/13888025.html
Copyright © 2011-2022 走看看