mirror of
https://github.com/krahets/hello-algo.git
synced 2026-06-16 15:18:37 +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:
152
ja/codes/csharp/chapter_stack_and_queue/array_deque.cs
Normal file
152
ja/codes/csharp/chapter_stack_and_queue/array_deque.cs
Normal file
@@ -0,0 +1,152 @@
|
||||
/**
|
||||
* File: array_deque.cs
|
||||
* Created Time: 2023-03-08
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_stack_and_queue;
|
||||
|
||||
/* 循環配列ベースの両端キュー */
|
||||
public class ArrayDeque {
|
||||
int[] nums; // 両端キューの要素を格納する配列
|
||||
int front; // 先頭ポインタ。先頭要素を指す
|
||||
int queSize; // 両端キューの長さ
|
||||
|
||||
/* コンストラクタ */
|
||||
public ArrayDeque(int capacity) {
|
||||
nums = new int[capacity];
|
||||
front = queSize = 0;
|
||||
}
|
||||
|
||||
/* 両端キューの容量を取得 */
|
||||
int Capacity() {
|
||||
return nums.Length;
|
||||
}
|
||||
|
||||
/* 両端キューの長さを取得 */
|
||||
public int Size() {
|
||||
return queSize;
|
||||
}
|
||||
|
||||
/* 両端キューが空かどうかを判定 */
|
||||
public bool IsEmpty() {
|
||||
return queSize == 0;
|
||||
}
|
||||
|
||||
/* 循環配列のインデックスを計算 */
|
||||
int Index(int i) {
|
||||
// 剰余演算により配列の先頭と末尾をつなげる
|
||||
// i が配列の末尾を越えたら先頭に戻る
|
||||
// i が配列の先頭を越えて前に出たら末尾に戻る
|
||||
return (i + Capacity()) % Capacity();
|
||||
}
|
||||
|
||||
/* キュー先頭にエンキュー */
|
||||
public void PushFirst(int num) {
|
||||
if (queSize == Capacity()) {
|
||||
Console.WriteLine("両端キューは満杯です");
|
||||
return;
|
||||
}
|
||||
// 先頭ポインタを左に 1 つ移動する
|
||||
// 剰余演算により、front が配列先頭を越えた後に末尾へ戻るようにする
|
||||
front = Index(front - 1);
|
||||
// num をキュー先頭に追加
|
||||
nums[front] = num;
|
||||
queSize++;
|
||||
}
|
||||
|
||||
/* キュー末尾にエンキュー */
|
||||
public void PushLast(int num) {
|
||||
if (queSize == Capacity()) {
|
||||
Console.WriteLine("両端キューは満杯です");
|
||||
return;
|
||||
}
|
||||
// キュー末尾ポインタを計算し、末尾インデックス + 1 を指す
|
||||
int rear = Index(front + queSize);
|
||||
// num をキュー末尾に追加
|
||||
nums[rear] = num;
|
||||
queSize++;
|
||||
}
|
||||
|
||||
/* キュー先頭からデキュー */
|
||||
public int PopFirst() {
|
||||
int num = PeekFirst();
|
||||
// 先頭ポインタを 1 つ後ろへ進める
|
||||
front = Index(front + 1);
|
||||
queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* キュー末尾からデキュー */
|
||||
public int PopLast() {
|
||||
int num = PeekLast();
|
||||
queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* キュー先頭の要素にアクセス */
|
||||
public int PeekFirst() {
|
||||
if (IsEmpty()) {
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
return nums[front];
|
||||
}
|
||||
|
||||
/* キュー末尾の要素にアクセス */
|
||||
public int PeekLast() {
|
||||
if (IsEmpty()) {
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
// 末尾要素のインデックスを計算
|
||||
int last = Index(front + queSize - 1);
|
||||
return nums[last];
|
||||
}
|
||||
|
||||
/* 出力用の配列を返す */
|
||||
public int[] ToArray() {
|
||||
// 有効長の範囲内のリスト要素のみを変換
|
||||
int[] res = new int[queSize];
|
||||
for (int i = 0, j = front; i < queSize; i++, j++) {
|
||||
res[i] = nums[Index(j)];
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
public class array_deque {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* 両端キューを初期化 */
|
||||
ArrayDeque deque = new(10);
|
||||
deque.PushLast(3);
|
||||
deque.PushLast(2);
|
||||
deque.PushLast(5);
|
||||
Console.WriteLine("双方向キュー deque = " + string.Join(" ", deque.ToArray()));
|
||||
|
||||
/* 要素にアクセス */
|
||||
int peekFirst = deque.PeekFirst();
|
||||
Console.WriteLine("先頭要素 peekFirst = " + peekFirst);
|
||||
int peekLast = deque.PeekLast();
|
||||
Console.WriteLine("末尾要素 peekLast = " + peekLast);
|
||||
|
||||
/* 要素をエンキュー */
|
||||
deque.PushLast(4);
|
||||
Console.WriteLine("要素 4 を末尾にエンキューした後 deque = " + string.Join(" ", deque.ToArray()));
|
||||
deque.PushFirst(1);
|
||||
Console.WriteLine("要素 1 を先頭にエンキューした後 deque = " + string.Join(" ", deque.ToArray()));
|
||||
|
||||
/* 要素をデキュー */
|
||||
int popLast = deque.PopLast();
|
||||
Console.WriteLine("末尾からデキューした要素 = " + popLast + "、末尾からデキューした後 deque = " + string.Join(" ", deque.ToArray()));
|
||||
int popFirst = deque.PopFirst();
|
||||
Console.WriteLine("先頭からデキューした要素 = " + popFirst + "、先頭からデキューした後 deque = " + string.Join(" ", deque.ToArray()));
|
||||
|
||||
/* 両端キューの長さを取得 */
|
||||
int size = deque.Size();
|
||||
Console.WriteLine("双方向キューの長さ size = " + size);
|
||||
|
||||
/* 両端キューが空かどうかを判定 */
|
||||
bool isEmpty = deque.IsEmpty();
|
||||
Console.WriteLine("双方向キューが空かどうか = " + isEmpty);
|
||||
}
|
||||
}
|
||||
114
ja/codes/csharp/chapter_stack_and_queue/array_queue.cs
Normal file
114
ja/codes/csharp/chapter_stack_and_queue/array_queue.cs
Normal file
@@ -0,0 +1,114 @@
|
||||
/**
|
||||
* File: array_queue.cs
|
||||
* Created Time: 2022-12-23
|
||||
* Author: haptear (haptear@hotmail.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_stack_and_queue;
|
||||
|
||||
/* 循環配列ベースのキュー */
|
||||
class ArrayQueue {
|
||||
int[] nums; // キュー要素を格納する配列
|
||||
int front; // 先頭ポインタ。先頭要素を指す
|
||||
int queSize; // キューの長さ
|
||||
|
||||
public ArrayQueue(int capacity) {
|
||||
nums = new int[capacity];
|
||||
front = queSize = 0;
|
||||
}
|
||||
|
||||
/* キューの容量を取得 */
|
||||
int Capacity() {
|
||||
return nums.Length;
|
||||
}
|
||||
|
||||
/* キューの長さを取得 */
|
||||
public int Size() {
|
||||
return queSize;
|
||||
}
|
||||
|
||||
/* キューが空かどうかを判定 */
|
||||
public bool IsEmpty() {
|
||||
return queSize == 0;
|
||||
}
|
||||
|
||||
/* エンキュー */
|
||||
public void Push(int num) {
|
||||
if (queSize == Capacity()) {
|
||||
Console.WriteLine("キューは満杯です");
|
||||
return;
|
||||
}
|
||||
// 末尾ポインタを計算し、末尾インデックス + 1 を指す
|
||||
// 剰余演算により、rear が配列末尾を越えた後に先頭へ戻るようにする
|
||||
int rear = (front + queSize) % Capacity();
|
||||
// num をキュー末尾に追加
|
||||
nums[rear] = num;
|
||||
queSize++;
|
||||
}
|
||||
|
||||
/* デキュー */
|
||||
public int Pop() {
|
||||
int num = Peek();
|
||||
// 先頭ポインタを1つ後ろへ進め、末尾を越えたら配列先頭に戻す
|
||||
front = (front + 1) % Capacity();
|
||||
queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* キュー先頭の要素にアクセス */
|
||||
public int Peek() {
|
||||
if (IsEmpty())
|
||||
throw new Exception();
|
||||
return nums[front];
|
||||
}
|
||||
|
||||
/* 配列を返す */
|
||||
public int[] ToArray() {
|
||||
// 有効長の範囲内のリスト要素のみを変換
|
||||
int[] res = new int[queSize];
|
||||
for (int i = 0, j = front; i < queSize; i++, j++) {
|
||||
res[i] = nums[j % this.Capacity()];
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
public class array_queue {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* キューを初期化 */
|
||||
int capacity = 10;
|
||||
ArrayQueue queue = new(capacity);
|
||||
|
||||
/* 要素をエンキュー */
|
||||
queue.Push(1);
|
||||
queue.Push(3);
|
||||
queue.Push(2);
|
||||
queue.Push(5);
|
||||
queue.Push(4);
|
||||
Console.WriteLine("キュー queue = " + string.Join(",", queue.ToArray()));
|
||||
|
||||
/* キュー先頭の要素にアクセス */
|
||||
int peek = queue.Peek();
|
||||
Console.WriteLine("先頭要素 peek = " + peek);
|
||||
|
||||
/* 要素をデキュー */
|
||||
int pop = queue.Pop();
|
||||
Console.WriteLine("デキューした要素 pop = " + pop + "、デキュー後の queue = " + string.Join(",", queue.ToArray()));
|
||||
|
||||
/* キューの長さを取得 */
|
||||
int size = queue.Size();
|
||||
Console.WriteLine("キューの長さ size = " + size);
|
||||
|
||||
/* キューが空かどうかを判定 */
|
||||
bool isEmpty = queue.IsEmpty();
|
||||
Console.WriteLine("キューが空かどうか = " + isEmpty);
|
||||
|
||||
/* 循環配列をテストする */
|
||||
for (int i = 0; i < 10; i++) {
|
||||
queue.Push(i);
|
||||
queue.Pop();
|
||||
Console.WriteLine("第 " + i + " 回のエンキュー + デキュー後 queue = " + string.Join(",", queue.ToArray()));
|
||||
}
|
||||
}
|
||||
}
|
||||
84
ja/codes/csharp/chapter_stack_and_queue/array_stack.cs
Normal file
84
ja/codes/csharp/chapter_stack_and_queue/array_stack.cs
Normal file
@@ -0,0 +1,84 @@
|
||||
/**
|
||||
* File: array_stack.cs
|
||||
* Created Time: 2022-12-23
|
||||
* Author: haptear (haptear@hotmail.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_stack_and_queue;
|
||||
|
||||
/* 配列ベースのスタック */
|
||||
class ArrayStack {
|
||||
List<int> stack;
|
||||
public ArrayStack() {
|
||||
// リスト(動的配列)を初期化する
|
||||
stack = [];
|
||||
}
|
||||
|
||||
/* スタックの長さを取得 */
|
||||
public int Size() {
|
||||
return stack.Count;
|
||||
}
|
||||
|
||||
/* スタックが空かどうかを判定 */
|
||||
public bool IsEmpty() {
|
||||
return Size() == 0;
|
||||
}
|
||||
|
||||
/* プッシュ */
|
||||
public void Push(int num) {
|
||||
stack.Add(num);
|
||||
}
|
||||
|
||||
/* ポップ */
|
||||
public int Pop() {
|
||||
if (IsEmpty())
|
||||
throw new Exception();
|
||||
var val = Peek();
|
||||
stack.RemoveAt(Size() - 1);
|
||||
return val;
|
||||
}
|
||||
|
||||
/* スタックトップの要素にアクセス */
|
||||
public int Peek() {
|
||||
if (IsEmpty())
|
||||
throw new Exception();
|
||||
return stack[Size() - 1];
|
||||
}
|
||||
|
||||
/* List を Array に変換して返す */
|
||||
public int[] ToArray() {
|
||||
return [.. stack];
|
||||
}
|
||||
}
|
||||
|
||||
public class array_stack {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* スタックを初期化 */
|
||||
ArrayStack stack = new();
|
||||
|
||||
/* 要素をプッシュ */
|
||||
stack.Push(1);
|
||||
stack.Push(3);
|
||||
stack.Push(2);
|
||||
stack.Push(5);
|
||||
stack.Push(4);
|
||||
Console.WriteLine("スタック stack = " + string.Join(",", stack.ToArray()));
|
||||
|
||||
/* スタックトップの要素にアクセス */
|
||||
int peek = stack.Peek();
|
||||
Console.WriteLine("スタックトップ要素 peek = " + peek);
|
||||
|
||||
/* 要素をポップ */
|
||||
int pop = stack.Pop();
|
||||
Console.WriteLine("ポップした要素 pop = " + pop + "、ポップ後の stack = " + string.Join(",", stack.ToArray()));
|
||||
|
||||
/* スタックの長さを取得 */
|
||||
int size = stack.Size();
|
||||
Console.WriteLine("スタックの長さ size = " + size);
|
||||
|
||||
/* 空かどうかを判定 */
|
||||
bool isEmpty = stack.IsEmpty();
|
||||
Console.WriteLine("スタックが空かどうか = " + isEmpty);
|
||||
}
|
||||
}
|
||||
44
ja/codes/csharp/chapter_stack_and_queue/deque.cs
Normal file
44
ja/codes/csharp/chapter_stack_and_queue/deque.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* File: deque.cs
|
||||
* Created Time: 2022-12-30
|
||||
* Author: moonache (microin1301@outlook.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_stack_and_queue;
|
||||
|
||||
public class deque {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* 両端キューを初期化 */
|
||||
// C# では、LinkedList を両端キューとして使う
|
||||
LinkedList<int> deque = new();
|
||||
|
||||
/* 要素をエンキュー */
|
||||
deque.AddLast(2); // 末尾に追加する
|
||||
deque.AddLast(5);
|
||||
deque.AddLast(4);
|
||||
deque.AddFirst(3); // 先頭に追加する
|
||||
deque.AddFirst(1);
|
||||
Console.WriteLine("双方向キュー deque = " + string.Join(",", deque));
|
||||
|
||||
/* 要素にアクセス */
|
||||
int? peekFirst = deque.First?.Value; // 先頭要素
|
||||
Console.WriteLine("先頭要素 peekFirst = " + peekFirst);
|
||||
int? peekLast = deque.Last?.Value; // 末尾要素
|
||||
Console.WriteLine("末尾要素 peekLast = " + peekLast);
|
||||
|
||||
/* 要素をデキュー */
|
||||
deque.RemoveFirst(); // 先頭要素を取り出す
|
||||
Console.WriteLine("先頭要素をデキューした後 deque = " + string.Join(",", deque));
|
||||
deque.RemoveLast(); // 末尾要素を取り出す
|
||||
Console.WriteLine("末尾要素をデキューした後 deque = " + string.Join(",", deque));
|
||||
|
||||
/* 両端キューの長さを取得 */
|
||||
int size = deque.Count;
|
||||
Console.WriteLine("双方向キューの長さ size = " + size);
|
||||
|
||||
/* 両端キューが空かどうかを判定 */
|
||||
bool isEmpty = deque.Count == 0;
|
||||
Console.WriteLine("双方向キューが空かどうか = " + isEmpty);
|
||||
}
|
||||
}
|
||||
177
ja/codes/csharp/chapter_stack_and_queue/linkedlist_deque.cs
Normal file
177
ja/codes/csharp/chapter_stack_and_queue/linkedlist_deque.cs
Normal file
@@ -0,0 +1,177 @@
|
||||
/**
|
||||
* File: linkedlist_deque.cs
|
||||
* Created Time: 2023-03-08
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_stack_and_queue;
|
||||
|
||||
/* 双方向連結リストノード */
|
||||
public class ListNode(int val) {
|
||||
public int val = val; // ノード値
|
||||
public ListNode? next = null; // 後続ノードへの参照
|
||||
public ListNode? prev = null; // 前駆ノードへの参照
|
||||
}
|
||||
|
||||
/* 双方向連結リストベースの両端キュー */
|
||||
public class LinkedListDeque {
|
||||
ListNode? front, rear; // 先頭ノード front、末尾ノード rear
|
||||
int queSize = 0; // 両端キューの長さ
|
||||
|
||||
public LinkedListDeque() {
|
||||
front = null;
|
||||
rear = null;
|
||||
}
|
||||
|
||||
/* 両端キューの長さを取得 */
|
||||
public int Size() {
|
||||
return queSize;
|
||||
}
|
||||
|
||||
/* 両端キューが空かどうかを判定 */
|
||||
public bool IsEmpty() {
|
||||
return Size() == 0;
|
||||
}
|
||||
|
||||
/* エンキュー操作 */
|
||||
void Push(int num, bool isFront) {
|
||||
ListNode node = new(num);
|
||||
// 連結リストが空なら、front と rear の両方を node に向ける
|
||||
if (IsEmpty()) {
|
||||
front = node;
|
||||
rear = node;
|
||||
}
|
||||
// 先頭へのエンキュー操作
|
||||
else if (isFront) {
|
||||
// node を連結リストの先頭に追加
|
||||
front!.prev = node;
|
||||
node.next = front;
|
||||
front = node; // 先頭ノードを更新する
|
||||
}
|
||||
// 末尾へのエンキュー操作
|
||||
else {
|
||||
// node を連結リストの末尾に追加
|
||||
rear!.next = node;
|
||||
node.prev = rear;
|
||||
rear = node; // 末尾ノードを更新する
|
||||
}
|
||||
|
||||
queSize++; // キューの長さを更新
|
||||
}
|
||||
|
||||
/* キュー先頭にエンキュー */
|
||||
public void PushFirst(int num) {
|
||||
Push(num, true);
|
||||
}
|
||||
|
||||
/* キュー末尾にエンキュー */
|
||||
public void PushLast(int num) {
|
||||
Push(num, false);
|
||||
}
|
||||
|
||||
/* デキュー操作 */
|
||||
int? Pop(bool isFront) {
|
||||
if (IsEmpty())
|
||||
throw new Exception();
|
||||
int? val;
|
||||
// キュー先頭からの取り出し
|
||||
if (isFront) {
|
||||
val = front?.val; // 先頭ノードの値を一時保存
|
||||
// 先頭ノードを削除
|
||||
ListNode? fNext = front?.next;
|
||||
if (fNext != null) {
|
||||
fNext.prev = null;
|
||||
front!.next = null;
|
||||
}
|
||||
front = fNext; // 先頭ノードを更新する
|
||||
}
|
||||
// キュー末尾からの取り出し
|
||||
else {
|
||||
val = rear?.val; // 末尾ノードの値を一時保存
|
||||
// 末尾ノードを削除
|
||||
ListNode? rPrev = rear?.prev;
|
||||
if (rPrev != null) {
|
||||
rPrev.next = null;
|
||||
rear!.prev = null;
|
||||
}
|
||||
rear = rPrev; // 末尾ノードを更新する
|
||||
}
|
||||
|
||||
queSize--; // キューの長さを更新
|
||||
return val;
|
||||
}
|
||||
|
||||
/* キュー先頭からデキュー */
|
||||
public int? PopFirst() {
|
||||
return Pop(true);
|
||||
}
|
||||
|
||||
/* キュー末尾からデキュー */
|
||||
public int? PopLast() {
|
||||
return Pop(false);
|
||||
}
|
||||
|
||||
/* キュー先頭の要素にアクセス */
|
||||
public int? PeekFirst() {
|
||||
if (IsEmpty())
|
||||
throw new Exception();
|
||||
return front?.val;
|
||||
}
|
||||
|
||||
/* キュー末尾の要素にアクセス */
|
||||
public int? PeekLast() {
|
||||
if (IsEmpty())
|
||||
throw new Exception();
|
||||
return rear?.val;
|
||||
}
|
||||
|
||||
/* 出力用の配列を返す */
|
||||
public int?[] ToArray() {
|
||||
ListNode? node = front;
|
||||
int?[] res = new int?[Size()];
|
||||
for (int i = 0; i < res.Length; i++) {
|
||||
res[i] = node?.val;
|
||||
node = node?.next;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
public class linkedlist_deque {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* 両端キューを初期化 */
|
||||
LinkedListDeque deque = new();
|
||||
deque.PushLast(3);
|
||||
deque.PushLast(2);
|
||||
deque.PushLast(5);
|
||||
Console.WriteLine("双方向キュー deque = " + string.Join(" ", deque.ToArray()));
|
||||
|
||||
/* 要素にアクセス */
|
||||
int? peekFirst = deque.PeekFirst();
|
||||
Console.WriteLine("先頭要素 peekFirst = " + peekFirst);
|
||||
int? peekLast = deque.PeekLast();
|
||||
Console.WriteLine("末尾要素 peekLast = " + peekLast);
|
||||
|
||||
/* 要素をエンキュー */
|
||||
deque.PushLast(4);
|
||||
Console.WriteLine("要素 4 を末尾にエンキューした後 deque = " + string.Join(" ", deque.ToArray()));
|
||||
deque.PushFirst(1);
|
||||
Console.WriteLine("要素 1 を先頭にエンキューした後 deque = " + string.Join(" ", deque.ToArray()));
|
||||
|
||||
/* 要素をデキュー */
|
||||
int? popLast = deque.PopLast();
|
||||
Console.WriteLine("末尾からデキューした要素 = " + popLast + "、末尾からデキューした後 deque = " + string.Join(" ", deque.ToArray()));
|
||||
int? popFirst = deque.PopFirst();
|
||||
Console.WriteLine("先頭からデキューした要素 = " + popFirst + "、先頭からデキューした後 deque = " + string.Join(" ", deque.ToArray()));
|
||||
|
||||
/* 両端キューの長さを取得 */
|
||||
int size = deque.Size();
|
||||
Console.WriteLine("双方向キューの長さ size = " + size);
|
||||
|
||||
/* 両端キューが空かどうかを判定 */
|
||||
bool isEmpty = deque.IsEmpty();
|
||||
Console.WriteLine("双方向キューが空かどうか = " + isEmpty);
|
||||
}
|
||||
}
|
||||
106
ja/codes/csharp/chapter_stack_and_queue/linkedlist_queue.cs
Normal file
106
ja/codes/csharp/chapter_stack_and_queue/linkedlist_queue.cs
Normal file
@@ -0,0 +1,106 @@
|
||||
/**
|
||||
* File: linkedlist_queue.cs
|
||||
* Created Time: 2022-12-23
|
||||
* Author: haptear (haptear@hotmail.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_stack_and_queue;
|
||||
|
||||
/* 連結リストベースのキュー */
|
||||
class LinkedListQueue {
|
||||
ListNode? front, rear; // 先頭ノード front、末尾ノード rear
|
||||
int queSize = 0;
|
||||
|
||||
public LinkedListQueue() {
|
||||
front = null;
|
||||
rear = null;
|
||||
}
|
||||
|
||||
/* キューの長さを取得 */
|
||||
public int Size() {
|
||||
return queSize;
|
||||
}
|
||||
|
||||
/* キューが空かどうかを判定 */
|
||||
public bool IsEmpty() {
|
||||
return Size() == 0;
|
||||
}
|
||||
|
||||
/* エンキュー */
|
||||
public void Push(int num) {
|
||||
// 末尾ノードの後ろに num を追加
|
||||
ListNode node = new(num);
|
||||
// キューが空なら、先頭・末尾ノードをともにそのノードに設定
|
||||
if (front == null) {
|
||||
front = node;
|
||||
rear = node;
|
||||
// キューが空でなければ、そのノードを末尾ノードの後ろに追加
|
||||
} else if (rear != null) {
|
||||
rear.next = node;
|
||||
rear = node;
|
||||
}
|
||||
queSize++;
|
||||
}
|
||||
|
||||
/* デキュー */
|
||||
public int Pop() {
|
||||
int num = Peek();
|
||||
// 先頭ノードを削除
|
||||
front = front?.next;
|
||||
queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* キュー先頭の要素にアクセス */
|
||||
public int Peek() {
|
||||
if (IsEmpty())
|
||||
throw new Exception();
|
||||
return front!.val;
|
||||
}
|
||||
|
||||
/* 連結リストを Array に変換して返す */
|
||||
public int[] ToArray() {
|
||||
if (front == null)
|
||||
return [];
|
||||
|
||||
ListNode? node = front;
|
||||
int[] res = new int[Size()];
|
||||
for (int i = 0; i < res.Length; i++) {
|
||||
res[i] = node!.val;
|
||||
node = node.next;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
public class linkedlist_queue {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* キューを初期化 */
|
||||
LinkedListQueue queue = new();
|
||||
|
||||
/* 要素をエンキュー */
|
||||
queue.Push(1);
|
||||
queue.Push(3);
|
||||
queue.Push(2);
|
||||
queue.Push(5);
|
||||
queue.Push(4);
|
||||
Console.WriteLine("キュー queue = " + string.Join(",", queue.ToArray()));
|
||||
|
||||
/* キュー先頭の要素にアクセス */
|
||||
int peek = queue.Peek();
|
||||
Console.WriteLine("先頭要素 peek = " + peek);
|
||||
|
||||
/* 要素をデキュー */
|
||||
int pop = queue.Pop();
|
||||
Console.WriteLine("デキューした要素 pop = " + pop + "、デキュー後の queue = " + string.Join(",", queue.ToArray()));
|
||||
|
||||
/* キューの長さを取得 */
|
||||
int size = queue.Size();
|
||||
Console.WriteLine("キューの長さ size = " + size);
|
||||
|
||||
/* キューが空かどうかを判定 */
|
||||
bool isEmpty = queue.IsEmpty();
|
||||
Console.WriteLine("キューが空かどうか = " + isEmpty);
|
||||
}
|
||||
}
|
||||
97
ja/codes/csharp/chapter_stack_and_queue/linkedlist_stack.cs
Normal file
97
ja/codes/csharp/chapter_stack_and_queue/linkedlist_stack.cs
Normal file
@@ -0,0 +1,97 @@
|
||||
/**
|
||||
* File: linkedlist_stack.cs
|
||||
* Created Time: 2022-12-23
|
||||
* Author: haptear (haptear@hotmail.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_stack_and_queue;
|
||||
|
||||
/* 連結リストベースのスタック */
|
||||
class LinkedListStack {
|
||||
ListNode? stackPeek; // 先頭ノードをスタックトップとする
|
||||
int stkSize = 0; // スタックの長さ
|
||||
|
||||
public LinkedListStack() {
|
||||
stackPeek = null;
|
||||
}
|
||||
|
||||
/* スタックの長さを取得 */
|
||||
public int Size() {
|
||||
return stkSize;
|
||||
}
|
||||
|
||||
/* スタックが空かどうかを判定 */
|
||||
public bool IsEmpty() {
|
||||
return Size() == 0;
|
||||
}
|
||||
|
||||
/* プッシュ */
|
||||
public void Push(int num) {
|
||||
ListNode node = new(num) {
|
||||
next = stackPeek
|
||||
};
|
||||
stackPeek = node;
|
||||
stkSize++;
|
||||
}
|
||||
|
||||
/* ポップ */
|
||||
public int Pop() {
|
||||
int num = Peek();
|
||||
stackPeek = stackPeek!.next;
|
||||
stkSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* スタックトップの要素にアクセス */
|
||||
public int Peek() {
|
||||
if (IsEmpty())
|
||||
throw new Exception();
|
||||
return stackPeek!.val;
|
||||
}
|
||||
|
||||
/* List を Array に変換して返す */
|
||||
public int[] ToArray() {
|
||||
if (stackPeek == null)
|
||||
return [];
|
||||
|
||||
ListNode? node = stackPeek;
|
||||
int[] res = new int[Size()];
|
||||
for (int i = res.Length - 1; i >= 0; i--) {
|
||||
res[i] = node!.val;
|
||||
node = node.next;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
public class linkedlist_stack {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* スタックを初期化 */
|
||||
LinkedListStack stack = new();
|
||||
|
||||
/* 要素をプッシュ */
|
||||
stack.Push(1);
|
||||
stack.Push(3);
|
||||
stack.Push(2);
|
||||
stack.Push(5);
|
||||
stack.Push(4);
|
||||
Console.WriteLine("スタック stack = " + string.Join(",", stack.ToArray()));
|
||||
|
||||
/* スタックトップの要素にアクセス */
|
||||
int peek = stack.Peek();
|
||||
Console.WriteLine("スタックトップ要素 peek = " + peek);
|
||||
|
||||
/* 要素をポップ */
|
||||
int pop = stack.Pop();
|
||||
Console.WriteLine("ポップした要素 pop = " + pop + "、ポップ後の stack = " + string.Join(",", stack.ToArray()));
|
||||
|
||||
/* スタックの長さを取得 */
|
||||
int size = stack.Size();
|
||||
Console.WriteLine("スタックの長さ size = " + size);
|
||||
|
||||
/* 空かどうかを判定 */
|
||||
bool isEmpty = stack.IsEmpty();
|
||||
Console.WriteLine("スタックが空かどうか = " + isEmpty);
|
||||
}
|
||||
}
|
||||
39
ja/codes/csharp/chapter_stack_and_queue/queue.cs
Normal file
39
ja/codes/csharp/chapter_stack_and_queue/queue.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* File: queue.cs
|
||||
* Created Time: 2022-12-23
|
||||
* Author: haptear (haptear@hotmail.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_stack_and_queue;
|
||||
|
||||
public class queue {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* キューを初期化 */
|
||||
Queue<int> queue = new();
|
||||
|
||||
/* 要素をエンキュー */
|
||||
queue.Enqueue(1);
|
||||
queue.Enqueue(3);
|
||||
queue.Enqueue(2);
|
||||
queue.Enqueue(5);
|
||||
queue.Enqueue(4);
|
||||
Console.WriteLine("キュー queue = " + string.Join(",", queue));
|
||||
|
||||
/* キュー先頭の要素にアクセス */
|
||||
int peek = queue.Peek();
|
||||
Console.WriteLine("先頭要素 peek = " + peek);
|
||||
|
||||
/* 要素をデキュー */
|
||||
int pop = queue.Dequeue();
|
||||
Console.WriteLine("デキューした要素 pop = " + pop + "、デキュー後の queue = " + string.Join(",", queue));
|
||||
|
||||
/* キューの長さを取得 */
|
||||
int size = queue.Count;
|
||||
Console.WriteLine("キューの長さ size = " + size);
|
||||
|
||||
/* キューが空かどうかを判定 */
|
||||
bool isEmpty = queue.Count == 0;
|
||||
Console.WriteLine("キューが空かどうか = " + isEmpty);
|
||||
}
|
||||
}
|
||||
40
ja/codes/csharp/chapter_stack_and_queue/stack.cs
Normal file
40
ja/codes/csharp/chapter_stack_and_queue/stack.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
/**
|
||||
* File: stack.cs
|
||||
* Created Time: 2022-12-23
|
||||
* Author: haptear (haptear@hotmail.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_stack_and_queue;
|
||||
|
||||
public class stack {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* スタックを初期化 */
|
||||
Stack<int> stack = new();
|
||||
|
||||
/* 要素をプッシュ */
|
||||
stack.Push(1);
|
||||
stack.Push(3);
|
||||
stack.Push(2);
|
||||
stack.Push(5);
|
||||
stack.Push(4);
|
||||
// 注意:stack.ToArray() で得られるのは逆順のシーケンスであり、インデックス 0 がスタックトップです
|
||||
Console.WriteLine("スタック stack = " + string.Join(",", stack));
|
||||
|
||||
/* スタックトップの要素にアクセス */
|
||||
int peek = stack.Peek();
|
||||
Console.WriteLine("スタックトップ要素 peek = " + peek);
|
||||
|
||||
/* 要素をポップ */
|
||||
int pop = stack.Pop();
|
||||
Console.WriteLine("ポップした要素 pop = " + pop + "、ポップ後の stack = " + string.Join(",", stack));
|
||||
|
||||
/* スタックの長さを取得 */
|
||||
int size = stack.Count;
|
||||
Console.WriteLine("スタックの長さ size = " + size);
|
||||
|
||||
/* 空かどうかを判定 */
|
||||
bool isEmpty = stack.Count == 0;
|
||||
Console.WriteLine("スタックが空かどうか = " + isEmpty);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user