QUEUE
QUEUE
Prepared by: Kashif Aman, Lecturer (CS), Computer Science Department, CECOS University of IT & Emerging
Sciences, Peshawar
QUEUE
Introduction to Queue:
A Queue is a Linear List of elements in which Deletion can take place only at one end,
called FRONT, and Insertion can take place at other end, called REAR. The term FRONT
and REAR are used in describing a Linear List only when it is implemented as a Queue.
OR
The Queue is also called First In First Out (FIFO) Lists, since the first element in the Queue
will be the first element that can get out of the Queue. In other words, the order in which the
elements enter into Queue is the order in which they will leave.
We define a queue to be a list in which all additions to the list are made at one end (back of the queue), and all
deletions from the list are made at the other end(front of the queue). The element which is first pushed into the order,
the delete operation is first performed on that.
The purpose of Queue is to provide some form of buffering. In a computer system, Queue is
used for:
Process Management: For example, in a timesharing (time-sharing system is a type of operating system that
allows multiple users to share the resources of a single computer simultaneously.) system in computer, programs
are added to a queue and are executed one after the other.
Buffer between the fast computer and a slow printer: Documents sent to the printer for
printing is added to a queue. The document sent first is printed first and document sent last is
printed last
Queues abound in everyday life. The automobiles are waiting to pass through an intersection
Prepared by: Kashif Aman, Lecturer (CS), Computer Science Department, CECOS University of IT & Emerging
Sciences, Peshawar
from a queue in which the first car in line is the first can through; the people are waiting in line
at a bank from a queue, where the first person in line is the first person to be waited on and so
on.
Advantages of Queue Data Structure:
• A large amount of data can be managed efficiently with ease.
• Operations such as insertion and deletion can be performed with ease as it follows the first in first out rule.
• Queues are useful when a particular service is used by multiple consumers.
• Queues are fast in speed for data inter-process communication. (means that queues are an efficient method
for exchanging data between different processes running on the same system)
• Queues can be used in the implementation of other data structures.
Disadvantages of Queue Data Structure:
• The operations such as insertion and deletion of elements from the middle are time consuming.
• Searching an element takes O(N) time.
• Maximum size of a queue must be defined prior in case of array implementation.
Applications of Queue Data Structure:
Application of queue is common. In a computer system, there may be queues of tasks waiting for the printer, for access
to disk storage, or even in a time-sharing system, for use of the CPU. Within a single program, there may be multiple
requests to be kept in a queue, or one task may create other tasks, which must be done in turn by keeping them in a
queue.
• Queue can be used in job scheduling like Printer Spooling.
• Queue can be used where we have a single resource and multiple consumers.
• In a network, a queue is used in devices such as a router/switch and mail queue.
• Queue can be used in various algorithm techniques like Breadth First Search, Topological Sort, etc
Representation of Queue:
The queue is represented in the computer in various ways, usually by means of one-way link list
or linear array. Let takes the example of linear array.
Consider a linear array QUEUE and two pointers’ variables FRONT and REAR. FRONT will
contain the location number of the front element of the queue and REAR will contain the
location number of the rear element. The condition FRONT = REAR = – 1 will indicate that
queue is empty. If the array QUEUE has N elements, then whenever a new element is added,
the value of the REAR will be incremented by 1 e.g.
REAR = REAR + 1
Similarly, when an item is deleted the value of the FRONT will be incremented by 1 e.g.
FRONT = FRONT + 1
Prepared by: Kashif Aman, Lecturer (CS), Computer Science Department, CECOS University of IT & Emerging
Sciences, Peshawar
Types of Queues:
The major drawback of using a linear Queue is that insertion is done only from the rear end. If the first three
elements are deleted from the Queue, we cannot insert more elements even though the space is available in a Linear
Queue. In this case, the linear Queue shows the overflow condition as the rear is pointing to the last element of the
Queue.
Simple example for non-Circular QUEUE: To explain the below operations we have
a simple example as follow:
Representation of Queue:
The queue is represented in the computer in various ways, usually by means of one-way link list
or linear array. Let takes the example of linear array.
Consider a linear array QUEUE and two pointers’ variables FRONT and REAR. FRONT will contain the
Prepared by: Kashif Aman, Lecturer (CS), Computer Science Department, CECOS University of IT & Emerging
Sciences, Peshawar
location number of the front element of the queue and REAR will contain the location number of the rear
element. The condition FRONT = REAR = – 1 will indicate that queue is empty. If the array QUEUE
has N elements, then whenever a new element is added, the value of the REAR will be incremented by 1
e.g.
REAR = REAR + 1
Similarly, when an item is deleted the value of the FRONT will be incremented by 1 e.g.
FRONT = FRONT + 1
Types of Queue:
There are two types of Queue:
1. Non-Circular Queue
2. Circular Queue
1. Non-Circular Queue:
Simple example for non-Circular QUEUE No 1: To explain the above operations we have
a simple example as follow:
FRONT = REAR = -1 or NULL
0 1 2 3 4
Empty (QUEUE) =? It will return
TRUE because QUEUE is empty.
FRONT = REAR = 0 0 1 2 3 4
Insert (QUEUE, A) A
0 1 2 3 4
FRONT = 0, REAR = 1
A B
Insert (QUEUE, B)
FRONT = 0, REAR = 2 0 1 2 3 4
Insert (QUEUE, C)
A B C
full (QUEUE) =? It will return FALSE because the QUEUE is not full.
FRONT = 1, REAR = 2 0 1 2 3 4
ITEM = remove (QUEUE, A) B C
FRONT = 1, REAR = 3
0 1 2 3 4
Insert (QUEUE, D) B C D
FRONT = 1, REAR = 4 0 1 2 3 4
B C D E
Insert (QUEUE, E)
FRONT = 2, REAR = 4
ITEM = remove (QUEUE, B) 0 1 2 3 4
C D E
Prepared by: Kashif Aman, Lecturer (CS), Computer Science Department, CECOS University of IT & Emerging
Sciences, Peshawar
full (QUEUE) =? It will return TRUE because the QUEUE is full (means REAR = N-1).
Example Scenario No 2
Let's illustrate this with an example:
• Suppose you have a linear queue with a size of 5, and its current state is:
01234
ABCDE
Where FRONT = 0 and REAR = 4.
• After removing the first three elements (A, B, and C), the queue looks like this:
01234
---DE
Where FRONT = 3 and REAR = 4.
• If you try to insert new elements, you cannot add them because the rear pointer is at index 4, which is the end
of the array, even though there is free space available at the beginning of the array.
Insertion Algorithm for Non-Circular QUEUE: This algorithm is used to insert an ITEM to Non-
Circular QUEUE.
ALGORITHM: INSERTION (FRONT, REAR, ITEM, N, QUEUE)
Prepared by: Kashif Aman, Lecturer (CS), Computer Science Department, CECOS University of IT & Emerging
Sciences, Peshawar
Deletion Algorithm for Non-Circular QUEUE: This algorithm is used to delete an ITEM
from non-Circular QUEUE.
Write (“Underflow”)
Return
[End of IF Structure]
Step 3: [Delete Item]
Else:
FRONT = FRONT + 1
[End of IF Structure]
Step 5: [Finish]
Exit
Prepared by: Kashif Aman, Lecturer (CS), Computer Science Department, CECOS University of IT & Emerging
Sciences, Peshawar
Circular Queue:
Example: To explain the above operations, we have a simple example as follow:
0 1 2 3 4
FRONT = 1, REAR = 2 0 1 2 3 4
ITEM = remove (QUEUE, A) B C
FRONT = 1, REAR = 3 0 1 2 3 4
Insert (QUEUE, D) B C D
FRONT = 1, REAR = 4 0 1 2 3 4
Insert (QUEUE, E) B C D E
FRONT = 2, REAR = 4 0 1 2 3 4
ITEM = remove (QUEUE, B)
C D E
full (QUEUE) =? It will return FALSE because the QUEUE is not full.
FRONT = 2, REAR = 0 0 1 2 3 4
Insert (QUEUE, F) F C D E
FRONT = 2, REAR = 1 0 1 2 3 4
Insert (QUEUE, G) F G C D E
full (QUEUE) =? It will return TRUE because the QUEUE is full (means REAR = N-1
&& FRONT = 0 || REAR + 1 = FRONT).
In Circular Queue, the QUEUE [0] comes after the QUEUE [N – 1] in linear array i.e.
the first element stored after the last element of the queue if the space is available. With this
assumption an ITEM is inserted into the QUEUE by assigning ITEM to QUEUE [0]. Instead of
incrementing REAR to N, reset REAR = 0 and then assign the ITEM.
Prepared by: Kashif Aman, Lecturer (CS), Computer Science Department, CECOS University of IT & Emerging
Sciences, Peshawar
QUEUE [REAR] = ITEM
Similarly, if FRONT = N – 1 and the element of QUEUE is deleted then reset FRONT = 0
instead of increasing FRONT to N.
Suppose that QUEUE (Circular/non-Circular) contains only one element i.e. FRONT
= REAR and the element is deleted then reset FRONT = REAR = NULL (or -1) indicating that
the queue is empty. If FRONT = REAR = -1 (means queue is empty) and the element is
inserted, then reset FRONT = REAR = 0.
Insertion Algorithm for Circular QUEUE: This algorithm is used to insert an ITEM
to Circular QUEUE.
ALGORITHM: INSERTION (FRONT, REAR, ITEM, N, QUEUE)
Step 1: [Initially FRONT & REAR pointers positions]
FRONT = -1 or 0 or 1 or 2....…& REAR = -1 or 0 or 1 or 2………
[End of IF Structure]
Step 5: [Finish]
Exit
Prepared by: Kashif Aman, Lecturer (CS), Computer Science Department, CECOS University of IT & Emerging
Sciences, Peshawar
Deletion Algorithm for Circular QUEUE: This algorithm is used to delete an ITEM
from Circular QUEUE.
FRONT = 0
Else:
FRONT = FRONT + 1
[End of IF Structure]
Step 5: [Finish]
Prepared by: Kashif Aman, Lecturer (CS), Computer Science Department, CECOS University of IT & Emerging
Sciences, Peshawar
Exit
Priority Queue:
A Priority Queue is different from a "Normal Queue", because instead of being a "FIFO (First-
In-First-Out)" technique, data structure values come out in order by Priority.
A priority queue is an abstract data type that behaves similarly to the normal queue except that each element has some
priority, i.e., the element with the highest priority would come first in a priority queue. The priority of the elements in a
priority queue will determine the order in which elements are removed from the priority queue.
Examples:
Prepared by: Kashif Aman, Lecturer (CS), Computer Science Department, CECOS University of IT & Emerging
Sciences, Peshawar
Printers use a priority queue to manage different print jobs, prioritizing them based on size or type.
Example Print Jobs:
o Job A: Large document printing (Low Priority)
o Job B: Urgent one-page report (High Priority)
o Job C: Standard email printout (Medium Priority)
Prepared by: Kashif Aman, Lecturer (CS), Computer Science Department, CECOS University of IT & Emerging
Sciences, Peshawar
Prepared by: Kashif Aman, Lecturer (CS), Computer Science Department, CECOS University of IT & Emerging
Sciences, Peshawar
1. Ascending Order Priority Queue/Min Priority Queue:
As the name suggests, in ascending order priority queue, the element with a lower priority
value is given a higher priority in the priority list.
Example: A Priority Queue might be used, for example, to handle the jobs sent to
the Computer Science Department's printer: Jobs sent by the department chairman should
be printed first, then jobs sent by professors, then those sent by graduate students, and
finally those sent by undergraduates. The values put into the priority queue would be the
priority of the sender (e.g. using 1 for the chairman, 2 for professors, 3 for graduate students,
and 4 for undergraduates), and the associated information would be the document to print.
Each time the printer is free; the job with the highest priority would be removed from the
print queue and printed. (Note that it is OK to have multiple jobs with the same
priority; if there is more than one job with the same highest priority and when the printer
is free, then any one of them can be selected).
A double-ended priority queue (DEPQ) is a data structure that stores a collection of elements, where each
element is associated with a priority or value. Elements can be inserted and removed from both ends of the
queue based on their priority.
The highest priority element can be accessed from either end of the queue without removing it. In a DEPQ,
the elements are stored in a way that maintains their priority order, such that the highest priority element is
always at the front or back of the queue, depending on the implementation.
Prepared by: Kashif Aman, Lecturer (CS), Computer Science Department, CECOS University of IT & Emerging
Sciences, Peshawar
double-ended priority queue (DEPQ)
A double ended priority queue supports operations of both max heap (a max priority queue) and min heap
(a min priority queue).
OR
A double-ended priority queue (DEPQ) is a data structure that stores a collection of elements, where
each element is associated with a priority or value. Elements can be inserted and removed from both
ends of the queue based on their priority.
The highest priority element can be accessed from either end of the queue without removing it. In a
DEPQ, the elements are stored in a way that maintains their priority order, such that the highest priority
element is always at the front or back of the queue, depending on the implementation.
The following operations are expected from double ended priority queue.
❖ getMax() : Returns maximum element.
❖ getMin() : Returns minimum element.
❖ deleteMax() : Deletes maximum element.
❖ deleteMin() : Deletes minimum element.
❖ size() : Returns count of elements.
❖ isEmpty() : Returns true if the queue is empty
Prepared by: Kashif Aman, Lecturer (CS), Computer Science Department, CECOS University of IT &
Emerging Sciences, Peshawar