0
点赞
收藏
分享

微信扫一扫

利用JS实现队列

朱悟能_9ad4 2022-04-13 阅读 103

普通队列

队列遵循先进先出的原则,队列在尾部添加新元素,并从顶部移除元素。最新添加的元素必须排在队列的末尾。

我们可以通过数组实现队列,但是为了使其获取元素的时候更加高效,我们将使用一个对象来实现队列。

我们需要实现一些队列有关的方法

enqueue(element(s)): 向队列尾部添加一个(或多个)新的项

dequeue(): 移除队列中的第一项,并返回被移除的元素

peek():查看队列中的第一个元素

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

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

clear():清空队列

class Queue {
	constructor() {
		this.count = 0; //最新的
		this.lowestCount = 0; //最旧的
		this.items = {};
	}

	// 向队列添加元素
	enqueue(element) {
		this.items[this.count] = element;
		this.count++;
	}

	// 从队列中移除元素
	dequeue() {
		if (this.isEmpty()) {
			return undefined;
		}
		const result = this.items[this.lowestCount];
		delete this.items[this.lowestCount];
		this.lowestCount++;
		return result;
	}

	// 查看队列的第一个元素
	peek() {
		if (this.isEmpty()) {
			return undefined;
		}
		return this.items[this.lowestCount];
	}

	// 查看队列是否为空
	isEmpty() {
		return this.count - this.lowestCount === 0;
	}

	// 查看队列有多少数据
	size() {
		return this.count - this.lowestCount;
	}

	// 清空队列
	clear() {
		this.items = {};
		this.count = 0;
		this.lowestCount = 0;
	}

	// toString方法
	toString() {
		if (this.isEmpty()) {
			return "";
		}
		let objString = `${this.items[this.lowestCount]}`;
		for (let i = this.lowestCount + 1; i < this.count; i++) {
			objString = `${objString},${this.items[i]}`;
		}
		return objString;
	}
}

双端队列

双端队列是一种匀速我们同时从前端和后端添加和移除元素的特殊队列

我们同样需要实现一些有关的方法

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

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

clear():清空队列

addFront(element):该方法在双端队列前端添加新的元素

addBack(element):该方法在双端队列后端添加新的元素

removeFront():该方法删除双端队列前端的第一个元素

removeBack():该方法删除双端队列后端的第一个元素

peekFront():返回双端队列前端的第一个元素

peekBack():返回双端队列后端的第一个元素

class Deque {
	constructor() {
		this.items = {};
		this.count = 0; //最新的
		this.lowestCount = 0; //最旧的
	}

	// 向双端队列后端添加数据
	addBack(element) {
		this.items[this.count] = element;
		this.count++;
	}

	// 向双端队列前端添加数据
	// 存在三种场景
	// 1.双端队列是空的,就直接调用addBack方法
	// 2.一个元素已经被从双端队列的前端移除,也就是说lowestCount属性会大于等于1
	// 3.lowestCount为0
	addFront(element) {
		if (this.isEmpty()) {
			this.addBack(element);
		} else if (this.lowestCount > 0) {
			this.lowestCount--;
			this.items[this.lowestCount] = element;
		} else {
			for (let i = this.count; i > 0; i--) {
				this.items[i] = this.items[i - 1];
			}
			this.count++;
			this.lowestCount = 0;
			this.items[0] = element;
		}
	}

	// 查看双端队列是否为空
	isEmpty() {
		return this.count - this.lowestCount === 0;
	}

	// 查看双端队列有多少数据
	size() {
		return this.count - this.lowestCount;
	}

	// 清空双端队列
	clear() {
		this.items = {};
		this.count = 0;
		this.lowestCount = 0;
	}

	// 删除双端队列前端的第一个元素
	removeFront() {
		const result = this.items[this.lowestCount];
		delete this.items[this.lowestCount];
		this.lowestCount++;
		return result;
	}

	// 删除双端队列后端的第一个元素
	removeBack() {
		this.count--;
		const result = this.items[this.count];
		delete this.items[this.count];
		return result;
	}

	// 返回双端队列前端的第一个元素
	peekFront() {
		if (this.isEmpty()) {
			return undefined;
		}
		return this.items[this.lowestCount];
	}

	// 返回双端队列后端的第一个元素
	peekBack() {
		return this.items[this.count - 1];
	}
}

举报

相关推荐

0 条评论