From 0cf37e3f8eaeadb4c9c054828359e582e743d068 Mon Sep 17 00:00:00 2001 From: moonache <476681765@qq.com> Date: Fri, 30 Dec 2022 14:35:54 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20deque.cs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 以 C# 内置的双向队列 LinkedList 为基础,编写了 C# 版本的 deque --- codes/csharp/chapter_stack_and_queue/deque.cs | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 codes/csharp/chapter_stack_and_queue/deque.cs diff --git a/codes/csharp/chapter_stack_and_queue/deque.cs b/codes/csharp/chapter_stack_and_queue/deque.cs new file mode 100644 index 000000000..b1360e283 --- /dev/null +++ b/codes/csharp/chapter_stack_and_queue/deque.cs @@ -0,0 +1,48 @@ +/** + * File: deque.cs + * Created Time: 2022-12-30 + * Author: moonache (microin1301@outlook.com) + */ + +using NUnit.Framework; + +namespace hello_algo.chapter_stack_and_queue +{ + public class deque + { + [Test] + public void Test() + { + /* 初始化双向队列 */ + LinkedList deque = new LinkedList(); + + /* 元素入队 */ + deque.AddLast(2); // 添加至队尾 + deque.AddLast(5); + deque.AddLast(4); + deque.AddFirst(3); // 添加至队首 + deque.AddFirst(1); + Console.WriteLine("双向队列 deque = " + String.Join(",", deque.ToArray())); + + /* 访问元素 */ + 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.ToArray())); + deque.RemoveLast(); // 队尾元素出队 + Console.WriteLine("队尾元素出队后 deque = " + String.Join(",", deque.ToArray())); + + /* 获取双向队列的长度 */ + int size = deque.Count; + Console.WriteLine("双向队列长度 size = " + size); + + /* 判断双向队列是否为空 */ + bool isEmpty = deque.Count == 0; + Console.WriteLine("双向队列是否为空 = " + isEmpty); + } + } +} From c67363a78e84148c1ae44b258d6ae3aa4bc4086a Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Fri, 30 Dec 2022 16:10:22 +0800 Subject: [PATCH 2/2] Update deque.cs --- codes/csharp/chapter_stack_and_queue/deque.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/codes/csharp/chapter_stack_and_queue/deque.cs b/codes/csharp/chapter_stack_and_queue/deque.cs index b1360e283..d9f7a5098 100644 --- a/codes/csharp/chapter_stack_and_queue/deque.cs +++ b/codes/csharp/chapter_stack_and_queue/deque.cs @@ -14,6 +14,7 @@ namespace hello_algo.chapter_stack_and_queue public void Test() { /* 初始化双向队列 */ + // 在 C# 中,将链表 LinkedList 看作双向队列来使用 LinkedList deque = new LinkedList(); /* 元素入队 */