Files
hello-algo/codes/csharp/chapter_stack_and_queue/deque.cs
T
Yudong JinandClaude Opus 5.5 eeb8a3b790 Cite demo-source regions from the remaining A-class snippets (batch 2) (#2035)
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]>
2026-11-16 04:23:45 +08:00

61 lines
2.0 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);
}
}