Does your company or startup need an expert? If so, write to me to talk about possibilities of professional cooperation.
PROGRAMMING on-line course is approaching! Write to me if you are interested in free early access.

MARCIN.com

Marcin Jamro, PhD, DSc

How to implement circular queue?

C#  |  .NET  |  Algorithms

A circular queue (a ring buffer) is a kind of queue that forms a circle, internally uses an array, as well as the maximum number of elements that can be placed inside the queue is limited. You need to specify two variables that indicate indices of the front and rear elements. The front one points to the element that will be dequeued first. The rear one points to the element that is the last in the queue.

Let's start with the implementation:

public class CircularQueue(int size)
    where T : struct
{
    private readonly T[] _items = new T[size];
    private int _front = -1;
    private int _rear = -1;
    private int _count = 0;
    public int Count { get { return _count; } }

    public bool Enqueue(T item)
    {
        if (_count == _items.Length) { return false; }

        if (_front < 0) { _front = _rear = 0; }
        else { _rear = ++_rear % _items.Length; }

        _items[_rear] = item;
        _count++;
        return true;
    }

    public T? Dequeue()
    {
        if (_count == 0) { return null; }

        T result = _items[_front];
        if (_front == _rear) { _front = _rear = -1; }
        else { _front = ++_front % _items.Length; }

        _count--;
        return result;
    }

    public T? Peek()
    {
        if (_count == 0) { return null; }
        return _items[_front];
    }
}

You can see how it works with the following code:

CircularQueue queue = new(8);
queue.Enqueue(2);
queue.Enqueue(-4);
queue.Enqueue(1);
queue.Enqueue(8);
queue.Enqueue(5);
int item = queue.Dequeue();
Console.WriteLine(item);

This entry can contain content, including parts of code, from one of my books, namely from the Second Edition of C# Data Structures and Algorithms, published by Packt Publishing in 2024. If you want to learn much more about a circular queue, as well as about other queues and other data structures in general, I encourage you to take a look at the mentioned book. You can buy it here.

The content, including any part of code, is presented without warranty, either express or implied. The author cannot be held liable for any damages caused or alleged to have been caused directly or indirectly by any content shown on this website.

Hello, I am Marcin

Reliable entrepreneur with 10+ years of companies operation, such as CEO at a few IT companies. I was an author of a few software & hardware products, still open to new ideas & cooperation.

Helpful expert with 10+ years of experience, together with PhD and DSc in Computer Science. I was an author of books and publications, as well as an expert in international projects.

Experienced developer with 10+ years of development and 100+ completed projects. I worked on various complex international projects, e.g. at Microsoft in USA and as CTO at a few companies. I have MCP, MCTS and MCPD certificates.

You can read more about me in my short bio. I am waiting for contact at [email protected], as well as at my Facebook and LinkedIn profiles.

Books

I am an author of a few books and numerous publications, also in high-quality international scientific journals.

If you want to learn data structures and algorithms in the context of C#, let's take a look at my newest book. It is the second edition of C# Data Structures and Algorithms. You can buy it here.

Projects

Do you like traveling? If so, discover an amazing world with local guides, right here and right now at: https://camaica.com

Travels

I love travels, learning new cultures and regions, meeting outstanding people, as well as taking pictures of beautiful places. Take a look at my travel diary.