CACS201 Unit 3 - Queue
CACS201 Unit 3 - Queue
Queue
Contents
• Introduction
• Queue as an ADT
• Primitive Operations in Queue
• Linear and Circular Queue and Their Application
• Enqueue and Dequeue
• Priority Queue
Introduction
• A queue is a linear collection of data elements in which the element
inserted first will be the element taken out first (i.e., a queue is a FIFO data
structure).
• A queue is an abstract data structure, somewhat similar to stacks.
• Unlike stacks, a queue is open from both ends.
• A queue is a linear data structure, in which the first element is inserted
from one end called the REAR end (also called the tail end), and the
deletion of the element takes place from the other end called the FRONT
end (also called the head).
• One end is always used to insert data and the other end is used to remove
data.
• Queues can be implemented by using arrays or linked lists.
Implementation of Queues Using Arrays
• Queues can be easily implemented using arrays.
• Initially the front end (head) and the rear end (tail) of the queue point
at the first position or location of the array.
• As we insert new elements into the queue, the rear keeps on
incrementing, always pointing to the position where the next element
will be inserted, while the front remains at the first position.
• The representation of a queue using an array is shown as follows:
Queue as an ADT
• A queue contains similar items organized in a sequential order.
• The following operations may be carried out in the queue:
• Enqueue () – Insert an element at the end of the queue that is the rear end.
• Dequeue () – Delete the frst item from the front end of the queue, if the
queue is not empty.
• Size () – Returns the number of items within the queue.
• IsEmpty () - Returns true if the queue is empty, otherwise return false.
• IsFull () – Returns true if the queue is full or returns false.
Primitive Operations in Queue
• MakeEmpty(q): To make q as an empty queue
• Enqueue(q, x): To insert an item x at the rear of the queue, this is also
called by names add, insert.
• Dequeue(q): To delete an item from the front of the queue q. this is
also known as Delete, Remove.
• IsFull(q): To check whether the queue q is full.
• IsEmpty(q): To check whether the queue q is empty
• Traverse (q): To read entire queue that is display the content of the
queue.
Operations on Queues
• The two basic operations that can be performed on queues are as
follows:
• Insertion (Enqueue)
• Deletion (Dequeue)
Enqueue
• Enqueue/Insertion is the process of adding new elements in the queue.
• However, before inserting any new element in the queue, we must always
check for the overflow condition, which occurs when we try to insert an
element in a queue which is already full.
• An overflow condition can be checked as follows:
• If REAR = MAX – 1, where MAX is the size of the queue.
• Hence, if the overflow condition is true, then an overflow message is
displayed on the screen; otherwise, the element is inserted into the queue.
• Insertion is always done at the rear end.
Enqueue Example
Algorithm for inserting a new element in a queue
Step 1: START
Step 2: IF REAR = MAX – 1
Print OVERFLOW ERROR
[End of If]
Step 3: IF FRONT = -1 && REAR = -1
Set FRONT = 0
Set REAR = 0
ELSE
REAR = REAR + 1
[End of If]
Step 4: Set QUE[REAR] = ITEM
Step 5: EXIT
Dequeue
• Dequeue/Deletion is the process of removing elements from the queue.
• However, before deleting any element from the queue, we must always
check for the underflow condition, which occurs when we try to delete an
element from the queue which is empty.
• An underflow condition can be checked as follows:
• If FRONT > REAR or FRONT = -1.
• Hence, if the underflow condition is true, then an underflow message is
displayed on the screen; otherwise, the element is deleted from the queue.
• Deletion is always done at the front end.
Dequeue Example
Algorithm for deleting an element from a queue
Step 1: START
Step 2: IF FRONT > REAR or FRONT = -1
Print UNDERFLOW ERROR
[End of If]
Step 3: Set ITEM = QUE[FRONT]
Step 4: Set FRONT = FRONT + 1
Step 5: EXIT
Write a menu-driven program for a linear
queue performing insertion and deletion
operations.
Types of Queues
• Circular Queue
• Priority Queue
• De-Queue (Double-ended queue)
Limitation of Linear Queues
• In linear queues, we discussed that while • Thus, we can see that even after the deletion
inserting a new element in the queue, it is of three elements from the queue, the queue
only done at the rear end. is still full, as REAR = MAX – 1.
• Similarly, while deleting an element from the • We still cannot insert any new elements in it
queue, it is only done at the front end. as there is no space to store new elements.
Therefore, this is a major drawback of the
• Now let us consider a queue of 10 elements linear queue.
given as follows:
• To overcome this problem we can shift all the
elements to the left so that the new elements
can be inserted from the rear end, but
shifting all the elements of the queue can be
a very time-consuming procedure, as the
• The queue is now full, so we cannot insert any practical queues are very large in size.
more elements in it. • Another solution to this problem is a circular
• If we delete three elements from the queue, queue.
now the queue will be:
Circular Queue
• A circular queue is a special type of queue which is implemented in a
circular fashion rather than in a straight line.
• A circular queue is a linear data structure in which the operations are
performed based on the FIFO (First In First Out) principle and the last
position is connected to the first position to make a circle.
• It is also called a “ring buffer.”
Circular Queue
• In a circular queue, the elements are stored in a circular form such
that the first element is next to the last element.
• A circular queue will be full when FRONT = 0 and REAR = MAX – 1 or
FRONT = REAR + 1.
• In that case an overflow error message will be displayed on the
screen.
• Similarly, a circular queue will be empty when both FRONT and REAR
are equal to zero.
• In that case, an underflow error message will be displayed on the
screen.
Inserting an Element in a Circular Queue
• While inserting a new element in the already existing queue, we will first check for
the overflow condition, which occurs when we are trying to insert an element in
the queue.
• The position of the new element to be inserted can be calculated by using the
following formula: REAR = (REAR + 1) % MAX, where MAX is equal to the size of
the queue.
• Example – Let us consider a circular queue with 3 elements in it.
• Suppose we want to insert an element 56 in it.
• Let us see how insertion is done in the circular queue.
• Step 1: Initially the queue contains 3 elements. FRONT denotes the beginning of
the circular queue and REAR denotes the end of the circular queue.
• Step 2: Now, the new element is to be inserted in the queue. Hence, REAR = (REAR
+ 1)%MAX, that is, REAR will be incremented by 1 so that it points to the next
location in the queue.
• Step 3: Finally, in this step the new element is inserted at the location pointed to
by REAR. Hence, after insertion the queue is shown as in the following figure:
Algorithm for inserting an element in a circular queue