Faculty of Information Technology - Computer Science Department 1
Data Structures
Faculty of Information Technology - Computer Science Department 2
Chapter 3
Circular Queue Data Structure Using Array
Faculty of Information Technology - Computer Science Department 3
Outline
Circular Queue (Array)
Methods Implementation
Faculty of Information Technology - Computer Science Department 4
Circular Queue Using
Array
With the Floating-front design, the rear
of the queue could reach the end of
the (physical) array while the queue is
not yet full.
A solution to this problem is to let the
queue elements “wrap around” the end
of the array.
The array can be treated as a circular
structure (Circular Queue) in which the
last slot is followed by the first slot
(like in Figure b)
Faculty of Information Technology - Computer Science Department 5
Main Class of the Linear
Queue
Output:
Faculty of Information Technology - Computer Science Department 6
Main Class of the Linear
Queue
Note that although some elements were
Output: removed from the queue, it
still indicates that the
list is full and the
𝑒𝑛𝑞𝑢𝑒 operation was not executed.
This case leads to thinking of
the
circular queue to solve this problem.
So, what is the circular queue?
Faculty of Information Technology - Computer Science Department 7
Circular Queue Using
Array
An array is called circular if we consider the first element as the next of the last
element.
The circular array is used to implement a circular queue.
Faculty of Information Technology - Computer Science Department 8
Boundary conditions for the circular
Array
How to indicate whether a queue is full or empty?
A good choice is to use a counter (size):
The queue is empty when size = 0, and it is full when size = maxqueue
(capacity)
How to get the next position for the front and rear indicators?
The simplest way is using an if statement:
if (front== (capacity - 1))
front = 0;
else
front++;
//
if (rear == (capacity - 1))
rear = 0;
else
rear++;
Faculty of Information Technology - Computer Science Department 9
Methods Implementation
10
Faculty of Information Technology - Computer Science Department
Methods Implementation
11
Faculty of Information Technology - Computer Science Department
Main Class
Output:
Note that the 𝑒𝑛𝑞𝑢𝑒 operation was
accepted, although there is an element
at index (capacity-1).
Faculty of Information Technology - Computer Science Department 12