Insert element ‘0’ in the middle of the queue, if the number of elements is even, and if it is odd, then after the middle element

Queue jumping is not allowed. A queue does not have an operation for inserting into the middle. As such, queue is probably not a good choice of data structure for your use case.

In order to achieve the corresponding result, following algorithm can be used:

repeat initial size / 2 times:
    push front element of the old queue into a new one
    pop element from the front of the old queue
push the new element
repeat until queue is empty
    push front element of the old queue into a new one
    pop element from the front of the old queue

Note that this has linear complexity.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top