Kalkwst / MicroLib

MIT License
3 stars 0 forks source link

:sparkles: Add Circular FIFO Queue Implementation #31

Open Kalkwst opened 1 year ago

Kalkwst commented 1 year ago

A circular FIFO (First In First Out) queue is a type of queue data structure that is similar to a regular queue but with the added feature of being able to "wrap around" when it reaches the end. This allows for more efficient use of memory, as the oldest element is overwritten when a new element is added to a full queue, instead of having to allocate additional memory for a new element.

From a more technical perspective, a circular FIFO queue is a data structure that follows the FIFO principle, where the first element added to the queue is the first one to be removed. The difference is that when the last position of the queue is reached, the next element will be added at the first position, overwriting the oldest element. The queue has two pointers, one pointing to the front of the queue and the other pointing to the next free position. When an element is dequeued, the front pointer is incremented, and when an element is enqueued, the next free position pointer is incremented, if the next free position pointer reaches the end of the queue, it wraps around to the first position. This way, the queue can use a fixed amount of memory and it's efficient in terms of memory usage and time complexity, as both enqueue and dequeue operations have constant time complexity O(1).