Code:
/**
* 隊列(基於動態數組)
* @class
*/
var AQueue = (function() {
/**
* 棧容器
* @type {DArray}
*/
let arr;
/**
* @class
*/
class _AQueue {
/**
* 構造器
* @constructor
* @param {number} [capacity]
*/
constructor(capacity) {
arr = new DArray(capacity);
}
/**
* 獲取現有元素的個數
* @return {number}
*/
get_size() {
return arr.get_size();
}
/**
* 獲取當前隊列數組的容量
* @return {number}
*/
get_capacity() {
return arr.get_capacity();
}
/**
* 獲取隊頭元素
* @return {*}
*/
get_front() {
return arr.get_head();
}
/**
* 入隊
* @param {*} el
*/
enqueue(el) {
arr.insert_to_tail(el);
}
/**
* 出隊
* @return {*}
*/
dequeue() {
return arr.remove_head();
}
/**
* 將隊列轉爲字符串輸出
* @return {string}
*/
to_string() {
const n = arr.get_size();
let ans = 'Queue: front [' + arr.get_head();
for (let i = 1; i < n; i++) {
ans += (',' + arr.get(i));
}
ans += '] tail';
return ans;
}
}
return _AQueue;
})();
注:DArray類的實現(代碼),請點擊 這裏