mirror of
https://github.com/krahets/hello-algo.git
synced 2026-02-12 15:15:48 +08:00
docs: add Japanese translate documents (#1812)
* docs: add Japanese documents (`ja/docs`) * docs: add Japanese documents (`ja/codes`) * docs: add Japanese documents * Remove pythontutor blocks in ja/ * Add an empty at the end of each markdown file. * Add the missing figures (use the English version temporarily). * Add index.md for Japanese version. * Add index.html for Japanese version. * Add missing index.assets * Fix backtracking_algorithm.md for Japanese version. * Add avatar_eltociear.jpg. Fix image links on the Japanese landing page. * Add the Japanese banner. --------- Co-authored-by: krahets <krahets@163.com>
This commit is contained in:
committed by
GitHub
parent
2487a27036
commit
954c45864b
156
ja/codes/cpp/chapter_stack_and_queue/array_deque.cpp
Normal file
156
ja/codes/cpp/chapter_stack_and_queue/array_deque.cpp
Normal file
@@ -0,0 +1,156 @@
|
||||
/**
|
||||
* File: array_deque.cpp
|
||||
* Created Time: 2023-03-02
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* 循環配列に基づく両端キュークラス */
|
||||
class ArrayDeque {
|
||||
private:
|
||||
vector<int> nums; // 両端キューの要素を格納する配列
|
||||
int front; // 先頭ポインタ、先頭要素を指す
|
||||
int queSize; // 両端キューの長さ
|
||||
|
||||
public:
|
||||
/* コンストラクタ */
|
||||
ArrayDeque(int capacity) {
|
||||
nums.resize(capacity);
|
||||
front = queSize = 0;
|
||||
}
|
||||
|
||||
/* 両端キューの容量を取得 */
|
||||
int capacity() {
|
||||
return nums.size();
|
||||
}
|
||||
|
||||
/* 両端キューの長さを取得 */
|
||||
int size() {
|
||||
return queSize;
|
||||
}
|
||||
|
||||
/* 両端キューが空かどうかを判定 */
|
||||
bool isEmpty() {
|
||||
return queSize == 0;
|
||||
}
|
||||
|
||||
/* 循環配列のインデックスを計算 */
|
||||
int index(int i) {
|
||||
// 剰余演算で循環配列を実現
|
||||
// iが配列の末尾を超えた場合、先頭に戻る
|
||||
// iが配列の先頭を超えた場合、末尾に戻る
|
||||
return (i + capacity()) % capacity();
|
||||
}
|
||||
|
||||
/* 先頭エンキュー */
|
||||
void pushFirst(int num) {
|
||||
if (queSize == capacity()) {
|
||||
cout << "Double-ended queue is full" << endl;
|
||||
return;
|
||||
}
|
||||
// 先頭ポインタを1つ左に移動
|
||||
// 剰余演算でfrontが配列の先頭を越えて末尾に戻ることを実現
|
||||
front = index(front - 1);
|
||||
// numを先頭に追加
|
||||
nums[front] = num;
|
||||
queSize++;
|
||||
}
|
||||
|
||||
/* 末尾エンキュー */
|
||||
void pushLast(int num) {
|
||||
if (queSize == capacity()) {
|
||||
cout << "Double-ended queue is full" << endl;
|
||||
return;
|
||||
}
|
||||
// 末尾ポインタを計算、末尾インデックス + 1を指す
|
||||
int rear = index(front + queSize);
|
||||
// numを末尾に追加
|
||||
nums[rear] = num;
|
||||
queSize++;
|
||||
}
|
||||
|
||||
/* 先頭デキュー */
|
||||
int popFirst() {
|
||||
int num = peekFirst();
|
||||
// 先頭ポインタを1つ後ろに移動
|
||||
front = index(front + 1);
|
||||
queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* 末尾デキュー */
|
||||
int popLast() {
|
||||
int num = peekLast();
|
||||
queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* 先頭要素にアクセス */
|
||||
int peekFirst() {
|
||||
if (isEmpty())
|
||||
throw out_of_range("Double-ended queue is empty");
|
||||
return nums[front];
|
||||
}
|
||||
|
||||
/* 末尾要素にアクセス */
|
||||
int peekLast() {
|
||||
if (isEmpty())
|
||||
throw out_of_range("Double-ended queue is empty");
|
||||
// 末尾要素のインデックスを計算
|
||||
int last = index(front + queSize - 1);
|
||||
return nums[last];
|
||||
}
|
||||
|
||||
/* 印刷用に配列を返却 */
|
||||
vector<int> toVector() {
|
||||
// 有効な長さ範囲内の要素のみを変換
|
||||
vector<int> res(queSize);
|
||||
for (int i = 0, j = front; i < queSize; i++, j++) {
|
||||
res[i] = nums[index(j)];
|
||||
}
|
||||
return res;
|
||||
}
|
||||
};
|
||||
|
||||
/* ドライバーコード */
|
||||
int main() {
|
||||
/* 両端キューを初期化 */
|
||||
ArrayDeque *deque = new ArrayDeque(10);
|
||||
deque->pushLast(3);
|
||||
deque->pushLast(2);
|
||||
deque->pushLast(5);
|
||||
cout << "Double-ended queue deque = ";
|
||||
printVector(deque->toVector());
|
||||
|
||||
/* 要素にアクセス */
|
||||
int peekFirst = deque->peekFirst();
|
||||
cout << "Front element peekFirst = " << peekFirst << endl;
|
||||
int peekLast = deque->peekLast();
|
||||
cout << "Back element peekLast = " << peekLast << endl;
|
||||
|
||||
/* 要素エンキュー */
|
||||
deque->pushLast(4);
|
||||
cout << "Element 4 enqueued at the tail, deque = ";
|
||||
printVector(deque->toVector());
|
||||
deque->pushFirst(1);
|
||||
cout << "Element 1 enqueued at the head, deque = ";
|
||||
printVector(deque->toVector());
|
||||
|
||||
/* 要素デキュー */
|
||||
int popLast = deque->popLast();
|
||||
cout << "Deque tail element = " << popLast << ", after dequeuing from the tail";
|
||||
printVector(deque->toVector());
|
||||
int popFirst = deque->popFirst();
|
||||
cout << "Deque front element = " << popFirst << ", after dequeuing from the front";
|
||||
printVector(deque->toVector());
|
||||
|
||||
/* 両端キューの長さを取得 */
|
||||
int size = deque->size();
|
||||
cout << "Length of the double-ended queue size = " << size << endl;
|
||||
|
||||
/* 両端キューが空かどうかを判定 */
|
||||
bool isEmpty = deque->isEmpty();
|
||||
cout << "Is the double-ended queue empty = " << boolalpha << isEmpty << endl;
|
||||
return 0;
|
||||
}
|
||||
129
ja/codes/cpp/chapter_stack_and_queue/array_queue.cpp
Normal file
129
ja/codes/cpp/chapter_stack_and_queue/array_queue.cpp
Normal file
@@ -0,0 +1,129 @@
|
||||
/**
|
||||
* File: array_queue.cpp
|
||||
* Created Time: 2022-11-25
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* 循環配列に基づくキュークラス */
|
||||
class ArrayQueue {
|
||||
private:
|
||||
int *nums; // キュー要素を格納する配列
|
||||
int front; // 先頭ポインタ、先頭要素を指す
|
||||
int queSize; // キューの長さ
|
||||
int queCapacity; // キューの容量
|
||||
|
||||
public:
|
||||
ArrayQueue(int capacity) {
|
||||
// 配列を初期化
|
||||
nums = new int[capacity];
|
||||
queCapacity = capacity;
|
||||
front = queSize = 0;
|
||||
}
|
||||
|
||||
~ArrayQueue() {
|
||||
delete[] nums;
|
||||
}
|
||||
|
||||
/* キューの容量を取得 */
|
||||
int capacity() {
|
||||
return queCapacity;
|
||||
}
|
||||
|
||||
/* キューの長さを取得 */
|
||||
int size() {
|
||||
return queSize;
|
||||
}
|
||||
|
||||
/* キューが空かどうかを判定 */
|
||||
bool isEmpty() {
|
||||
return size() == 0;
|
||||
}
|
||||
|
||||
/* エンキュー */
|
||||
void push(int num) {
|
||||
if (queSize == queCapacity) {
|
||||
cout << "Queue is full" << endl;
|
||||
return;
|
||||
}
|
||||
// 末尾ポインタを計算、末尾インデックス + 1を指す
|
||||
// 剰余演算を使用して末尾ポインタが配列の末尾から先頭に戻るようにラップ
|
||||
int rear = (front + queSize) % queCapacity;
|
||||
// numを末尾に追加
|
||||
nums[rear] = num;
|
||||
queSize++;
|
||||
}
|
||||
|
||||
/* デキュー */
|
||||
int pop() {
|
||||
int num = peek();
|
||||
// 先頭ポインタを1つ後ろに移動、末尾を超えた場合は配列の先頭に戻る
|
||||
front = (front + 1) % queCapacity;
|
||||
queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* 先頭要素にアクセス */
|
||||
int peek() {
|
||||
if (isEmpty())
|
||||
throw out_of_range("Queue is empty");
|
||||
return nums[front];
|
||||
}
|
||||
|
||||
/* 配列をVectorに変換して返却 */
|
||||
vector<int> toVector() {
|
||||
// 有効な長さ範囲内の要素のみを変換
|
||||
vector<int> arr(queSize);
|
||||
for (int i = 0, j = front; i < queSize; i++, j++) {
|
||||
arr[i] = nums[j % queCapacity];
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
};
|
||||
|
||||
/* ドライバーコード */
|
||||
int main() {
|
||||
/* キューを初期化 */
|
||||
int capacity = 10;
|
||||
ArrayQueue *queue = new ArrayQueue(capacity);
|
||||
|
||||
/* 要素エンキュー */
|
||||
queue->push(1);
|
||||
queue->push(3);
|
||||
queue->push(2);
|
||||
queue->push(5);
|
||||
queue->push(4);
|
||||
cout << "Queue queue = ";
|
||||
printVector(queue->toVector());
|
||||
|
||||
/* 先頭要素にアクセス */
|
||||
int peek = queue->peek();
|
||||
cout << "Front element peek = " << peek << endl;
|
||||
|
||||
/* 要素デキュー */
|
||||
peek = queue->pop();
|
||||
cout << "Element dequeued = " << peek << ", after dequeuing";
|
||||
printVector(queue->toVector());
|
||||
|
||||
/* キューの長さを取得 */
|
||||
int size = queue->size();
|
||||
cout << "Length of the queue size = " << size << endl;
|
||||
|
||||
/* キューが空かどうかを判定 */
|
||||
bool empty = queue->isEmpty();
|
||||
cout << "Is the queue empty = " << empty << endl;
|
||||
|
||||
/* 循環配列をテスト */
|
||||
for (int i = 0; i < 10; i++) {
|
||||
queue->push(i);
|
||||
queue->pop();
|
||||
cout << "After the " << i << "th round of enqueueing + dequeuing, queue = ";
|
||||
printVector(queue->toVector());
|
||||
}
|
||||
|
||||
// メモリを解放
|
||||
delete queue;
|
||||
|
||||
return 0;
|
||||
}
|
||||
85
ja/codes/cpp/chapter_stack_and_queue/array_stack.cpp
Normal file
85
ja/codes/cpp/chapter_stack_and_queue/array_stack.cpp
Normal file
@@ -0,0 +1,85 @@
|
||||
/**
|
||||
* File: array_stack.cpp
|
||||
* Created Time: 2022-11-28
|
||||
* Author: qualifier1024 (2539244001@qq.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* 配列に基づくスタッククラス */
|
||||
class ArrayStack {
|
||||
private:
|
||||
vector<int> stack;
|
||||
|
||||
public:
|
||||
/* スタックの長さを取得 */
|
||||
int size() {
|
||||
return stack.size();
|
||||
}
|
||||
|
||||
/* スタックが空かどうかを判定 */
|
||||
bool isEmpty() {
|
||||
return stack.size() == 0;
|
||||
}
|
||||
|
||||
/* プッシュ */
|
||||
void push(int num) {
|
||||
stack.push_back(num);
|
||||
}
|
||||
|
||||
/* ポップ */
|
||||
int pop() {
|
||||
int num = top();
|
||||
stack.pop_back();
|
||||
return num;
|
||||
}
|
||||
|
||||
/* スタックトップ要素にアクセス */
|
||||
int top() {
|
||||
if (isEmpty())
|
||||
throw out_of_range("Stack is empty");
|
||||
return stack.back();
|
||||
}
|
||||
|
||||
/* Vectorを返却 */
|
||||
vector<int> toVector() {
|
||||
return stack;
|
||||
}
|
||||
};
|
||||
|
||||
/* ドライバーコード */
|
||||
int main() {
|
||||
/* スタックを初期化 */
|
||||
ArrayStack *stack = new ArrayStack();
|
||||
|
||||
/* 要素プッシュ */
|
||||
stack->push(1);
|
||||
stack->push(3);
|
||||
stack->push(2);
|
||||
stack->push(5);
|
||||
stack->push(4);
|
||||
cout << "Stack stack = ";
|
||||
printVector(stack->toVector());
|
||||
|
||||
/* スタックトップ要素にアクセス */
|
||||
int top = stack->top();
|
||||
cout << "Top element of the stack top = " << top << endl;
|
||||
|
||||
/* 要素ポップ */
|
||||
top = stack->pop();
|
||||
cout << "Element popped from the stack = " << top << ", after popping";
|
||||
printVector(stack->toVector());
|
||||
|
||||
/* スタックの長さを取得 */
|
||||
int size = stack->size();
|
||||
cout << "Length of the stack size = " << size << endl;
|
||||
|
||||
/* 空かどうかを判定 */
|
||||
bool empty = stack->isEmpty();
|
||||
cout << "Is the stack empty = " << empty << endl;
|
||||
|
||||
// メモリを解放
|
||||
delete stack;
|
||||
|
||||
return 0;
|
||||
}
|
||||
46
ja/codes/cpp/chapter_stack_and_queue/deque.cpp
Normal file
46
ja/codes/cpp/chapter_stack_and_queue/deque.cpp
Normal file
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
* File: deque.cpp
|
||||
* Created Time: 2022-11-25
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* ドライバーコード */
|
||||
int main() {
|
||||
/* 両端キューを初期化 */
|
||||
deque<int> deque;
|
||||
|
||||
/* 要素エンキュー */
|
||||
deque.push_back(2);
|
||||
deque.push_back(5);
|
||||
deque.push_back(4);
|
||||
deque.push_front(3);
|
||||
deque.push_front(1);
|
||||
cout << "Double-ended queue deque = ";
|
||||
printDeque(deque);
|
||||
|
||||
/* 要素にアクセス */
|
||||
int front = deque.front();
|
||||
cout << "Front element of the queue front = " << front << endl;
|
||||
int back = deque.back();
|
||||
cout << "Back element of the queue back = " << back << endl;
|
||||
|
||||
/* 要素デキュー */
|
||||
deque.pop_front();
|
||||
cout << "Front element dequeued = " << front << ", after dequeuing from the front";
|
||||
printDeque(deque);
|
||||
deque.pop_back();
|
||||
cout << "Back element dequeued = " << back << ", after dequeuing from the back";
|
||||
printDeque(deque);
|
||||
|
||||
/* 両端キューの長さを取得 */
|
||||
int size = deque.size();
|
||||
cout << "Length of the double-ended queue size = " << size << endl;
|
||||
|
||||
/* 両端キューが空かどうかを判定 */
|
||||
bool empty = deque.empty();
|
||||
cout << "Is the double-ended queue empty = " << empty << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
194
ja/codes/cpp/chapter_stack_and_queue/linkedlist_deque.cpp
Normal file
194
ja/codes/cpp/chapter_stack_and_queue/linkedlist_deque.cpp
Normal file
@@ -0,0 +1,194 @@
|
||||
/**
|
||||
* File: linkedlist_deque.cpp
|
||||
* Created Time: 2023-03-02
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* 双方向連結リストノード */
|
||||
struct DoublyListNode {
|
||||
int val; // ノードの値
|
||||
DoublyListNode *next; // 後続ノードへのポインタ
|
||||
DoublyListNode *prev; // 前続ノードへのポインタ
|
||||
DoublyListNode(int val) : val(val), prev(nullptr), next(nullptr) {
|
||||
}
|
||||
};
|
||||
|
||||
/* 双方向連結リストに基づく両端キュークラス */
|
||||
class LinkedListDeque {
|
||||
private:
|
||||
DoublyListNode *front, *rear; // 先頭ノードfront、末尾ノードrear
|
||||
int queSize = 0; // 両端キューの長さ
|
||||
|
||||
public:
|
||||
/* コンストラクタ */
|
||||
LinkedListDeque() : front(nullptr), rear(nullptr) {
|
||||
}
|
||||
|
||||
/* デストラクタ */
|
||||
~LinkedListDeque() {
|
||||
// 連結リストを走査、ノードを削除、メモリを解放
|
||||
DoublyListNode *pre, *cur = front;
|
||||
while (cur != nullptr) {
|
||||
pre = cur;
|
||||
cur = cur->next;
|
||||
delete pre;
|
||||
}
|
||||
}
|
||||
|
||||
/* 両端キューの長さを取得 */
|
||||
int size() {
|
||||
return queSize;
|
||||
}
|
||||
|
||||
/* 両端キューが空かどうかを判定 */
|
||||
bool isEmpty() {
|
||||
return size() == 0;
|
||||
}
|
||||
|
||||
/* エンキュー操作 */
|
||||
void push(int num, bool isFront) {
|
||||
DoublyListNode *node = new DoublyListNode(num);
|
||||
// リストが空の場合、frontとrearの両方をnodeに向ける
|
||||
if (isEmpty())
|
||||
front = rear = node;
|
||||
// 先頭エンキュー操作
|
||||
else if (isFront) {
|
||||
// ノードをリストの先頭に追加
|
||||
front->prev = node;
|
||||
node->next = front;
|
||||
front = node; // 先頭ノードを更新
|
||||
// 末尾エンキュー操作
|
||||
} else {
|
||||
// ノードをリストの末尾に追加
|
||||
rear->next = node;
|
||||
node->prev = rear;
|
||||
rear = node; // 末尾ノードを更新
|
||||
}
|
||||
queSize++; // キュー長を更新
|
||||
}
|
||||
|
||||
/* 先頭エンキュー */
|
||||
void pushFirst(int num) {
|
||||
push(num, true);
|
||||
}
|
||||
|
||||
/* 末尾エンキュー */
|
||||
void pushLast(int num) {
|
||||
push(num, false);
|
||||
}
|
||||
|
||||
/* デキュー操作 */
|
||||
int pop(bool isFront) {
|
||||
if (isEmpty())
|
||||
throw out_of_range("Queue is empty");
|
||||
int val;
|
||||
// 先頭デキュー操作
|
||||
if (isFront) {
|
||||
val = front->val; // 先頭ノードの値を一時保存
|
||||
// 先頭ノードを削除
|
||||
DoublyListNode *fNext = front->next;
|
||||
if (fNext != nullptr) {
|
||||
fNext->prev = nullptr;
|
||||
front->next = nullptr;
|
||||
}
|
||||
delete front;
|
||||
front = fNext; // 先頭ノードを更新
|
||||
// 末尾デキュー操作
|
||||
} else {
|
||||
val = rear->val; // 末尾ノードの値を一時保存
|
||||
// 末尾ノードを削除
|
||||
DoublyListNode *rPrev = rear->prev;
|
||||
if (rPrev != nullptr) {
|
||||
rPrev->next = nullptr;
|
||||
rear->prev = nullptr;
|
||||
}
|
||||
delete rear;
|
||||
rear = rPrev; // 末尾ノードを更新
|
||||
}
|
||||
queSize--; // キュー長を更新
|
||||
return val;
|
||||
}
|
||||
|
||||
/* 先頭デキュー */
|
||||
int popFirst() {
|
||||
return pop(true);
|
||||
}
|
||||
|
||||
/* 末尾デキュー */
|
||||
int popLast() {
|
||||
return pop(false);
|
||||
}
|
||||
|
||||
/* 先頭要素にアクセス */
|
||||
int peekFirst() {
|
||||
if (isEmpty())
|
||||
throw out_of_range("Double-ended queue is empty");
|
||||
return front->val;
|
||||
}
|
||||
|
||||
/* 末尾要素にアクセス */
|
||||
int peekLast() {
|
||||
if (isEmpty())
|
||||
throw out_of_range("Double-ended queue is empty");
|
||||
return rear->val;
|
||||
}
|
||||
|
||||
/* 印刷用に配列を返却 */
|
||||
vector<int> toVector() {
|
||||
DoublyListNode *node = front;
|
||||
vector<int> res(size());
|
||||
for (int i = 0; i < res.size(); i++) {
|
||||
res[i] = node->val;
|
||||
node = node->next;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
};
|
||||
|
||||
/* ドライバーコード */
|
||||
int main() {
|
||||
/* 両端キューを初期化 */
|
||||
LinkedListDeque *deque = new LinkedListDeque();
|
||||
deque->pushLast(3);
|
||||
deque->pushLast(2);
|
||||
deque->pushLast(5);
|
||||
cout << "Double-ended queue deque = ";
|
||||
printVector(deque->toVector());
|
||||
|
||||
/* 要素にアクセス */
|
||||
int peekFirst = deque->peekFirst();
|
||||
cout << "Front element peekFirst = " << peekFirst << endl;
|
||||
int peekLast = deque->peekLast();
|
||||
cout << "Back element peekLast = " << peekLast << endl;
|
||||
|
||||
/* 要素エンキュー */
|
||||
deque->pushLast(4);
|
||||
cout << "Element 4 rear enqueued, deque =";
|
||||
printVector(deque->toVector());
|
||||
deque->pushFirst(1);
|
||||
cout << "Element 1 enqueued at the head, deque = ";
|
||||
printVector(deque->toVector());
|
||||
|
||||
/* 要素デキュー */
|
||||
int popLast = deque->popLast();
|
||||
cout << "Deque tail element = " << popLast << ", after dequeuing from the tail";
|
||||
printVector(deque->toVector());
|
||||
int popFirst = deque->popFirst();
|
||||
cout << "Deque front element = " << popFirst << ", after dequeuing from the front";
|
||||
printVector(deque->toVector());
|
||||
|
||||
/* 両端キューの長さを取得 */
|
||||
int size = deque->size();
|
||||
cout << "Length of the double-ended queue size = " << size << endl;
|
||||
|
||||
/* 両端キューが空かどうかを判定 */
|
||||
bool isEmpty = deque->isEmpty();
|
||||
cout << "Is the double-ended queue empty = " << boolalpha << isEmpty << endl;
|
||||
|
||||
// メモリを解放
|
||||
delete deque;
|
||||
|
||||
return 0;
|
||||
}
|
||||
120
ja/codes/cpp/chapter_stack_and_queue/linkedlist_queue.cpp
Normal file
120
ja/codes/cpp/chapter_stack_and_queue/linkedlist_queue.cpp
Normal file
@@ -0,0 +1,120 @@
|
||||
/**
|
||||
* File: linkedlist_queue.cpp
|
||||
* Created Time: 2022-11-25
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* 連結リストに基づくキュークラス */
|
||||
class LinkedListQueue {
|
||||
private:
|
||||
ListNode *front, *rear; // 先頭ノードfront、末尾ノードrear
|
||||
int queSize;
|
||||
|
||||
public:
|
||||
LinkedListQueue() {
|
||||
front = nullptr;
|
||||
rear = nullptr;
|
||||
queSize = 0;
|
||||
}
|
||||
|
||||
~LinkedListQueue() {
|
||||
// 連結リストを走査、ノードを削除、メモリを解放
|
||||
freeMemoryLinkedList(front);
|
||||
}
|
||||
|
||||
/* キューの長さを取得 */
|
||||
int size() {
|
||||
return queSize;
|
||||
}
|
||||
|
||||
/* キューが空かどうかを判定 */
|
||||
bool isEmpty() {
|
||||
return queSize == 0;
|
||||
}
|
||||
|
||||
/* エンキュー */
|
||||
void push(int num) {
|
||||
// 末尾ノードの後ろにnumを追加
|
||||
ListNode *node = new ListNode(num);
|
||||
// キューが空の場合、先頭と末尾ノードの両方をそのノードに向ける
|
||||
if (front == nullptr) {
|
||||
front = node;
|
||||
rear = node;
|
||||
}
|
||||
// キューが空でない場合、そのノードを末尾ノードの後ろに追加
|
||||
else {
|
||||
rear->next = node;
|
||||
rear = node;
|
||||
}
|
||||
queSize++;
|
||||
}
|
||||
|
||||
/* デキュー */
|
||||
int pop() {
|
||||
int num = peek();
|
||||
// 先頭ノードを削除
|
||||
ListNode *tmp = front;
|
||||
front = front->next;
|
||||
// メモリを解放
|
||||
delete tmp;
|
||||
queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* 先頭要素にアクセス */
|
||||
int peek() {
|
||||
if (size() == 0)
|
||||
throw out_of_range("Queue is empty");
|
||||
return front->val;
|
||||
}
|
||||
|
||||
/* 連結リストをVectorに変換して返却 */
|
||||
vector<int> toVector() {
|
||||
ListNode *node = front;
|
||||
vector<int> res(size());
|
||||
for (int i = 0; i < res.size(); i++) {
|
||||
res[i] = node->val;
|
||||
node = node->next;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
};
|
||||
|
||||
/* ドライバーコード */
|
||||
int main() {
|
||||
/* キューを初期化 */
|
||||
LinkedListQueue *queue = new LinkedListQueue();
|
||||
|
||||
/* 要素エンキュー */
|
||||
queue->push(1);
|
||||
queue->push(3);
|
||||
queue->push(2);
|
||||
queue->push(5);
|
||||
queue->push(4);
|
||||
cout << "Queue queue = ";
|
||||
printVector(queue->toVector());
|
||||
|
||||
/* 先頭要素にアクセス */
|
||||
int peek = queue->peek();
|
||||
cout << "Front element peek = " << peek << endl;
|
||||
|
||||
/* 要素デキュー */
|
||||
peek = queue->pop();
|
||||
cout << "Element dequeued = " << peek << ", after dequeuing";
|
||||
printVector(queue->toVector());
|
||||
|
||||
/* キューの長さを取得 */
|
||||
int size = queue->size();
|
||||
cout << "Length of the queue size = " << size << endl;
|
||||
|
||||
/* キューが空かどうかを判定 */
|
||||
bool empty = queue->isEmpty();
|
||||
cout << "Is the queue empty = " << empty << endl;
|
||||
|
||||
// メモリを解放
|
||||
delete queue;
|
||||
|
||||
return 0;
|
||||
}
|
||||
109
ja/codes/cpp/chapter_stack_and_queue/linkedlist_stack.cpp
Normal file
109
ja/codes/cpp/chapter_stack_and_queue/linkedlist_stack.cpp
Normal file
@@ -0,0 +1,109 @@
|
||||
/**
|
||||
* File: linkedlist_stack.cpp
|
||||
* Created Time: 2022-11-28
|
||||
* Author: qualifier1024 (2539244001@qq.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* 連結リストに基づくスタッククラス */
|
||||
class LinkedListStack {
|
||||
private:
|
||||
ListNode *stackTop; // 先頭ノードをスタックトップとして使用
|
||||
int stkSize; // スタックの長さ
|
||||
|
||||
public:
|
||||
LinkedListStack() {
|
||||
stackTop = nullptr;
|
||||
stkSize = 0;
|
||||
}
|
||||
|
||||
~LinkedListStack() {
|
||||
// 連結リストを走査、ノードを削除、メモリを解放
|
||||
freeMemoryLinkedList(stackTop);
|
||||
}
|
||||
|
||||
/* スタックの長さを取得 */
|
||||
int size() {
|
||||
return stkSize;
|
||||
}
|
||||
|
||||
/* スタックが空かどうかを判定 */
|
||||
bool isEmpty() {
|
||||
return size() == 0;
|
||||
}
|
||||
|
||||
/* プッシュ */
|
||||
void push(int num) {
|
||||
ListNode *node = new ListNode(num);
|
||||
node->next = stackTop;
|
||||
stackTop = node;
|
||||
stkSize++;
|
||||
}
|
||||
|
||||
/* ポップ */
|
||||
int pop() {
|
||||
int num = top();
|
||||
ListNode *tmp = stackTop;
|
||||
stackTop = stackTop->next;
|
||||
// メモリを解放
|
||||
delete tmp;
|
||||
stkSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* スタックトップ要素にアクセス */
|
||||
int top() {
|
||||
if (isEmpty())
|
||||
throw out_of_range("Stack is empty");
|
||||
return stackTop->val;
|
||||
}
|
||||
|
||||
/* リストを配列に変換して返却 */
|
||||
vector<int> toVector() {
|
||||
ListNode *node = stackTop;
|
||||
vector<int> res(size());
|
||||
for (int i = res.size() - 1; i >= 0; i--) {
|
||||
res[i] = node->val;
|
||||
node = node->next;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
};
|
||||
|
||||
/* ドライバーコード */
|
||||
int main() {
|
||||
/* スタックを初期化 */
|
||||
LinkedListStack *stack = new LinkedListStack();
|
||||
|
||||
/* 要素プッシュ */
|
||||
stack->push(1);
|
||||
stack->push(3);
|
||||
stack->push(2);
|
||||
stack->push(5);
|
||||
stack->push(4);
|
||||
cout << "Stack stack = ";
|
||||
printVector(stack->toVector());
|
||||
|
||||
/* スタックトップ要素にアクセス */
|
||||
int top = stack->top();
|
||||
cout << "Top element of the stack top = " << top << endl;
|
||||
|
||||
/* 要素ポップ */
|
||||
top = stack->pop();
|
||||
cout << "Element popped from the stack = " << top << ", after popping";
|
||||
printVector(stack->toVector());
|
||||
|
||||
/* スタックの長さを取得 */
|
||||
int size = stack->size();
|
||||
cout << "Length of the stack size = " << size << endl;
|
||||
|
||||
/* 空かどうかを判定 */
|
||||
bool empty = stack->isEmpty();
|
||||
cout << "Is the stack empty = " << empty << endl;
|
||||
|
||||
// メモリを解放
|
||||
delete stack;
|
||||
|
||||
return 0;
|
||||
}
|
||||
41
ja/codes/cpp/chapter_stack_and_queue/queue.cpp
Normal file
41
ja/codes/cpp/chapter_stack_and_queue/queue.cpp
Normal file
@@ -0,0 +1,41 @@
|
||||
/**
|
||||
* File: queue.cpp
|
||||
* Created Time: 2022-11-28
|
||||
* Author: qualifier1024 (2539244001@qq.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* ドライバーコード */
|
||||
int main() {
|
||||
/* キューを初期化 */
|
||||
queue<int> queue;
|
||||
|
||||
/* 要素エンキュー */
|
||||
queue.push(1);
|
||||
queue.push(3);
|
||||
queue.push(2);
|
||||
queue.push(5);
|
||||
queue.push(4);
|
||||
cout << "Queue queue = ";
|
||||
printQueue(queue);
|
||||
|
||||
/* 先頭要素にアクセス */
|
||||
int front = queue.front();
|
||||
cout << "Front element of the queue front = " << front << endl;
|
||||
|
||||
/* 要素デキュー */
|
||||
queue.pop();
|
||||
cout << "Element dequeued = " << front << ", after dequeuing";
|
||||
printQueue(queue);
|
||||
|
||||
/* キューの長さを取得 */
|
||||
int size = queue.size();
|
||||
cout << "Length of the queue size = " << size << endl;
|
||||
|
||||
/* キューが空かどうかを判定 */
|
||||
bool empty = queue.empty();
|
||||
cout << "Is the queue empty = " << empty << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
41
ja/codes/cpp/chapter_stack_and_queue/stack.cpp
Normal file
41
ja/codes/cpp/chapter_stack_and_queue/stack.cpp
Normal file
@@ -0,0 +1,41 @@
|
||||
/**
|
||||
* File: stack.cpp
|
||||
* Created Time: 2022-11-28
|
||||
* Author: qualifier1024 (2539244001@qq.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* ドライバーコード */
|
||||
int main() {
|
||||
/* スタックを初期化 */
|
||||
stack<int> stack;
|
||||
|
||||
/* 要素プッシュ */
|
||||
stack.push(1);
|
||||
stack.push(3);
|
||||
stack.push(2);
|
||||
stack.push(5);
|
||||
stack.push(4);
|
||||
cout << "Stack stack = ";
|
||||
printStack(stack);
|
||||
|
||||
/* スタックトップ要素にアクセス */
|
||||
int top = stack.top();
|
||||
cout << "Top element of the stack top = " << top << endl;
|
||||
|
||||
/* 要素ポップ */
|
||||
stack.pop(); // 戻り値なし
|
||||
cout << "Element popped from the stack = " << top << ", after popping";
|
||||
printStack(stack);
|
||||
|
||||
/* スタックの長さを取得 */
|
||||
int size = stack.size();
|
||||
cout << "Length of the stack size = " << size << endl;
|
||||
|
||||
/* 空かどうかを判定 */
|
||||
bool empty = stack.empty();
|
||||
cout << "Is the stack empty = " << empty << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user