Page 91 - DCAP605_ADVANCED_DATA_STRUCTURE_AND_ALGORITHMS
P. 91

Advanced Data Structure and Algorithms




                    Notes          An implementation of the circular queue is shown below:

                                   struct Cqueue
                                      {
                                      T data[n];
                                      int front, rear;
                                     }
                                   void insert(queue q[], T x)
                                   /* This procedure inserts an element x into the queue q */
                                   {
                                      q.rear = (q.rear + 1) % n;
                                      if(q.rear == q.front)
                                          printf(“Queue full”);
                                      else
                                          q.data[q.rear] = x;
                                   }
                                   void delete(queue q, T x)
                                   /* deletes an element & stores it in x */
                                   {
                                      if(q.front == q.rear)
                                          printf(“queue empty”);
                                      else
                                      {
                                          x = q.data[q.front];
                                          q.front =(q.front+ l) % n;
                                      }
                                   }
                                   boolean empty(queue q[])
                                   /* This function checks if the queue q is empty or not */
                                   {
                                      if(q.front == q.rear) return(true);
                                      else return(false);
                                   }
                                   void initialize(queue q[])
                                   /* This procedure initializes a queue q * /
                                   {
                                      q.front = 0;
                                      q.rear = 0;
                                   }
                                   In this implementation, we are using only n-1 entries of the ‘elements’ array. To use all the

                                   elements, a special flag has to be maintained to distinguish between the queue_full and queue_
                                   empty situations.



          86                               LOVELY PROFESSIONAL UNIVERSITY
   86   87   88   89   90   91   92   93   94   95   96