Page 188 - DCAP201_FUNDAMENTALS_OF_DATA_STRUCTURES
P. 188

Unit 11: Operations and Applications of Queues




               Check whether a queue is full (Is full): Checks whether the queue is full. (If Is full is true,  Notes
               then the removal of an item is possible. If this is false, then acknowledge with the message
               “Queue is full”).
               Add item at the rear of the queue(enqueue).
               Remove item from front of queue(dequeue).

               Read the front of the queue.
               Print the entire queue.




             Notes  The primitive isEmpty(Q) is required to know whether the queue is empty or not,
            because calling next on an empty queue should cause an error. Like stack, the situation
            may be such when the queue is “full” in the case of a finite queue. But we avoid defining
            this as it would depend on the actual length of the Queue defined in a specific problem.




              Task  Make distinction between is empty and Is full operation.
          11.1.1 Enqueue Operation


          This operation is used to add an item to the queue at the rear end. So, the head of the queue will
          be now occupied with an item currently added in the queue. Head count will be incremented by
          one after addition of each item until the queue reaches the tail point.

               !
             Caution  This operation will be performed at the rear end of the queue.

          The following code below will state the Enqueue operation through a function:-
          1.   enqueue()
          2.   {
          3.   int item;
          4.   if (rear==MAX-1) /* QUEUE FULL condition */
          5.   printf(“Queue Overflow, Cannot insert more elements\n”);
          6.   else
          7.   {
          8.   if (front==-1) /*If queue is initially empty */

          9.   front=0;
          10.  printf(“Enter the data element that is to be inserted in queue:”);
          11.  scanf(“%d”, &item);
          12.  rear=rear+1; /* incrementing rear for holding the index of added ele*/
          13.  queue[rear] = item;
          14.  }
          15.  }




                                           LOVELY PROFESSIONAL UNIVERSITY                                   181
   183   184   185   186   187   188   189   190   191   192   193