UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Department of Computer Science
Lab Manual 12
CS201: Data Structure and Algorithms
Class: BSCS-2k22
Lab 12: Queues in C++ using Arrays
Instructors:
Mr. Awais Mehmood
awais.mehmood@uettaxila.edu.pk
&
Mr. M. Faheem Saleem
muhammad.faheem@uettaxila.edu.pk
CS201: Data Structure and Algorithms Page 1
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Department of Computer Science
Lab-12: Implementation of Queues using Arrays in C++
Introduction
Implementation of Queues using Arrays List in C++
Objectives
The objective of this session is to understand the various operations on queues using arrays in
C++.
Tools/Software Requirement
Dev C++
Goals for today’s lab:
• Perform operations related to Queues in C++ using Arrays.
Queues using Arrays:-
CS201: Data Structure and Algorithms Page 2
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Department of Computer Science
This manual discusses another important data structure, called a queue. The notion of a queue in
computer science is the same as the notion of the queues to which you are accustomed in
everyday life. There are queues of customers in a bank or in a grocery store and queues of cars
waiting to pass through a tollbooth. Similarly, because a computer can send a print request faster
than a printer can print, a queue of documents is often waiting to be printed at a printer. The
general rule to process elements in a queue is that the customer at the front of the queue is served
next and that when a new customer arrives, he or she stands at the end of the queue. That is, a
queue is a First In First Out data structure.
A queue is a set of elements of the same type in which the elements are added at one end, called
the back or rear, and deleted from the other end, called the front. For example, consider a line of
customers in a bank, wherein the customers are waiting to withdraw/deposit money or to conduct
some other business. Each new customer gets in the line at the rear. Whenever a teller is ready
for a new customer, the customer at the front of the line is served.
The rear of the queue is accessed whenever a new element is added to the queue, and the front of
the queue is accessed whenever an element is deleted from the queue. As in a stack, the middle
elements of the queue are inaccessible, even if the queue elements are stored in an array.
Queue: A data structure in which the elements are added at one end, called the rear, and deleted
from the other end, called the front; a First In First Out (FIFO) data structure.
Queues may be represented in the computer in various ways, usually by means at one way list or
linear arrays. Unless otherwise stated or implied each of our queues will be maintained by a
linear array QUEUE and two pointer variable FRONT containing the location of the front
element of the queue and REAR containing the location of the rear element of the queue. The
condition FRONT = NULL will indicate that the queue is empty.
CS201: Data Structure and Algorithms Page 3
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Department of Computer Science
Whenever an element is deleted from the queue the value of FRONT is increased by one. This
can be implemented by the assignment.
FRONT = FRONT + 1
Similarly whenever an element is added to the queue the value of REAR is increased by one.
This can be implemented by the assignment.
REAR = REAR + 1
This means that after N insertions the rear element of the queue will occupy QUEUE[N] or in
other words eventually the queue will occupy the last part of the array. This occurs even though
the queue itself may not contain many elements.
CS201: Data Structure and Algorithms Page 4
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Department of Computer Science
Suppose we want to insert an element ITEM into a queue at the time the queue does occupy the
last part of the array i.e. when REAR = N. One way is to do this simply move the entire queue to
the beginning of the array changing FRONT and REAR accordingly, and then inserting ITEM as
above. This procedure may by very expensive. The procedure we adopt is to assume that the
array QUEUE is circular that is that QUEUE[1] comes after QUEUE[N] in the array. With this
assumption we insert ITEM into the queue by assigning ITEM to QUEUE[1]. Specifically
instead of increasing REAR to N+1 we reset REAR=1 and then assign
QUEUE[REAR] = ITEM
Similarly if FRONT=N and an element is deleted then we reset FRONT=1 instead of increasing
FRONT to N+1.
Suppose that our queue only contains one element i.e. suppose that
FRONT = REAR ≠ NULL
And suppose that the element is deleted. Then we assign
FRONT = NULL
and
REAR = NULL
to indicate that the queue is empty.
Algorithm for insertion into the Queue:-
QINSERT(QUEUE, N ,FRONT,REAR, ITEM)
This procedure inserts an element ITEM into a queue.
1. [Queue already filled?]
If FRONT = 1 and REAR = N or if FRONT =REAR+1 then
Write OVERFLOW and Return.
2. [Find new value of REAR]
If FRONT = NULL then [Queue initially empty]
Set FRONT =1 and REAR = 1.
else if REAR = N then
Set REAR = 1.
else
CS201: Data Structure and Algorithms Page 5
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Department of Computer Science
Set REAR = REAR + 1.
[End of if Structure]
3. Set QUEUE[REAR] = ITEM [This inserts new element]
4. Return.
Algorithm for Deletion from Queues:-
QDELETE(QUEUE , N ,FRONT REAR, ITEM)
This procedure deletes an element from a queue and assigns it
to the variable ITEM.
1. [Queue already empty?]
If FRONT = NULL then Write Underflow and Return.
2. Set ITEM = QUEUE[FRONT]
3. [Find new value of FRONT]
If FRONT = REAR then [Queue has only one element to
start]
Set FRONT = NULL and REAR = NULL
else if FRONT = N then
Set FRONT = 1
else
Set FRONT = FRONT + 1
[End of If Structure]
4. Return.
CS201: Data Structure and Algorithms Page 6
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Department of Computer Science
Basic Operations
The queue data structure includes the following operations:
EnQueue: Adds an item to the queue. Addition of an item to the
queue is always done at the rear of the queue.
DeQueue: Removes an item from the queue. An item is removed or
de-queued always from the front of the queue.
isEmpty: Checks if the queue is empty.
isFull: Checks if the queue is full.
peek: Gets an element at the front of the queue without
removing it.
Enqueue
In this process, the following steps are performed:
Check if the queue is full.
If full, produce overflow error and exit.
Else, increment ‘rear’.
Add an element to the location pointed by ‘rear’.
Return success.
Dequeue
Dequeue operation consists of the following steps:
Check if the queue is empty.
If empty, display an underflow error and exit.
Else, the access element is pointed out by ‘front’.
Increment the ‘front’ to point to the next accessible data.
Return success.
CS201: Data Structure and Algorithms Page 7
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Department of Computer Science
Next, we will see a detailed illustration of insertion and
deletion operations in queue.
Illustration
This is an empty queue and thus we have rear and empty set to
-1.
Next, we add 1 to the queue and as a result, the rear pointer
moves ahead by one location.
In the next figure, we add element 2 to the queue by moving
the rear pointer ahead by another increment.
In the following figure, we add element 3 and move the rear
pointer by 1.
CS201: Data Structure and Algorithms Page 8
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Department of Computer Science
At this point, the rear pointer has value 2 while the front
pointer is at the 0th location.
Next, we delete the element pointed by the front pointer. As
the front pointer is at 0, the element that is deleted is 1.
Thus the first element entered in the queue i.e. 1 happens to
be the first element removed from the queue. As a result,
after the first dequeue, the front pointer now will be moved
ahead t0 the next location which is 1.
Lab Task:-
Write a C++ code to perform insertion and deletion in queue
using arrays applying the algorithms given in the manual. Create
a menu shown below.
CS201: Data Structure and Algorithms Page 9
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Department of Computer Science
CS201: Data Structure and Algorithms Page 10