Queue data structure enables you to use FIFO [ First IN First OUT ] approach. Whatever data comes first goes first. It is also called LILO [ Last IN Last OUT ]. For I/O, it provides you two functions.
Enqueue [ To insert data into queue ]
Dequeue [ To delete the data from the queue ]
Have you ever thought about where this queue concept is being used?
It is quite famous to solve the producer-consumer problem.
How to create your own queue from scratch? The below code is just a sample of creating and utilizing a queue.
Drawbacks /-
For this solution, one can notice that queue is not being utilized in an optimized way. If Queue is full and even if one runs dequeue operation which will eventually empty one space but still enqueue operation can’t be performed since rear points to last. Take this below example. After enqueue(4), Queue is full but after dequeue() operation we have one space available in the queue but can not run the enqueue() operation as rear points to the last index.
In order to optimized this solution, we can do a cyclic queue approach also known as Ring Buffer.