Posts

Showing posts with the label Circular Queue

Circular Queue Implementation using Arrays - All Operations

Image
  Algorithm : Enqueue: Check if the queue is full. If the queue is full, throw an error else : Fixing the position of endOFQueue: Check if endOFQueue has reached the last cell of the array if yes, then make endOFQueue=0 , as the queue is not full and it has space left towards the start of the array if no, then just simply increment endOFQueue by 1, as the queue is not full and it has space left towards the end of the array Once the position of endOFQueue is fixed by above, we simply put the data in the array at endOFQueue using arr[endOFQueue] Dequeue: Check is the queue is empty. If the queue is empty, throw an error else: Remove the element from start of queue i.e. arr[startOFQueue] Fix the new Postion of startOFQueue: Check if there was only one element in queue, using if(startOFQueue==endOFQueue) if yes, just simply set startOFQueue=endOFQueue=-1 if no, i.e. there are more than 1 element in queue, check the current position of start: if st...