circular queue in data structure

Circular queue

 A queue   is  called circular when the last  room comes just before the  the 1st room.

Algorithm to insert an element in a circular queue

Step 1  [check for the overflow condition]

                        If f=0 and  r = size-1 or r+1 = f, then

                        Output  overflow and exit

Step2 [insert an element in the queue]

            Else if   f = -1 , then

            F =0

            R = 0

            Q[r] = value

Step3 [check if the rear at the end of the queue.]

            Else if rear = size -1 , then

                        R  = 0

                        Q[r] = value

Step4 insert the element

            Else r = r+1

            Q[r] = value

Step5 exit

           

Algorithm to delete an element in a circular queue

Step1 – [check for under flow condition]

                        If front = -1

                        Output   underflow  & exit

Step2  [remove the  element]

                        Item  = Q[front]

Step3 [check whether the queue is empty or not]

                        If front  = rear, then

                        Front = -1

                        Rear   = -1

Step4  [ check the front pointer position]

                        Else if, front = size-1, then

                        Front = 0

                        Else, front = front+1

Step5   [return the removed element]

                        Return[item]