mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-27 17:33:23 +08:00
Migrate init_array, hash_map_operations, hash_map_traversal, queue_operations, deque_operations and insert_remove_node: zh and ru demo sources gain region markers and the snippets' comments, en, ja and ko are regenerated from reviewed translations, zh-hant via OpenCC. 70 blocks become region references; builtin_hash and five blocks without reviewed translations stay literal. The Java, Kotlin and Rust deque demos follow the book's order, and a few demo comments and typos are aligned with the snippets. Co-Authored-By: Claude Opus 5.5 <[email protected]>
61 lines
2.3 KiB
C#
61 lines
2.3 KiB
C#
/**
|
|
* File: deque.cs
|
|
* Created Time: 2022-12-30
|
|
* Author: moonache ([email protected])
|
|
*/
|
|
|
|
namespace hello_algo.chapter_stack_and_queue;
|
|
|
|
public class deque {
|
|
[Test]
|
|
public void Test() {
|
|
// [region]{init_deque}
|
|
/* 両端キューを初期化する */
|
|
// C# では、連結リスト LinkedList を両端キューとして使う
|
|
LinkedList<int> deque = new();
|
|
// [endregion]{init_deque}
|
|
|
|
// [region]{push}
|
|
/* 要素をエンキューする */
|
|
deque.AddLast(2); // キューの末尾に追加する
|
|
deque.AddLast(5);
|
|
deque.AddLast(4);
|
|
deque.AddFirst(3); // キューの先頭に追加する
|
|
deque.AddFirst(1);
|
|
// [endregion]{push}
|
|
Console.WriteLine("両端キュー deque = " + string.Join(",", deque));
|
|
|
|
// [region]{peek_first}
|
|
/* 要素にアクセスする */
|
|
int? peekFirst = deque.First?.Value; // キューの先頭要素
|
|
// [endregion]{peek_first}
|
|
Console.WriteLine("キューの先頭要素 peekFirst = " + peekFirst);
|
|
// [region]{peek_last}
|
|
int? peekLast = deque.Last?.Value; // キューの末尾要素
|
|
// [endregion]{peek_last}
|
|
Console.WriteLine("キューの末尾要素 peekLast = " + peekLast);
|
|
|
|
// [region]{pop_first}
|
|
/* 要素をデキューする */
|
|
deque.RemoveFirst(); // キューの先頭要素をデキューする
|
|
// [endregion]{pop_first}
|
|
Console.WriteLine("先頭要素をデキューした後 deque = " + string.Join(",", deque));
|
|
// [region]{pop_last}
|
|
deque.RemoveLast(); // キューの末尾要素をデキューする
|
|
// [endregion]{pop_last}
|
|
Console.WriteLine("末尾要素をデキューした後 deque = " + string.Join(",", deque));
|
|
|
|
// [region]{size}
|
|
/* 両端キューの長さを取得する */
|
|
int size = deque.Count;
|
|
// [endregion]{size}
|
|
Console.WriteLine("両端キューの長さ size = " + size);
|
|
|
|
// [region]{is_empty}
|
|
/* 両端キューが空かどうかを判定する */
|
|
bool isEmpty = deque.Count == 0;
|
|
// [endregion]{is_empty}
|
|
Console.WriteLine("両端キューは空か = " + isEmpty);
|
|
}
|
|
}
|