队列

1:队列概念

队列是遵循FIFO先进先出原则的一组有序的元素.队列在尾部添加新元素,从队列的头部删除元素.

2:创建队列

function Queue(){

//这里是属性和方法

}

3:队列api

首先需要一个用于存储队列元素的数据结构,我们可以使用数组

var items = [];

方法:

  enqueue(element):向队列尾部添加一个或者多个元素的新的数据项

  dequeue():移除队列的第一个数据项,并且返回被移除的元素.

  front():返回队列中的第一个元素

  isEmpty():如果队列中不包含任何元素,返回true,否则返回false.

  size():返回队列包含的元素的个数

4:队列完整代码:

function Queue(){
	var items = [];
	this.enqueue = function(element){
		items.push(element)
	}
	this.dequeue = function(){
		return items.shift();
	}
	this.front = function(){
		return items[0];
	}
	this.isEmpty=function(){
		return items.length == 0;
	}
	
	this.clear = function(){
		items = [];
	}
	this.size = function(){
		return items.length;
	}
	this.print = function(){
		console.log(items.toString());
	}
	
}

  

原文地址:https://www.cnblogs.com/airycode/p/8074421.html