Skip to main content


queue works on the simple principle FIFO (First In First Out)

The very simple example of queues are consider you are visit your favourite restaurant and there you are standing in a line to order a food
This is nothing but a queue ,the person who comes first will be able to order his/her food first and then second person and so on ,same is the case with the queue ,element added in queue will be removed from the queue first.
Example :
|COUNTER| person1 <- person2 <- person3
|COUNTER| person2 <- person3
|COUNTER| person2 <- person3
so person1 is in front of the counter so person1 will order the food and will leave the lineand then person2 will be on the counter and will order the food and so on .

Let's Dig in the coding part ,how we will design the queue
Queue Operations:
1. Enqueue() : To add an element in the queue in front position
2. Dequeue() : To remove an element from the queue from front position

Demonstration :
First of all we will define pointers as well as the queue and the size of queue we want which is MAX macros.
Now the purpose of pointer FRONT is to point at front front of queue which is 0th position and the pupose of RARE is to point at last element in the queue available.
MAX will be the size of queue and ofcourse the queue[MAX] is the queue which can contain atmost MAX elements in it
Now we will define the method 'enqueue()' : as mentioned above the purpose of this method is to push the elements at queue
so we will first check if the FRONT pointer is -1 and queue is not FULL which means that queue is empty then we will increment the FRONT and push the element at 0th position and will increment the RARE as well.
In 2nd case if the queue is not empty and not FULL as well then will push the element at RARE'th position .
There is a simple property of the queue which is when we want to dequeue() the element which means we want to remove the element from queue we will remove it from the 0th position always but when it comes to enqueue() which means adding element in queue we will always add the element at last of position of queue so example : if queue of size already has 4 element then we will enqueue() / add the. next element at 5th position and so on until queue is not FULL.

second important method is 'dequeue()' so as explained already with help of this method we can remove element from the queue at 0th position if queue is not empty.
we will first check whether the queue is EMPTY or not and if empty then we will not do anything because if already queue is empty then there is nothing to rmeove from the queue as well.
in case the queue is not EMPTY then we will simply increment the FRONT by 1 so example if we have queue with elements 1 2 3 4 and if we perform dequeue operation then update queue will be 2 3 4 ,as our FRONT will point to 2 now and if we again perform dequeue then 2 will be removed and FRONt will again be incremented
now we need to take care of the case if we remove all the elements from the queue and again try to remove element from the empty queue ,in that case we have additional if condition as FRONT > RARE so if we have queue of 10 elements and we removed all the 10 elements from queue and again tried removing an element from queue then our FRONT will be 10 and RARE will 9 , in this case we will reset the FRONT and RARE to -1 since the queue is empty

The last method is to print the queue for better understanding what is going on in the queue

At last lets implement our main function to perform operations on queue

Now this is not hard to undertand we perform 4 enqueue operations ,so when we print queue it will be like
FRONT-> [ 1 2 3 4] <-RARE
then if we perform 2 dequeue operation then the update queue will be
FRONT-> [ 3 4] <-RARE

you can find the Full code of queue and some of its types like circular queue ,priority queue in python in this repository link : queue's