0% found this document useful (0 votes)
0 views

Module 4 Queue

Uploaded by

pratapshivam007
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Module 4 Queue

Uploaded by

pratapshivam007
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

QUEUE

INTRODUCTION
Let us explain the concept of queues using the analogies given below.
 People moving on an escalator. The people who got on the escalator first will be the first one to step out of it.
 People waiting for a bus. The first person standing in the line will be the first one to get into the bus.
 Luggage kept on conveyor belts. The bag which was placed first will be the first to come out at the other end.
 Cars lined at a toll bridge. The first car to reach the bridge will be the first to leave.
In all these examples, we see that the element at the first position is served first. Same is the case with queue
data structure.
 A queue is a FIFO (First-In, First-Out) data structure in which the element that is inserted first is the first one to
be taken out.
 The elements in a queue are added at one end called the REAR and removed from the other end called the
FRONT.
 Queues can be implemented by using either arrays or linked lists.

ARRAY REPRESENTATION OF QUEUE

Queue can be represented using linear arrays. As stated earlier, every queue has front and rear variables that point to
the position from where deletions and insertions can be done, respectively.

OPERATIONS ON QUEUES
In Fig. 8.1, FRONT = 0 and REAR = 5. Suppose we want to add another element with value 45, then REAR would be
incremented by 1 and the value would be stored at the position pointed by REAR. The queue after addition would be as
shown in Fig. 8.2. Here, FRONT = 0 and REAR = 6. Every time a new element has to be added, we repeat the same
procedure. If we want to delete an element from the queue, then the value of FRONT will be incremented. Deletions are
done from FRONT of the queue. The queue after deletion will be as shown in Fig. 8.3.
However, before inserting an element in a queue, we must check for overflow conditions. An overflow will occur when
we try to insert an element into a queue that is already full. When REAR = MAX – 1, where MAX is the size of the queue,
we have an overflow condition (we have written MAX – 1 because the index starts from 0).
Similarly, before deleting an element from a queue, we must check for underflow conditions. An underflow condition
occurs when we try to delete an element from a queue that is already empty. If FRONT = –1 and REAR = –1, it means
there is no element in the queue.
ALGORITHM TO INSERT AN ELEMENT IN A QUEUE

Step 1: IF REAR = MAX-1


Write OVERFLOW
Goto 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

ALGORITHM TO DELETE AN ELEMENT FROM A QUEUE

Step 1: IF FRONT = -1 OR FRONT > REAR


Write UNDERFLOW
ELSE
SET FRONT = FRONT + 1
[END OF IF]
Step 2: EXIT

1. Circular Queues
In linear queues, insertions can be done only at one end called the REAR and deletions are always done from the other
end called the FRONT.

Now, if you want to insert another value, it will not be possible because the queue is completely full. There is no empty
space where the value can be inserted. Consider a scenario in which two successive deletions are made. The queue will
be as shown in following fig.

Suppose we want to insert a new element in the queue. Even though there is space available, the overflow condition
still exists because the condition rear = MAX – 1 still holds true. This is a major drawback of a linear queue. To resolve
this problem, we have two solutions. First, shift the elements to the left so that the vacant space can be occupied and
utilized efficiently. But this can be very time-consuming, especially when the queue is quite large.
The second option is to use a circular queue. In the circular queue, the first index comes right after the last index.
The circular queue will be full only when front = 0 and rear = Max – 1. A circular queue is implemented in the same
manner as a linear queue is implemented. The only difference will be in the code that performs insertion and deletion
operations. For insertion, we now have to check for the following three conditions:
 If front = 0 and rear = MAX – 1, then the circular queue is full.
 If rear != MAX – 1, then rear will be incremented and the value will be inserted.
 If front != 0 and rear = MAX – 1, then it means that the queue is not full. So, set rear = 0 and insert the new
element there.
ALGORITHM for INSERTION in CIRCULAR QUEUE

Step 1: IF FRONT = and Rear = MAX - 1


Write OVERFLOW
Goto step 4
[End OF IF]
Step 2: IF FRONT = -1 and REAR = -1
SET FRONT = REAR = 0
ELSE IF REAR = MAX - 1 and FRONT !=0
SET REAR =0
ELSE
SET REAR = REAR + 1
[END OF IF]
Step 3: SET QUEUE[REAR] = VAL
Step 4: EXIT

You might also like