資料結構之佇列結構
阿新 • • 發佈:2021-10-14
佇列結構的特點是:一端進入,從另一端出去,可以總結為“先進先出”。
利用陣列實現佇列結構:
function Queue(params) { this.items = []; } // 1、進入佇列 Queue.prototype.enQueue = function (element) { this.items.push(element); } // 2、出佇列 Queue.prototype.deQueue = function () { return this.items.shift() } // 3、檢視佇列前的元素 Queue.prototype.peek = function () { return this.items[0]; } // 4、佇列是否為空 Queue.prototype.isEmpty = function () { return this.items.length === 0; } // 5、佇列長度 Queue.prototype.size = function () { return this.items.length; } // 6、toString方法 Queue.prototype.toString = function () { let result = ''; for (let i = 0; i < this.items.length; i++) { const element = this.items[i]; result += element + '' } // return result; }