牛骨文教育服务平台(让学习变的简单)
博文笔记

nodejs实现队列

创建时间:2017-11-13 投稿人: 浏览次数:233
"use strict"
const MAXLEN=2000;
class Queue {
    constructor() {
        this.filelist=[];
        this.top=0;
    }
    Push(path){
        this.filelist.push(path);
    }
    Pop() {
        if (this.top < this.filelist.length) {
            if (this.top > 32) {
                this.filelist=this.filelist.splice(this.top,this.filelist.length-this.top);
                this.top=0;
            }
            this.top+=1;
            return this.filelist[this.top-1]
        } else {
            return null;
        }
        this.Shuff();
    }
    Length(){
        return (this.filelist.length-this.top);
    }
    Shuff(){
        //队列push频率高,pop频率低
        if ((this.filelist.length-this.top) > MAXLEN) {
            this.filelist=this.filelist.splice(this.top,MAXLEN-700);
            this.top=0;
        }
    }
}

module.exports=new Queue();
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。