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
/** * [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();
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!