Page 55 - Data Structures Interactive Book
P. 55

6.3    Operations on Queues


                       Queues support several fundamental operations that define how elements are added,

               removed, and inspected. These operations ensure that the queue maintains its FIFO (First-In,
               First-Out) behavior, meaning the earliest element inserted is always the first to be processed.



                      6.3.1  Enqueue Operation

                       The enqueue operation inserts a new element at the rear of the queue. In arrays, this

               involves incrementing the rear index and storing the new value. In linked lists, a new node is
               created and linked to the current rear node. If the queue is full (in arrays), an overflow occurs.

               Enqueue ensures that tasks or data items are stored in the order they arrive.



                      6.3.2  Dequeue Operation

                       The dequeue operation removes the element at the front of the queue. In arrays, this
               means incrementing the front index, while in linked lists it involves updating the front pointer

               to the next node and freeing memory. If the queue is empty, an underflow condition occurs.

               Dequeue is critical in systems where tasks must be processed in sequence.


                      6.3.3  Peek/Front Operation


                       The peek (or front) operation retrieves the element at the front without removing it.

               This allows inspection of the next item to be processed, which is useful in applications like

               task scheduling or packet transmission.


                      6.3.4  Checking for Empty and Full Conditions


                       Before performing enqueue or dequeue, it is important to check whether the queue

               is empty or full. In arrays, the queue is full when rear == MAX-1 and empty when front == -1

               or front > rear. In linked lists, the queue is empty when front == NULL. These checks prevent

               invalid operations and ensure program stability.







                                                             55
   50   51   52   53   54   55   56   57   58   59   60