A queue is a linear data structure that serves as a collection of elements, with three main operations.
The queue is also known as First–In, First–Out (FIFO) data structure considering the order in which elements come off a queue, i.e., the first element inserted into the queue is the first one to be removed. Following is a simple representation of a FIFO queue: Circular Queue Implementation using an array: There are several efficient implementations of FIFO queues. A (bounded) queue can be easily implemented using an array using a five elements structure: structure stack: item : array maxsize : integer front : integer rear : integer size : integer Since fixed-length arrays have limited capacity, we need to convert the array into a closed circle. If n is the array’s size, then computing indices modulo n will turn the array into a circle. Now, front and rear can drift around endlessly in that circle, making it unnecessary to move items stored in the array. Following is the C program that demonstrates it:
Download Run Code
It is possible to implement a queue that can grow or shrink as much as needed using a dynamic array. For example, using std::vector in C++ or ArrayList in Java. Applications of a Queue:
Thanks for reading. Please use our online compiler to post code in comments using C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages. Like us? Refer us to your friends and help us grow. Happy coding 🙂 |