JS数据结构(三)——队列

125 阅读1分钟

//创建队列
function Queue() {
	let 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.size = function() {
		return items.length;
	}

	//打印队列元素
	this.print = function() {
		console.log(items.toString());
	}
}