hello-algo/codes/csharp/chapter_stack_and_queue/array_queue.cs

132 lines
3.5 KiB
Java
Raw Normal View History

/**
* File: array_queue.cs
* Created Time: 2022-12-23
* Author: haptear (haptear@hotmail.com)
*/
2022-12-23 15:42:02 +08:00
using NUnit.Framework;
namespace hello_algo.chapter_stack_and_queue
{
/* 基于环形数组实现的队列 */
2022-12-24 17:05:58 +08:00
class ArrayQueue
{
2023-02-01 03:23:29 +08:00
private int[] nums; // 用于存储队列元素的数组
private int front; // 队首指针,指向队首元素
private int queSize; // 队列长度
2022-12-23 15:42:02 +08:00
2022-12-24 17:05:58 +08:00
public ArrayQueue(int capacity)
{
2022-12-23 15:42:02 +08:00
nums = new int[capacity];
2023-02-01 03:23:29 +08:00
front = queSize = 0;
2022-12-23 15:42:02 +08:00
}
/* 获取队列的容量 */
2022-12-24 17:05:58 +08:00
public int capacity()
{
2022-12-23 15:42:02 +08:00
return nums.Length;
}
/* 获取队列的长度 */
2022-12-24 17:05:58 +08:00
public int size()
{
2023-02-01 03:23:29 +08:00
return queSize;
2022-12-23 15:42:02 +08:00
}
/* 判断队列是否为空 */
2022-12-24 17:05:58 +08:00
public bool isEmpty()
{
2023-02-01 03:23:29 +08:00
return queSize == 0;
2022-12-23 15:42:02 +08:00
}
/* 入队 */
2022-12-24 17:05:58 +08:00
public void offer(int num)
{
2023-02-01 03:23:29 +08:00
if (queSize == capacity())
2022-12-24 17:05:58 +08:00
{
2022-12-23 15:42:02 +08:00
Console.WriteLine("队列已满");
return;
}
2023-02-01 03:23:29 +08:00
// 计算尾指针,指向队尾索引 + 1
// 通过取余操作,实现 rear 越过数组尾部后回到头部
int rear = (front + queSize) % capacity();
2022-12-23 15:42:02 +08:00
// 尾结点后添加 num
nums[rear] = num;
2023-02-01 03:23:29 +08:00
queSize++;
2022-12-23 15:42:02 +08:00
}
/* 出队 */
2022-12-24 17:05:58 +08:00
public int poll()
{
2022-12-23 15:42:02 +08:00
int num = peek();
2023-02-01 03:23:29 +08:00
// 队首指针向后移动一位,若越过尾部则返回到数组头部
2022-12-23 15:42:02 +08:00
front = (front + 1) % capacity();
2023-02-01 03:23:29 +08:00
queSize--;
2022-12-23 15:42:02 +08:00
return num;
}
/* 访问队首元素 */
2022-12-24 17:05:58 +08:00
public int peek()
{
2022-12-23 15:42:02 +08:00
if (isEmpty())
throw new Exception();
return nums[front];
}
/* 返回数组 */
2022-12-24 17:05:58 +08:00
public int[] toArray()
{
2022-12-23 15:42:02 +08:00
// 仅转换有效长度范围内的列表元素
2023-02-01 03:23:29 +08:00
int[] res = new int[queSize];
for (int i = 0, j = front; i < queSize; i++, j++)
2022-12-24 17:05:58 +08:00
{
2023-02-01 03:23:29 +08:00
res[i] = nums[j % this.capacity()];
2022-12-23 15:42:02 +08:00
}
return res;
}
}
2022-12-24 17:05:58 +08:00
public class array_queue
{
2022-12-23 15:42:02 +08:00
[Test]
public void Test()
{
/* 初始化队列 */
int capacity = 10;
ArrayQueue queue = new ArrayQueue(capacity);
/* 元素入队 */
queue.offer(1);
queue.offer(3);
queue.offer(2);
queue.offer(5);
queue.offer(4);
2022-12-24 17:05:58 +08:00
Console.WriteLine("队列 queue = " + string.Join(",", queue.toArray()));
2022-12-23 15:42:02 +08:00
/* 访问队首元素 */
int peek = queue.peek();
Console.WriteLine("队首元素 peek = " + peek);
/* 元素出队 */
int poll = queue.poll();
Console.WriteLine("出队元素 poll = " + poll + ",出队后 queue = " + string.Join(",", queue.toArray()));
/* 获取队列的长度 */
int size = queue.size();
Console.WriteLine("队列长度 size = " + size);
/* 判断队列是否为空 */
bool isEmpty = queue.isEmpty();
Console.WriteLine("队列是否为空 = " + isEmpty);
/* 测试环形数组 */
2022-12-24 17:05:58 +08:00
for (int i = 0; i < 10; i++)
{
2022-12-23 15:42:02 +08:00
queue.offer(i);
queue.poll();
Console.WriteLine("" + i + " 轮入队 + 出队后 queue = " + string.Join(",", queue.toArray()));
}
}
}
}