タイトル説明
問題解決のアイデア
- 2つのアレイを使用して初期化します
- エンキュー操作を格納する配列
- デキュー操作を格納する配列
- エンキュー関数は、システムによって入力された値をA配列にプッシュします
- デキュー関数は、最初にデキュー配列に要素があるかどうかを判断し、要素がある場合はそれをポップアウトします
- そうでない場合は、A配列の要素を逆の順序でB配列にプッシュしてから、ポップアウトします。
実装コード
var CQueue = function() {
this.stackA = [];
this.stackB = [];
};
CQueue.prototype.appendTail = function(value) {
console.log("系统输入的数据:",value);
this.stackA.push(value);
};
CQueue.prototype.deleteHead = function() {
if (this.stackB.length) {
return this.stackB.pop();
} else {
while (this.stackA.length) {
this.stackB.push(this.stackA.pop());
}
if (this.stackB.length === 0) {
return -1;
} else {
return this.stackB.pop();
}
}
};