Page 189 - DCAP201_FUNDAMENTALS_OF_DATA_STRUCTURES
P. 189

Fundamentals of Data Structures




                    Notes          The code written above shows the enqueue operation on a queue, i.e. inserting elements to it.
                                   Line marks the beginning of the enqueue function. Line 3 declares a variable item that will
                                   supposedly hold the value that is to be inserted.




                                     Notes  Although we can even approach in a different way, we could have even passed the
                                     element to be inserted as an argument to the function, well let’s proceed with our current
                                     approach.
                                   Line 4 is the condition for a full queue or we can say queue overflow. Logically speaking if the
                                   value of rear is equal to MAX-1 (stating the maximum number of elements a queue can accept),
                                   than it obviously states that the queue is full. Line 5 displays the appropriate message if the
                                   condition is true.

                                                        Figure 11.1: Enqueue Operation on a Queue


















                                   Line 8 is the part if the queue overflow condition is false, another if condition is encountered and
                                   it checks if the queue is empty or queue underflow. The variable queue is initially initialized as
                                   –1 which states that the queue is empty. When the first element is inserted the queue is initialized
                                   to zero. Hence since its time for inserting the first element, we initialize the queue as zero if the
                                   queue is empty. Line 11 accepts the data that is to be inserted and saves it in variable item. Line
                                   12 increments the rear variable since it has to be incremented every time we will insert an
                                   element. We insert the element to be added on to the array queue[].

                                   11.1.2 Dequeue Operation

                                   This operation is used to remove an item from the queue at the front end. Now the tail count will
                                   be decremented by one each time when an item is removed from the queue until the queue
                                   reaches the head point.

                                       !

                                     Caution  This operation will be performed at the front end of the queue.
                                   The dequeue operation is shown by the dequeue function which helps to delete an element:-
                                   1.  dequeue()

                                   2.  {
                                   3.  if (front == -1 || front > rear) /* queue empty condition */
                                   4.  {




          182                               LOVELY PROFESSIONAL UNIVERSITY
   184   185   186   187   188   189   190   191   192   193   194