kratos00
kratos00

Reputation: 1

How can I display dequeued items in a Circular Queue?

I need to make a program that can keep track of dequeued elements. I was thinking of using three instances of the CircularQueue class, one for all customers as they arrive at the my shop, another two for serving the next customer at counter A and counter 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--;
        }

Upvotes: 0

Views: 449

Answers (1)

Sebeke
Sebeke

Reputation: 69

If you want to keep track of all the elements you dequeue, you can just store them in a list (you're talking about having multiple queues, so the list will probably have to be static) and for printing them, a simple foreach will suffice.

Upvotes: 1

Related Questions