Home > Web Front-end > JS Tutorial > How to implement a simple queue using js

How to implement a simple queue using js

不言
Release: 2018-07-11 17:54:26
Original
2285 people have browsed it

This article mainly introduces how to use js to implement a simple queue, which has certain reference value. Now I share it with everyone. Friends in need can refer to it

Simple js queue

/**
 * [Queue]
 * @param {[Int]} size [队列大小]
 */function Queue(size) {    
 var list = [];    //向队列中添加数据
    this.push = function(data) {        
    if (data==null) {            
    return false;
        }        //如果传递了size参数就设置了队列的大小        
        if (size != null && !isNaN(size)) {            
        if (list.length == size) {
                this.pop();
            }
        }        list.unshift(data);        
        return true;
    }    //从队列中取出数据
    this.pop = function() {        
    return list.pop();
    }    //返回队列的大小
    this.size = function() {        
    return list.length;
    }    //返回队列的内容
    this.quere = function() {        
    return list;
    }
}function test(){     //初始化没有参数的队列    
var queue = new Queue();    
for (var i = 1; i <= 5; i++) {
        queue.push(i);
    }
    console.log(queue.quere()+queue.size());
    queue.pop();   //从队列中取出一个
    console.log(queue.quere()+queue.size());
    queue.push("yuruixin");
    queue.pop();   //从队列中取出一个
    console.log(queue.quere()+queue.size());
}
test();
Copy after login

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

How to disable the browser's backspace key in JS

How to use Vue.js with ajax binding Fixed data

The above is the detailed content of How to implement a simple queue using js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template