DSA - Lab13,14 Queue in CPP Array
DSA - Lab13,14 Queue in CPP Array
Lab Manual 12
CS201: Data Structure and Algorithms
Class: BSCS-2k22
Lab 12: Queues in C++ using Arrays
Instructors:
Mr. Awais Mehmood
[email protected]
&
Mr. M. Faheem Saleem
[email protected]
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.
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.
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.
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 = NULL
and
REAR = NULL
Basic Operations
Enqueue
Return success.
Dequeue
Return success.
Illustration
This is an empty queue and thus we have rear and empty set to
-1.
At this point, the rear pointer has value 2 while the front
pointer is at the 0th location.
Lab Task:-