Queue is a simple data structure that works on the simple principle of "First in First out" just like an ordinary queue at a coffee shop or ticket counters etc, where the first one to enter the queue gets served first.
In a queue, a new element is added from the rear, and existing elements are removed from the front.
As you can see in the diagram above, new elements are added from the back, and this function of adding a new element to the queue is called Enqueue. And elements are removed from the front, which is called Dequeue.
To learn more about how a queue works, chekout: Queue Data Structure
Hence, to create a simple queue data structure, we need to provide it with the following functions:
-
enqueue:
To add a new data element at the rear of the queue.
-
dequeue:
To remove an element from the front of the queue.
-
isEmpty:
To return True
if the queue is empty, else return False
.
-
size:
To check the size of the queue, in other words, count the number of elements in the queue and return it.
-
show:
To print all the queue elements.
We will be using Python List for implementing queue data structure.
NOTE: We can also use the dequeue
library to implement Queue in python and we can also use arrays.