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

CACS201 Unit 3 - Queue

Uploaded by

Mizash Kandel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

CACS201 Unit 3 - Queue

Uploaded by

Mizash Kandel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Unit 3

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

Step 1: START Step 4: ELSE


Step 2: IF (FRONT = 0 && REAR = IF (REAR = MAX - 1)
MAX – 1) OR (FRONT = Set REAR = 0
(REAR + 1)%MAX) ELSE
Print OVERFLOW ERROR REAR = REAR + 1
[End of If]
Step 3: ELSE
[End of If]
IF (FRONT = -1)
Step 5: Set CQUEUE[REAR] = ITEM
Set FRONT = 0 Step 6: EXIT
Set REAR = 0
Deleting an Element from a Circular Queue
• While deleting an element from the already existing queue, we will first
check for the underflow condition, which occurs when we are trying to
delete an element from the queue which is empty.
• After deleting an element from the circular queue, the position of the
FRONT end can be calculated by the formula: FRONT = (FRONT +1) %
MAX, where MAX is equal to the size of the queue.
• For Example – Let us consider a circular queue with 8 elements in it.
Suppose we want to delete an element 45 from it.
• Let us see how deletion is done in the circular queue.
• Step 1: Initially the queue contains 8 elements. FRONT denotes the
beginning of the circular queue and REAR denotes the end of the circular
queue.
• Step 2: Now, the element is to be deleted from the queue. Hence, FRONT
= (FRONT + 1)%MAX, that is, FRONT will be incremented by 1 so that it
points to the next location in the queue. Also, the value is deleted from
the queue.
Algorithm for deleting an element from a circular queue
Step 1: START
Step 2: IF (FRONT = -1)
Print UNDERFLOW ERROR
Step 3: ELSE
Set ITEM = CQUEUE[FRONT]
Step 4: IF (FRONT = REAR)
Set FRONT = -1
Set REAR = -1
Step 5: ELSE IF (FRONT = MAX – 1)
Set FRONT = 0
ELSE
FRONT = FRONT + 1
[End of If]
[End of If]
Step 6: EXIT
Applications of Circular Queue
• Memory Management: The unused memory locations in the case of
ordinary queues can be utilized in circular queues.
• Traffic system: In computer controlled traffic system, circular queues
are used to switch on the traffic lights one by one repeatedly as per
the time set.
• CPU Scheduling: Operating systems often maintain a queue of
processes that are ready to execute or that are waiting for a particular
event to occur.
Write a menu-driven program for a circular queue
performing insertion and deletion operations.
Priority Queue
• A priority queue is another variant of a queue in which elements are
processed on the basis of assigned priority.
• Each element in a priority queue is assigned a special value called the
priority of the element.
• The elements in the priority queue are processed on the basis of the
following rules:
1. An element with higher priority is processed first and then the element with
lower priority is processed.
2. If the two elements have the same priority, then the elements are
processed on the First Come First Served basis. The priority of the element
is selected by its value called the implicit priority, and the priority number
given with each element is called the explicit priority.
Types of Priority Queue
• Ascending Priority Queue – In this type of priority queue, elements
can be inserted in any order, but at the time of deletion of elements
from the queue, the smallest element is searched and deleted first.
• Descending Priority Queue – In this type of priority queue, elements
can be inserted in any order, but at the time of deletion of elements
from the queue, the largest element is searched and deleted first.
Implementation of a priority queue using arrays
• While implementing a priority queue using arrays, the following points
must be considered:
• Maintain a separate queue for each level of priority or priority number.
• Each queue will appear in its own circular array and must have its own pairs of
pointers, that is, FRONT AND REAR.
• If each queue is allocated the same amount of memory, then a 2D array can be
used instead of a linear array.
• For example – FRONT [K] and REAR [K] are the pointers containing the front and
rear values of row “K” of the queue, where K is the priority number.
• Suppose we want to insert an element with priority K, then we will add the element
at the REAR end of row K; K is the row as well as the priority number of that
element.
• If we add F with priority number 4, then the queue will be given as shown in the
following:
Implementation of a priority queue using arrays
Applications of Priority Queue
• CPU Scheduling
• Graph algorithms like Dijkstra’s shortest path algorithm, Prim’s
Minimum Spanning Tree, etc.
• Stack Implementation
• All queue applications where priority is involved
• Data compression in Huffman code
• Event-driven simulation such as customers waiting in a queue
• Finding Kth largest/smallest element
Write a menu-driven program for a priority queue
performing insertion and deletion operations.

You might also like