
The back index points at the last element in the queue.Use return values like boolean and OptionalInt instead. It uses printing statements to give information about the state of the data structure.I would expect the deQueue() method to have OptionalInt as its return type. You don't need a parameter to remove an element that's already in the queue.

The reason that enQueue() takes a parameter is because you need to tell the queue what element it needs to store. Call Center phone systems use Queues to hold people calling them in order.The enQueue() operation puts an element in the next empty slot at the back of the queue, and the deQueue() operation removes the element from the front of the queue simply by incrementing the index that keeps track of where the front of the queue starts.


The complexity of enqueue and dequeue operations in a queue using an array is O(1). This is implemented by a modified queue called the circular queue. Limitation of a queueĪnd we can only add indexes 0 and 1 only when the queue is reset (when all the elements have been dequeued).Īfter REAR reaches the last index, if we can store extra elements in the empty spaces (0 and 1), we can make use of the empty spaces. 1Īs you can see in the image below, after a bit of enqueuing and dequeuing, the size of the queue has been reduced. deQueue removes element entered first i.e. deQueue removes element entered first i.e.
Enqueue vs dequeue full#
6th element can't be added to because the queue is full deQueue is not possible on empty queue * Function to display elements of Queue */ } /* Q has only one element, so we reset the queue after deleting it. Queue Implementations in Python, Java, C, and C++

In programming terms, putting items in the queue is called enqueue, and removing items from the queue is called dequeue. In the above image, since 1 was kept in the queue before 2, it is the first to be removed from the queue as well. Queue follows the First In First Out (FIFO) rule - the item that goes in first is the item that comes out first. It is similar to the ticket queue outside a cinema hall, where the first person entering the queue is the first person who gets the ticket.
