Friday, March 8, 2019

CIRCULAR QUEUE


  • To overcome the disadvantages such as,
                  (1) Time consuming
                (2)  Initiate queue is full even if the queue is actually                                            empty.
another important data structure used are called circular queue.

  • In a circular queue if we reach the end of the queue while inserting elements into the queue,it is possible to insert new elements into new elements into queue if the array slots  at the beginning of the circular queue is empty.
  • Circular array allows the entire array can store elements without shifting any data within the array locations in the queue.
  • Circular queue operations:
       (1).Insert an element at the rear end.
       (2).Delete an elements a the front end.
       (3).Display element of circular queue.
  

Insert an element at the rear end:

  • This operation inserts an element always at rear end of the circular queue.      
  • Insertion at the rear end is done by keeping back the counter variable. 
  • Count keeps the track of the numbers of elements present in the queue. whenever any element is inserted count will be incremented by 1 & as the element is deleted count will be decrement by 1. 

     Algorithm: Insert_rear(item).

          STEP 1:Check for queue overflow.
                         if(count==Queue_size)
                         {
                           Printf("Queue overflow");
                           return;
                          }
          STEP 2:Increment rear pointer.
                        rear=(rear+1)%Queue_Size;
          STEP 3:Insert an element
                         Queue[rear]=item.
          STEP 4:Increment count value.
                         count=count+1;
          STEP 5:Stop algorithm.
                         return;

EXAMPLE:

                       

After  inserting 20,30,40&50,

Delete an element at the front end:

       It is used to delete an element from the queue.
  Algorithm:Delete_Front()

       STEP 1:Check for underflow.
                      if(count=0)
                      {
                        printf("Queue underflow);
                        return;
                       }
       STEP 2:Delete an element
                      Cqueue[front];
      STEP 3:Increment the front pointer
                      front=(front+1)%Queue_size;
      STEP 4:Decrement the count value
                      count=count-1;
      STEP 5:Stop algorithm
                     return;

EXAMPLE:

       
After deleting 10,20&30

To display circular queue element:

  • If circular queue is not empty,elements present in the circular queue should be displayed  from the front end to rear end.
  • The reference of the starting element is taken as front pointer.
  • Total element to be displayed can be obtained from the count variable.
  • The display function is achieved by initializing the front pointer variable to J and then increment J each time using the statement,
                  J=(J+1)%Queue_size.

  Algorithm:Display()

   STEP 1:Check for the empty queue.
                  if(count=0)          
                  {     
                   Printf("circular queue is empty");                                                         return;       
                   } 
   STEP 2: Take the front reference and print the element.                            J←front; 
                  Repeat for(I←J to count)      
                  {         
                   Print(Cqueue[I]);   
                   J=(J+1)% queue_size;  
                   }     
   STEP 3: Stop algorithm.         
                  return;
                       ********

No comments:

Post a Comment