添加 deque.cs

以 C# 内置的双向队列 LinkedList 为基础,编写了 C# 版本的 deque
pull/193/head
moonache 2 years ago committed by GitHub
parent 047e15e8d5
commit 0cf37e3f8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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<int> deque = new LinkedList<int>();
/* 元素入队 */
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);
}
}
}
Loading…
Cancel
Save