Skip to content

Commit

Permalink
Merge pull request krahets#193 from Wonderdch/patch-2
Browse files Browse the repository at this point in the history
添加 deque.cs
  • Loading branch information
krahets authored Dec 30, 2022
2 parents 2465db1 + c67363a commit 11c2bc1
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions codes/csharp/chapter_stack_and_queue/deque.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* File: deque.cs
* Created Time: 2022-12-30
* Author: moonache ([email protected])
*/

using NUnit.Framework;

namespace hello_algo.chapter_stack_and_queue
{
public class deque
{
[Test]
public void Test()
{
/* 初始化双向队列 */
// 在 C# 中,将链表 LinkedList 看作双向队列来使用
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);
}
}
}

0 comments on commit 11c2bc1

Please sign in to comment.