mirror of
https://github.com/krahets/hello-algo.git
synced 2026-06-15 22:57:48 +08:00
Re-translate the Japanese version (#1871)
* Retranslate Japanese docs with GPT-5.4 * Retranslate Japanese code with GPT-5.4
This commit is contained in:
156
ja/codes/javascript/chapter_stack_and_queue/array_deque.js
Normal file
156
ja/codes/javascript/chapter_stack_and_queue/array_deque.js
Normal file
@@ -0,0 +1,156 @@
|
||||
/**
|
||||
* File: array_deque.js
|
||||
* Created Time: 2023-02-28
|
||||
* Author: Zhuo Qinyue (1403450829@qq.com)
|
||||
*/
|
||||
|
||||
/* 循環配列ベースの両端キュー */
|
||||
class ArrayDeque {
|
||||
#nums; // 両端キューの要素を格納する配列
|
||||
#front; // 先頭ポインタ。先頭要素を指す
|
||||
#queSize; // 両端キューの長さ
|
||||
|
||||
/* コンストラクタ */
|
||||
constructor(capacity) {
|
||||
this.#nums = new Array(capacity);
|
||||
this.#front = 0;
|
||||
this.#queSize = 0;
|
||||
}
|
||||
|
||||
/* 両端キューの容量を取得 */
|
||||
capacity() {
|
||||
return this.#nums.length;
|
||||
}
|
||||
|
||||
/* 両端キューの長さを取得 */
|
||||
size() {
|
||||
return this.#queSize;
|
||||
}
|
||||
|
||||
/* 両端キューが空かどうかを判定 */
|
||||
isEmpty() {
|
||||
return this.#queSize === 0;
|
||||
}
|
||||
|
||||
/* 循環配列のインデックスを計算 */
|
||||
index(i) {
|
||||
// 剰余演算により配列の先頭と末尾をつなげる
|
||||
// i が配列の末尾を越えたら先頭に戻る
|
||||
// i が配列の先頭を越えて前に出たら末尾に戻る
|
||||
return (i + this.capacity()) % this.capacity();
|
||||
}
|
||||
|
||||
/* キュー先頭にエンキュー */
|
||||
pushFirst(num) {
|
||||
if (this.#queSize === this.capacity()) {
|
||||
console.log('両端キューがいっぱいです');
|
||||
return;
|
||||
}
|
||||
// 先頭ポインタを左に 1 つ移動する
|
||||
// 剰余演算により、front が配列先頭を越えた後に末尾へ戻るようにする
|
||||
this.#front = this.index(this.#front - 1);
|
||||
// num をキュー先頭に追加
|
||||
this.#nums[this.#front] = num;
|
||||
this.#queSize++;
|
||||
}
|
||||
|
||||
/* キュー末尾にエンキュー */
|
||||
pushLast(num) {
|
||||
if (this.#queSize === this.capacity()) {
|
||||
console.log('両端キューがいっぱいです');
|
||||
return;
|
||||
}
|
||||
// キュー末尾ポインタを計算し、末尾インデックス + 1 を指す
|
||||
const rear = this.index(this.#front + this.#queSize);
|
||||
// num をキュー末尾に追加
|
||||
this.#nums[rear] = num;
|
||||
this.#queSize++;
|
||||
}
|
||||
|
||||
/* キュー先頭からデキュー */
|
||||
popFirst() {
|
||||
const num = this.peekFirst();
|
||||
// 先頭ポインタを 1 つ後ろへ進める
|
||||
this.#front = this.index(this.#front + 1);
|
||||
this.#queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* キュー末尾からデキュー */
|
||||
popLast() {
|
||||
const num = this.peekLast();
|
||||
this.#queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* キュー先頭の要素にアクセス */
|
||||
peekFirst() {
|
||||
if (this.isEmpty()) throw new Error('The Deque Is Empty.');
|
||||
return this.#nums[this.#front];
|
||||
}
|
||||
|
||||
/* キュー末尾の要素にアクセス */
|
||||
peekLast() {
|
||||
if (this.isEmpty()) throw new Error('The Deque Is Empty.');
|
||||
// 末尾要素のインデックスを計算
|
||||
const last = this.index(this.#front + this.#queSize - 1);
|
||||
return this.#nums[last];
|
||||
}
|
||||
|
||||
/* 出力用の配列を返す */
|
||||
toArray() {
|
||||
// 有効長の範囲内のリスト要素のみを変換
|
||||
const res = [];
|
||||
for (let i = 0, j = this.#front; i < this.#queSize; i++, j++) {
|
||||
res[i] = this.#nums[this.index(j)];
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* 両端キューを初期化 */
|
||||
const capacity = 5;
|
||||
const deque = new ArrayDeque(capacity);
|
||||
deque.pushLast(3);
|
||||
deque.pushLast(2);
|
||||
deque.pushLast(5);
|
||||
console.log('両端キュー deque = [' + deque.toArray() + ']');
|
||||
|
||||
/* 要素にアクセス */
|
||||
const peekFirst = deque.peekFirst();
|
||||
console.log('先頭要素 peekFirst = ' + peekFirst);
|
||||
const peekLast = deque.peekLast();
|
||||
console.log('末尾要素 peekLast = ' + peekLast);
|
||||
|
||||
/* 要素をエンキュー */
|
||||
deque.pushLast(4);
|
||||
console.log('要素 4 を末尾に追加した後 deque = [' + deque.toArray() + ']');
|
||||
deque.pushFirst(1);
|
||||
console.log('要素 1 を先頭に追加した後 deque = [' + deque.toArray() + ']');
|
||||
|
||||
/* 要素をデキュー */
|
||||
const popLast = deque.popLast();
|
||||
console.log(
|
||||
'末尾から取り出した要素 = ' +
|
||||
popLast +
|
||||
'、末尾から取り出した後 deque = [' +
|
||||
deque.toArray() +
|
||||
']'
|
||||
);
|
||||
const popFirst = deque.popFirst();
|
||||
console.log(
|
||||
'先頭から取り出した要素 = ' +
|
||||
popFirst +
|
||||
'、先頭から取り出した後 deque = [' +
|
||||
deque.toArray() +
|
||||
']'
|
||||
);
|
||||
|
||||
/* 両端キューの長さを取得 */
|
||||
const size = deque.size();
|
||||
console.log('両端キューの長さ size = ' + size);
|
||||
|
||||
/* 両端キューが空かどうかを判定 */
|
||||
const isEmpty = deque.isEmpty();
|
||||
console.log('両端キューが空かどうか = ' + isEmpty);
|
||||
106
ja/codes/javascript/chapter_stack_and_queue/array_queue.js
Normal file
106
ja/codes/javascript/chapter_stack_and_queue/array_queue.js
Normal file
@@ -0,0 +1,106 @@
|
||||
/**
|
||||
* File: array_queue.js
|
||||
* Created Time: 2022-12-13
|
||||
* Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com)
|
||||
*/
|
||||
|
||||
/* 循環配列ベースのキュー */
|
||||
class ArrayQueue {
|
||||
#nums; // キュー要素を格納する配列
|
||||
#front = 0; // 先頭ポインタ。先頭要素を指す
|
||||
#queSize = 0; // キューの長さ
|
||||
|
||||
constructor(capacity) {
|
||||
this.#nums = new Array(capacity);
|
||||
}
|
||||
|
||||
/* キューの容量を取得 */
|
||||
get capacity() {
|
||||
return this.#nums.length;
|
||||
}
|
||||
|
||||
/* キューの長さを取得 */
|
||||
get size() {
|
||||
return this.#queSize;
|
||||
}
|
||||
|
||||
/* キューが空かどうかを判定 */
|
||||
isEmpty() {
|
||||
return this.#queSize === 0;
|
||||
}
|
||||
|
||||
/* エンキュー */
|
||||
push(num) {
|
||||
if (this.size === this.capacity) {
|
||||
console.log('キューがいっぱいです');
|
||||
return;
|
||||
}
|
||||
// 末尾ポインタを計算し、末尾インデックス + 1 を指す
|
||||
// 剰余演算により、rear が配列末尾を越えた後に先頭へ戻るようにする
|
||||
const rear = (this.#front + this.size) % this.capacity;
|
||||
// num をキュー末尾に追加
|
||||
this.#nums[rear] = num;
|
||||
this.#queSize++;
|
||||
}
|
||||
|
||||
/* デキュー */
|
||||
pop() {
|
||||
const num = this.peek();
|
||||
// 先頭ポインタを1つ後ろへ進め、末尾を越えたら配列先頭に戻す
|
||||
this.#front = (this.#front + 1) % this.capacity;
|
||||
this.#queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* キュー先頭の要素にアクセス */
|
||||
peek() {
|
||||
if (this.isEmpty()) throw new Error('キューが空です');
|
||||
return this.#nums[this.#front];
|
||||
}
|
||||
|
||||
/* Array を返す */
|
||||
toArray() {
|
||||
// 有効長の範囲内のリスト要素のみを変換
|
||||
const arr = new Array(this.size);
|
||||
for (let i = 0, j = this.#front; i < this.size; i++, j++) {
|
||||
arr[i] = this.#nums[j % this.capacity];
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* キューを初期化 */
|
||||
const capacity = 10;
|
||||
const queue = new ArrayQueue(capacity);
|
||||
|
||||
/* 要素をエンキュー */
|
||||
queue.push(1);
|
||||
queue.push(3);
|
||||
queue.push(2);
|
||||
queue.push(5);
|
||||
queue.push(4);
|
||||
console.log('キュー queue =', queue.toArray());
|
||||
|
||||
/* キュー先頭の要素にアクセス */
|
||||
const peek = queue.peek();
|
||||
console.log('先頭要素 peek = ' + peek);
|
||||
|
||||
/* 要素をデキュー */
|
||||
const pop = queue.pop();
|
||||
console.log('取り出した要素 pop = ' + pop + '、取り出した後 queue =', queue.toArray());
|
||||
|
||||
/* キューの長さを取得 */
|
||||
const size = queue.size;
|
||||
console.log('キューの長さ size = ' + size);
|
||||
|
||||
/* キューが空かどうかを判定 */
|
||||
const isEmpty = queue.isEmpty();
|
||||
console.log('キューは空か = ' + isEmpty);
|
||||
|
||||
/* 循環配列をテストする */
|
||||
for (let i = 0; i < 10; i++) {
|
||||
queue.push(i);
|
||||
queue.pop();
|
||||
console.log('第 ' + i + ' 回エンキュー + デキュー後 queue =', queue.toArray());
|
||||
}
|
||||
75
ja/codes/javascript/chapter_stack_and_queue/array_stack.js
Normal file
75
ja/codes/javascript/chapter_stack_and_queue/array_stack.js
Normal file
@@ -0,0 +1,75 @@
|
||||
/**
|
||||
* File: array_stack.js
|
||||
* Created Time: 2022-12-09
|
||||
* Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com)
|
||||
*/
|
||||
|
||||
/* 配列ベースのスタック */
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* スタックを初期化 */
|
||||
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);
|
||||
44
ja/codes/javascript/chapter_stack_and_queue/deque.js
Normal file
44
ja/codes/javascript/chapter_stack_and_queue/deque.js
Normal file
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* File: deque.js
|
||||
* Created Time: 2023-01-17
|
||||
* Author: Zhuo Qinyue (1403450829@qq.com)
|
||||
*/
|
||||
|
||||
/* Driver Code */
|
||||
/* 両端キューを初期化 */
|
||||
// JavaScript には組み込みの両端キューがないため、Array を両端キューとして使う
|
||||
const deque = [];
|
||||
|
||||
/* 要素をエンキュー */
|
||||
deque.push(2);
|
||||
deque.push(5);
|
||||
deque.push(4);
|
||||
// 注意: 配列であるため、unshift() メソッドの時間計算量は O(n)
|
||||
deque.unshift(3);
|
||||
deque.unshift(1);
|
||||
console.log('両端キュー deque = ', deque);
|
||||
|
||||
/* 要素にアクセス */
|
||||
const peekFirst = deque[0];
|
||||
console.log('先頭要素 peekFirst = ' + peekFirst);
|
||||
const peekLast = deque[deque.length - 1];
|
||||
console.log('末尾要素 peekLast = ' + peekLast);
|
||||
|
||||
/* 要素をデキュー */
|
||||
// 注意: 配列であるため、shift() メソッドの時間計算量は O(n)
|
||||
const popFront = deque.shift();
|
||||
console.log(
|
||||
'先頭からデキューした要素 popFront = ' + popFront + ',先頭からデキュー後 deque = ' + deque
|
||||
);
|
||||
const popBack = deque.pop();
|
||||
console.log(
|
||||
'末尾からデキューした要素 popBack = ' + popBack + ',末尾からデキュー後 deque = ' + deque
|
||||
);
|
||||
|
||||
/* 両端キューの長さを取得 */
|
||||
const size = deque.length;
|
||||
console.log('両端キューの長さ size = ' + size);
|
||||
|
||||
/* 両端キューが空かどうかを判定 */
|
||||
const isEmpty = size === 0;
|
||||
console.log('両端キューが空かどうか = ' + isEmpty);
|
||||
167
ja/codes/javascript/chapter_stack_and_queue/linkedlist_deque.js
Normal file
167
ja/codes/javascript/chapter_stack_and_queue/linkedlist_deque.js
Normal file
@@ -0,0 +1,167 @@
|
||||
/**
|
||||
* File: linkedlist_deque.js
|
||||
* Created Time: 2023-02-04
|
||||
* Author: Zhuo Qinyue (1403450829@qq.com)
|
||||
*/
|
||||
|
||||
/* 双方向連結リストノード */
|
||||
class ListNode {
|
||||
prev; // 前駆ノードへの参照(ポインタ)
|
||||
next; // 後継ノードへの参照(ポインタ)
|
||||
val; // ノード値
|
||||
|
||||
constructor(val) {
|
||||
this.val = val;
|
||||
this.next = null;
|
||||
this.prev = null;
|
||||
}
|
||||
}
|
||||
|
||||
/* 双方向連結リストベースの両端キュー */
|
||||
class LinkedListDeque {
|
||||
#front; // 先頭ノード front
|
||||
#rear; // 末尾ノード rear
|
||||
#queSize; // 両端キューの長さ
|
||||
|
||||
constructor() {
|
||||
this.#front = null;
|
||||
this.#rear = null;
|
||||
this.#queSize = 0;
|
||||
}
|
||||
|
||||
/* 末尾へのエンキュー操作 */
|
||||
pushLast(val) {
|
||||
const node = new ListNode(val);
|
||||
// 連結リストが空なら、front と rear の両方を node に向ける
|
||||
if (this.#queSize === 0) {
|
||||
this.#front = node;
|
||||
this.#rear = node;
|
||||
} else {
|
||||
// node を連結リストの末尾に追加
|
||||
this.#rear.next = node;
|
||||
node.prev = this.#rear;
|
||||
this.#rear = node; // 末尾ノードを更新する
|
||||
}
|
||||
this.#queSize++;
|
||||
}
|
||||
|
||||
/* 先頭へのエンキュー操作 */
|
||||
pushFirst(val) {
|
||||
const node = new ListNode(val);
|
||||
// 連結リストが空なら、front と rear の両方を node に向ける
|
||||
if (this.#queSize === 0) {
|
||||
this.#front = node;
|
||||
this.#rear = node;
|
||||
} else {
|
||||
// node を連結リストの先頭に追加
|
||||
this.#front.prev = node;
|
||||
node.next = this.#front;
|
||||
this.#front = node; // 先頭ノードを更新する
|
||||
}
|
||||
this.#queSize++;
|
||||
}
|
||||
|
||||
/* キュー末尾からの取り出し */
|
||||
popLast() {
|
||||
if (this.#queSize === 0) {
|
||||
return null;
|
||||
}
|
||||
const value = this.#rear.val; // 末尾ノードの値を保存する
|
||||
// 末尾ノードを削除
|
||||
let temp = this.#rear.prev;
|
||||
if (temp !== null) {
|
||||
temp.next = null;
|
||||
this.#rear.prev = null;
|
||||
}
|
||||
this.#rear = temp; // 末尾ノードを更新する
|
||||
this.#queSize--;
|
||||
return value;
|
||||
}
|
||||
|
||||
/* キュー先頭からの取り出し */
|
||||
popFirst() {
|
||||
if (this.#queSize === 0) {
|
||||
return null;
|
||||
}
|
||||
const value = this.#front.val; // 末尾ノードの値を保存する
|
||||
// 先頭ノードを削除
|
||||
let temp = this.#front.next;
|
||||
if (temp !== null) {
|
||||
temp.prev = null;
|
||||
this.#front.next = null;
|
||||
}
|
||||
this.#front = temp; // 先頭ノードを更新する
|
||||
this.#queSize--;
|
||||
return value;
|
||||
}
|
||||
|
||||
/* キュー末尾の要素にアクセス */
|
||||
peekLast() {
|
||||
return this.#queSize === 0 ? null : this.#rear.val;
|
||||
}
|
||||
|
||||
/* キュー先頭の要素にアクセス */
|
||||
peekFirst() {
|
||||
return this.#queSize === 0 ? null : this.#front.val;
|
||||
}
|
||||
|
||||
/* 両端キューの長さを取得 */
|
||||
size() {
|
||||
return this.#queSize;
|
||||
}
|
||||
|
||||
/* 両端キューが空かどうかを判定 */
|
||||
isEmpty() {
|
||||
return this.#queSize === 0;
|
||||
}
|
||||
|
||||
/* 両端キューを出力する */
|
||||
print() {
|
||||
const arr = [];
|
||||
let temp = this.#front;
|
||||
while (temp !== null) {
|
||||
arr.push(temp.val);
|
||||
temp = temp.next;
|
||||
}
|
||||
console.log('[' + arr.join(', ') + ']');
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* 両端キューを初期化 */
|
||||
const linkedListDeque = new LinkedListDeque();
|
||||
linkedListDeque.pushLast(3);
|
||||
linkedListDeque.pushLast(2);
|
||||
linkedListDeque.pushLast(5);
|
||||
console.log('両端キュー linkedListDeque = ');
|
||||
linkedListDeque.print();
|
||||
|
||||
/* 要素にアクセス */
|
||||
const peekFirst = linkedListDeque.peekFirst();
|
||||
console.log('先頭要素 peekFirst = ' + peekFirst);
|
||||
const peekLast = linkedListDeque.peekLast();
|
||||
console.log('末尾要素 peekLast = ' + peekLast);
|
||||
|
||||
/* 要素をエンキュー */
|
||||
linkedListDeque.pushLast(4);
|
||||
console.log('要素 4 を末尾にエンキュー後 linkedListDeque = ');
|
||||
linkedListDeque.print();
|
||||
linkedListDeque.pushFirst(1);
|
||||
console.log('要素 1 を先頭にエンキュー後 linkedListDeque = ');
|
||||
linkedListDeque.print();
|
||||
|
||||
/* 要素をデキュー */
|
||||
const popLast = linkedListDeque.popLast();
|
||||
console.log('末尾からデキューした要素 = ' + popLast + ',末尾からデキュー後 linkedListDeque = ');
|
||||
linkedListDeque.print();
|
||||
const popFirst = linkedListDeque.popFirst();
|
||||
console.log('先頭からデキューした要素 = ' + popFirst + ',先頭からデキュー後 linkedListDeque = ');
|
||||
linkedListDeque.print();
|
||||
|
||||
/* 両端キューの長さを取得 */
|
||||
const size = linkedListDeque.size();
|
||||
console.log('両端キューの長さ size = ' + size);
|
||||
|
||||
/* 両端キューが空かどうかを判定 */
|
||||
const isEmpty = linkedListDeque.isEmpty();
|
||||
console.log('両端キューが空かどうか = ' + isEmpty);
|
||||
@@ -0,0 +1,99 @@
|
||||
/**
|
||||
* File: linkedlist_queue.js
|
||||
* Created Time: 2022-12-20
|
||||
* Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com)
|
||||
*/
|
||||
|
||||
const { ListNode } = require('../modules/ListNode');
|
||||
|
||||
/* 連結リストベースのキュー */
|
||||
class LinkedListQueue {
|
||||
#front; // 先頭ノード #front
|
||||
#rear; // 末尾ノード #rear
|
||||
#queSize = 0;
|
||||
|
||||
constructor() {
|
||||
this.#front = null;
|
||||
this.#rear = null;
|
||||
}
|
||||
|
||||
/* キューの長さを取得 */
|
||||
get size() {
|
||||
return this.#queSize;
|
||||
}
|
||||
|
||||
/* キューが空かどうかを判定 */
|
||||
isEmpty() {
|
||||
return this.size === 0;
|
||||
}
|
||||
|
||||
/* エンキュー */
|
||||
push(num) {
|
||||
// 末尾ノードの後ろに num を追加
|
||||
const node = new ListNode(num);
|
||||
// キューが空なら、先頭・末尾ノードをともにそのノードに設定
|
||||
if (!this.#front) {
|
||||
this.#front = node;
|
||||
this.#rear = node;
|
||||
// キューが空でなければ、そのノードを末尾ノードの後ろに追加
|
||||
} else {
|
||||
this.#rear.next = node;
|
||||
this.#rear = node;
|
||||
}
|
||||
this.#queSize++;
|
||||
}
|
||||
|
||||
/* デキュー */
|
||||
pop() {
|
||||
const num = this.peek();
|
||||
// 先頭ノードを削除
|
||||
this.#front = this.#front.next;
|
||||
this.#queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* キュー先頭の要素にアクセス */
|
||||
peek() {
|
||||
if (this.size === 0) throw new Error('キューが空');
|
||||
return this.#front.val;
|
||||
}
|
||||
|
||||
/* 連結リストを Array に変換して返す */
|
||||
toArray() {
|
||||
let node = this.#front;
|
||||
const res = new Array(this.size);
|
||||
for (let i = 0; i < res.length; i++) {
|
||||
res[i] = node.val;
|
||||
node = node.next;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* キューを初期化 */
|
||||
const queue = new LinkedListQueue();
|
||||
|
||||
/* 要素をエンキュー */
|
||||
queue.push(1);
|
||||
queue.push(3);
|
||||
queue.push(2);
|
||||
queue.push(5);
|
||||
queue.push(4);
|
||||
console.log('キュー queue = ' + queue.toArray());
|
||||
|
||||
/* キュー先頭の要素にアクセス */
|
||||
const peek = queue.peek();
|
||||
console.log('先頭要素 peek = ' + peek);
|
||||
|
||||
/* 要素をデキュー */
|
||||
const pop = queue.pop();
|
||||
console.log('デキューした要素 pop = ' + pop + ',デキュー後 queue = ' + queue.toArray());
|
||||
|
||||
/* キューの長さを取得 */
|
||||
const size = queue.size;
|
||||
console.log('キューの長さ size = ' + size);
|
||||
|
||||
/* キューが空かどうかを判定 */
|
||||
const isEmpty = queue.isEmpty();
|
||||
console.log('キューは空か = ' + isEmpty);
|
||||
@@ -0,0 +1,88 @@
|
||||
/**
|
||||
* File: linkedlist_stack.js
|
||||
* Created Time: 2022-12-22
|
||||
* Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com)
|
||||
*/
|
||||
|
||||
const { ListNode } = require('../modules/ListNode');
|
||||
|
||||
/* 連結リストベースのスタック */
|
||||
class LinkedListStack {
|
||||
#stackPeek; // 先頭ノードをスタックトップとする
|
||||
#stkSize = 0; // スタックの長さ
|
||||
|
||||
constructor() {
|
||||
this.#stackPeek = null;
|
||||
}
|
||||
|
||||
/* スタックの長さを取得 */
|
||||
get size() {
|
||||
return this.#stkSize;
|
||||
}
|
||||
|
||||
/* スタックが空かどうかを判定 */
|
||||
isEmpty() {
|
||||
return this.size === 0;
|
||||
}
|
||||
|
||||
/* プッシュ */
|
||||
push(num) {
|
||||
const node = new ListNode(num);
|
||||
node.next = this.#stackPeek;
|
||||
this.#stackPeek = node;
|
||||
this.#stkSize++;
|
||||
}
|
||||
|
||||
/* ポップ */
|
||||
pop() {
|
||||
const num = this.peek();
|
||||
this.#stackPeek = this.#stackPeek.next;
|
||||
this.#stkSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* スタックトップの要素にアクセス */
|
||||
peek() {
|
||||
if (!this.#stackPeek) throw new Error('スタックが空');
|
||||
return this.#stackPeek.val;
|
||||
}
|
||||
|
||||
/* 連結リストを Array に変換して返す */
|
||||
toArray() {
|
||||
let node = this.#stackPeek;
|
||||
const res = new Array(this.size);
|
||||
for (let i = res.length - 1; i >= 0; i--) {
|
||||
res[i] = node.val;
|
||||
node = node.next;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* スタックを初期化 */
|
||||
const stack = new LinkedListStack();
|
||||
|
||||
/* 要素をプッシュ */
|
||||
stack.push(1);
|
||||
stack.push(3);
|
||||
stack.push(2);
|
||||
stack.push(5);
|
||||
stack.push(4);
|
||||
console.log('スタック stack = ' + stack.toArray());
|
||||
|
||||
/* スタックトップの要素にアクセス */
|
||||
const peek = stack.peek();
|
||||
console.log('スタックトップ要素 peek = ' + peek);
|
||||
|
||||
/* 要素をポップ */
|
||||
const pop = stack.pop();
|
||||
console.log('ポップした要素 pop = ' + pop + ',ポップ後 stack = ' + stack.toArray());
|
||||
|
||||
/* スタックの長さを取得 */
|
||||
const size = stack.size;
|
||||
console.log('スタックの長さ size = ' + size);
|
||||
|
||||
/* 空かどうかを判定 */
|
||||
const isEmpty = stack.isEmpty();
|
||||
console.log('スタックは空か = ' + isEmpty);
|
||||
35
ja/codes/javascript/chapter_stack_and_queue/queue.js
Normal file
35
ja/codes/javascript/chapter_stack_and_queue/queue.js
Normal file
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* File: queue.js
|
||||
* Created Time: 2022-12-05
|
||||
* Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com)
|
||||
*/
|
||||
|
||||
/* Driver Code */
|
||||
/* キューを初期化 */
|
||||
// JavaScript には組み込みのキューがないため、Array をキューとして使う
|
||||
const queue = [];
|
||||
|
||||
/* 要素をエンキュー */
|
||||
queue.push(1);
|
||||
queue.push(3);
|
||||
queue.push(2);
|
||||
queue.push(5);
|
||||
queue.push(4);
|
||||
console.log('キュー queue =', queue);
|
||||
|
||||
/* キュー先頭の要素にアクセス */
|
||||
const peek = queue[0];
|
||||
console.log('先頭要素 peek =', peek);
|
||||
|
||||
/* 要素をデキュー */
|
||||
// 基盤が配列であるため、shift() メソッドの時間計算量は O(n)
|
||||
const pop = queue.shift();
|
||||
console.log('デキューした要素 pop =', pop, ',デキュー後 queue = ', queue);
|
||||
|
||||
/* キューの長さを取得 */
|
||||
const size = queue.length;
|
||||
console.log('キューの長さ size =', size);
|
||||
|
||||
/* キューが空かどうかを判定 */
|
||||
const isEmpty = queue.length === 0;
|
||||
console.log('キューは空か = ', isEmpty);
|
||||
35
ja/codes/javascript/chapter_stack_and_queue/stack.js
Normal file
35
ja/codes/javascript/chapter_stack_and_queue/stack.js
Normal file
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* File: stack.js
|
||||
* Created Time: 2022-12-04
|
||||
* Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com)
|
||||
*/
|
||||
|
||||
/* Driver Code */
|
||||
/* スタックを初期化 */
|
||||
// JavaScript には組み込みのスタッククラスがないため、Array をスタックとして使う
|
||||
const stack = [];
|
||||
|
||||
/* 要素をプッシュ */
|
||||
stack.push(1);
|
||||
stack.push(3);
|
||||
stack.push(2);
|
||||
stack.push(5);
|
||||
stack.push(4);
|
||||
console.log('スタック stack =', stack);
|
||||
|
||||
/* スタックトップの要素にアクセス */
|
||||
const peek = stack[stack.length - 1];
|
||||
console.log('スタックトップ要素 peek =', peek);
|
||||
|
||||
/* 要素をポップ */
|
||||
const pop = stack.pop();
|
||||
console.log('ポップした要素 pop =', pop);
|
||||
console.log('ポップ後 stack =', stack);
|
||||
|
||||
/* スタックの長さを取得 */
|
||||
const size = stack.length;
|
||||
console.log('スタックの長さ size =', size);
|
||||
|
||||
/* 空かどうかを判定 */
|
||||
const isEmpty = stack.length === 0;
|
||||
console.log('スタックは空か =', isEmpty);
|
||||
Reference in New Issue
Block a user