mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-03 18:50:58 +08:00
完善所以c#相关的文档和代码
This commit is contained in:
41
codes/csharp/chapter_stack_and_queue/queue.cs
Normal file
41
codes/csharp/chapter_stack_and_queue/queue.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using System;
|
||||
using hello_algo.include;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace hello_algo.chapter_stack_and_queue
|
||||
{
|
||||
public class queue
|
||||
{
|
||||
[Test]
|
||||
public void Test()
|
||||
{
|
||||
/* 初始化队列 */
|
||||
Queue<int> queue = new();
|
||||
|
||||
/* 元素入队 */
|
||||
queue.Enqueue(1);
|
||||
queue.Enqueue(3);
|
||||
queue.Enqueue(2);
|
||||
queue.Enqueue(5);
|
||||
queue.Enqueue(4);
|
||||
Console.WriteLine("队列 queue = " + String.Join(",", queue.ToArray()));
|
||||
|
||||
/* 访问队首元素 */
|
||||
int peek = queue.Peek();
|
||||
Console.WriteLine("队首元素 peek = " + peek);
|
||||
|
||||
/* 元素出队 */
|
||||
int poll = queue.Dequeue();
|
||||
Console.WriteLine("出队元素 poll = " + poll + ",出队后 queue = " + String.Join(",", queue.ToArray()));
|
||||
|
||||
/* 获取队列的长度 */
|
||||
int size = queue.Count();
|
||||
Console.WriteLine("队列长度 size = " + size);
|
||||
|
||||
/* 判断队列是否为空 */
|
||||
bool isEmpty = queue.Count() == 0;
|
||||
Console.WriteLine("队列是否为空 = " + isEmpty);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user