Operations in queue, Enqueue and Dequeue


Queue 

A queue is a linear data structure that follows the First In, First Out (FIFO) principle, meaning the element that is inserted first is the one that gets removed first. Think of it like a line at a ticket counter or a queue at a cafeteria. The first person to join the line is the first one to be served, and as people join the line, they add themselves at the end (rear) of the queue.

The following diagram shows array representation of queue data structure:

Applications of queue in real world scenarios:

Operating Systems: The operating system uses queues for scheduling tasks. For example, when multiple processes are waiting for execution, they are placed in a queue, and the operating system schedules them based on the FIFO principle.

Print Queue: In a computer system, when multiple print jobs are sent to a printer, they are placed in a queue. The printer processes them in the order they were received.

Breadth-First Search (BFS): BFS algorithm uses a queue to keep track of which nodes to visit next. It's commonly used in graph traversal to find the shortest path.

Message Queues in Networking: In networking, message queues are used to store and forward messages between network nodes. For example, in a chat application, messages sent by users are stored in a queue and then delivered to the recipients.

Call Center Systems: In a call center, incoming calls are placed in a queue and serviced by agents in the order they were received.

The two fundamental operations that can be performed on the queue are: 

  1. Enqueue operation
  2. Dequeue operation

Enqueue operation

If a given queue is not full, the enqueue operations insert elements at the rear end of the queue. 

Algorithm to insert element into the queue[Enqueue operation]

Check if the queue is already full by comparing rear to max - 1. if so, then return an overflow error.

If the item is to be inserted as the first element in the list, in that case set the value of front and rear to 0 and insert the element at the rear end.

Otherwise keep increasing the value of rear and insert each element one by one having rear as the index.

Algorithm: Enqueue (Insert element into the queue)
Input: NUM (Element to be inserted)

Step 1: IF REAR = MAX - 1
           Write "OVERFLOW"
           Go to Step 4
       [END OF IF]

Step 2: IF FRONT = -1 and REAR = -1
           SET FRONT = REAR = 0
       ELSE
           SET REAR = REAR + 1
       [END OF IF]

Step 3: Set QUEUE[REAR] = NUM

Step 4: EXIT

Dequeue operation

If a given queue is not empty the dequeue operation deletes elements from the front end of the queue.

Algorithm to delete element from the queue[Dequeue]

If the value of front is -1 or value of front is greater than rear , write an underflow message and exit.

Otherwise, keep increasing the value of front and return the item stored at the front end of the queue at each time.

Algorithm: Dequeue (Remove element from the queue)

Step 1: IF FRONT = -1 or FRONT > REAR
           Write "UNDERFLOW"
       ELSE
           SET VAL = QUEUE[FRONT]
           SET FRONT = FRONT + 1
       [END OF IF]

Step 2: EXIT

The following diagram shows enqueue and dequeue operations in the array implemented queue data structure: