我需要制作一个可以跟踪出队元素的程序。我正在考虑使用 CircularQueue 类的三个实例,一个用于所有到达我的商店的顾客,另外两个用于在柜台 A 和柜台 B 服务下一位顾客。
private T[] array, arrayA, arrayB;
private int count, front, rear;
public CircularQueuing()
{
array = new T[6];
count = 0; front = 0; rear = -1;
}
public int Count
{
get { return count; }
}
public void Enqueue(T item)
{
if (count < array.Length)
{
rear = (rear + 1) % array.Length;
array[rear] = item;
count++;
}
else
throw new Exception("Queue is Full!");
}
public T Dequeue()
{
if (count > 0)
{
T item = array[front];
front = (front + 1) % array.Length;
count--;
return item;
}
else
throw new Exception("Queue is Empty!");
}
public T Peek()
{
if (count > 0)
{
T item = array[front];
return item;
}
else
throw new Exception("Queue is Empty!");
}
public void printQueue()
{
int counter = count;
int index = front;
while (counter > 0)
{
Console.WriteLine(array[index]);
index = (index + 1) % array.Length;
counter--;
}