队列
队列与栈结构类似,不同的在于一个(队列)是先进先出(FIFO),一个(栈)是后进先出(LIFO)。抽象示意图(基本队列):
这种结构,类似与菜市场排队买菜很类似,要实现这种数据结构,在javaScript中同样有两种方式,第一种基于数组的实现,这里不再赘述,仅以基于对象进行实现,中间的实现过程略微不同。
基于javaScript对象实现基本的队列
let queue = new Queue();
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
function Queue() {
// 队列
this.list = {};
// 当前排队值(队列尾)
this.count = 0;
// 队列头
this.index = 0;
// 向队列中加入一个元素
Queue.prototype.enqueue = function (item) {
this.list[this.count] = item;
this.count++;
}
// 出队列
Queue.prototype.dequeue = function () { // delete queue
if (this.isEmpty()) return undefined;
let shiftData = this.list[this.index];
delete this.list[this.index]
this.index++;
return shiftData;
}
// 队列是否为空
Queue.prototype.isEmpty = function () {
return !(this.count - this.index);
}
// 查看队列头
Queue.prototype.peek = function () {
if (this.isEmpty()) return undefined;
return this.list[this.index];
}
// 查看队列大小
Queue.prototype.size = function () {
return this.count - this.index;
}
// 清除队列
Queue.prototype.clear = function () {
this.list = {};
this.count = 0;
this.index = 0;
}
// toString方法
Queue.prototype.toString = function () {
if(this.isEmpty()) return "";
let str = "";
for(let i=this.index;i<this.count;i++){
str += this.list[i];
}
return str;
}
}
简单测试功能:
基于对象的双端队列
双端队列,和基本的队列很相似,不同的在于,所有可以改变队列的方法,会变成"前操作",“后操作”,比如,加入队列(在队列头添加或在队列尾添加)、删除队列(在队列头删除、在队列尾删除)。下面是实现的思路:
function DeQueue() { //double ended queue
this.list = {};
this.count = 0;
this.index = 0;
// 从前面加入队列
DeQueue.prototype.addFront = function (item) {
if (this.isEmpty()) {
this.addBack(item);
} else if(this.index > 0){
this.index--;
this.list[this.index] = item;
}else {
for(let i=this.count;i>0;i--){
this.list[i] = this.list[i - 1];
}
this.count++
this.index = 0;
this.list[0] = item;
}
}
// 从后面加入队列
DeQueue.prototype.addBack = function (item) {
this.list[this.count] = item;
this.count++;
}
// 从前面出队列
DeQueue.prototype.shift = function () { // delete queue
if (this.isEmpty()) return undefined;
let shiftData = this.list[this.index];
delete this.list[this.index]
this.index++;
return shiftData;
}
// 从后面出队列,与pop功能类似
DeQueue.prototype.pop = function () { // delete queue
if (this.isEmpty()) return undefined;
this.count--;
let popData = this.list[this.count];
delete this.list[this.count];
return popData;
}
// 队列是否为空
DeQueue.prototype.isEmpty = function () {
return !(this.count - this.index);
}
// 查看队列头
DeQueue.prototype.peekFront = function () {
if (this.isEmpty()) return undefined;
return this.list[this.index];
}
// 查看队列尾
DeQueue.prototype.peekBack = function () {
if (this.isEmpty()) return undefined;
return this.list[this.count - 1];
}
// 查看队列大小
DeQueue.prototype.size = function () {
return this.count - this.index;
}
// 清除队列
DeQueue.prototype.clear = function () {
this.list = {};
this.count = 0;
this.index = 0;
}
// toString方法
DeQueue.prototype.toString = function () {
if (this.isEmpty()) return "";
let str = "";
for (let i = this.index; i < this.count; i++) {
str += this.list[i];
}
return str;
}
}
let dequeue = new DeQueue();
dequeue.addFront(1); // list: 1
dequeue.addFront(2); // list: 2 1
dequeue.addFront(3); // list: 3 2 1
dequeue.addBack(4); // list: 3 2 1 4
dequeue.addBack(5); // list: 3 2 1 4 5
dequeue.addBack(6); // list: 3 2 1 4 5 6
dequeue.shift(); // list: 2 1 4 5 6
dequeue.pop(); // list: 2 1 4 5
console.log(dequeue.peekFront()); // 2
console.log(dequeue.peekBack()); // 5
使用队列的例子(击鼓传花)
function whoHasFlower(eles, num) {
let queue = new Queue();
let eliminatedList = [];
for (let i = 0; i < eles.length; i++) {
queue.enqueue(eles[i]);
}
while (queue.size() > 1) {
for (let i = 0; i < num; i++) {
queue.enqueue(queue.dequeue());
}
// 默认第一个被淘汰
eliminatedList.push(queue.dequeue());
}
return {
eliminated: eliminatedList,
winner: queue.dequeue()
}
}
// 测试
let res = whoHasFlower([1, 2, 3, 4, 5], 7);
console.log(res) // eliminated: [3,2,5,4],winner: 1
使用双端队列的例子(短文是否为回文的判断)
// 回文检查
function palindromeCheck(str){
if(!str) return false;
let dequeue = new DeQueue();
// 将语句中的字符串,转换成小写并去掉空格
let formatStr = str.toLowerCase().split(" ").join("");
let isEqual = true;
let firstStr, lastStr;
for(let i=0; i<formatStr.length;i++){
dequeue.addBack(formatStr.charAt(i));
}
while(dequeue.size() > 1 && isEqual){
firstStr = dequeue.shift();
lastStr = dequeue.pop();
if(firstStr != lastStr) {
isEqual = false;
}
}
console.log(isEqual)
return isEqual;
}
// 测试
palindromeCheck("a") // true
palindromeCheck("ab") // false
palindromeCheck("abba") // true
palindromeCheck("Step on no pets") // true
以上便是队列的简单应用,不管是栈结构还是队列结构,在编程过程中的性能优化,应用还是挺广泛的。尤其是栈结构,配合递归后会有不一样的效果。比如后面的广度优先搜索(BFS)的算法,就是结合了栈结构