Files
hello-algo/ja/codes/javascript/chapter_stack_and_queue/array_stack.js
T
Yudong Jin 7257641e0e Retranslate the complete Japanese online edition from Chinese (#2008)
* Retranslate the complete Japanese online edition from Chinese

* Fit Japanese homepage labels and localize heading permalinks

* Point Japanese contribution instructions to the Japanese preview

* Replace locale conditionals with multilingual UI tables
2026-10-30 02:12:26 +08:00

76 lines
1.7 KiB
JavaScript

/**
* File: array_stack.js
* Created Time: 2022-12-09
* Author: S-N-O-R-L-A-X ([email protected])
*/
/* 配列で実装したスタック */
class ArrayStack {
#stack;
constructor() {
this.#stack = [];
}
/* スタックの長さを取得する */
get size() {
return this.#stack.length;
}
/* スタックが空かどうかを判定する */
isEmpty() {
return this.#stack.length === 0;
}
/* プッシュ */
push(num) {
this.#stack.push(num);
}
/* ポップ */
pop() {
if (this.isEmpty()) throw new Error('スタックが空です');
return this.#stack.pop();
}
/* スタックの先頭要素にアクセスする */
top() {
if (this.isEmpty()) throw new Error('スタックが空です');
return this.#stack[this.#stack.length - 1];
}
/* Array を返す */
toArray() {
return this.#stack;
}
}
/* 実行用コード */
/* スタックを初期化する */
const stack = new ArrayStack();
/* 要素をプッシュする */
stack.push(1);
stack.push(3);
stack.push(2);
stack.push(5);
stack.push(4);
console.log('スタック stack = ');
console.log(stack.toArray());
/* スタックの先頭要素にアクセスする */
const top = stack.top();
console.log('スタックの先頭要素 top = ' + top);
/* 要素をポップする */
const pop = stack.pop();
console.log('ポップした要素 pop = ' + pop + '、ポップ後 stack = ');
console.log(stack.toArray());
/* スタックの長さを取得する */
const size = stack.size;
console.log('スタックの長さ size = ' + size);
/* 空かどうかを判定する */
const isEmpty = stack.isEmpty();
console.log('スタックは空か = ' + isEmpty);