UNIT III-Circular Queue
UNIT III-Circular Queue
CIRCULAR QUEUES
• Circular Queue is a variation of the
traditional linear queue, where the
last element connects back to the
first element, forming a circular
structure.
• This ensures continuous use of
memory and efficient space
utilization.
• The front and rear pointers wrap
around the array, allowing us to
enqueue elements at the rear and
dequeue elements from the front
while keeping the logical order
intact.
Need for Circular Queue
• As elements are dequeued from the front, the queue's front pointer moves
forward, leaving unused memory space at the beginning. This inefficient memory
usage can be overcome with the Circular Queue.
Circular Queue Operations
• Enqueue: Adding an element to the rear of the queue.
• Dequeue: Removing an element from the front of the queue.
• Front: Accessing the front element of the queue without removing it.
• Rear: Accessing the rear element of the queue without removing it.
• IsEmpty: Checking if the queue is empty.
• IsFull: Checking if the queue is full.
Circular Queue Implementation
CircularQueue:
max_size // maximum size of the Circular Queue
queue // an array to hold the elements of the Circular Queue
front // pointer to the front of the Circular Queue
rear // pointer to the rear of the Circular Queue
Constructor to initialize the Circular Queue
CircularQueue(max_size):
max_size = max_size
queue = [None] * max_size
front = rear = -1
Function to check if the Circular Queue is empty
is_empty():
return front == -1
Function to check if the Circular Queue is full
is_full():
return (rear + 1) % max_size == front
Enqueue
enqueue(data):
if is_full():
// The Circular Queue is full, enqueue operation cannot be performed
return "Queue is full"
if is_empty():
// If the Circular Queue is empty, set both front and rear to 0
front = rear = 0
else:
// Increment the rear pointer in a circular manner
rear = (rear + 1) % max_size
// Add the new element to the rear of the Circular Queue
queue[rear] = data
return "Enqueued element"
Dequeue
dequeue():
if is_empty():
// The Circular Queue is empty, dequeue operation cannot be performed
return "Queue is empty"
// Get the element to dequeue
data_to_dequeue = queue[front]
if front == rear:
// If there is only one element, reset front and rear to -1 to indicate an empty queue
front = rear = -1
else:
// Increment the front pointer in a circular manner
front = (front + 1) % max_size
return data_to_dequeue
Front element of the Circular
Queue
peek_front():
if is_empty():
// The Circular Queue is empty, no front element to peek
return "Queue is empty"
return queue[front]
Rear element of the Circular
Queue
peek_rear():
if is_empty():
// The Circular Queue is empty, no rear element to peek
return "Queue is empty"
return queue[rear]
Advantages of Circular Queue
1. Efficient Memory Utilization: Circular Queues overcome the issue
of wasted space by reusing the vacant slots.
2. Simplicity in Implementation: They can be implemented using
arrays, which are contiguous blocks of memory.
3. Continuous Data Flow: Circular Queues allow continuous data
flow without the need for resetting the front pointer.
Applications of Circular Queues
• Printer Spooling: Managing print jobs in a printer queue.
• CPU Task Scheduling: Scheduling tasks in operating systems.
• Buffer Management in Networking: Handling data packets in
network routers.
Review Questions
1. Explain the concept of a circular queue? How is it better than a linear queue?
2. Draw the queue structure in each case when the following operations are performed on an empty queue.
(a)Add A, B, C, D, E, F. (b) Delete two letters
(c) Add G (d) Add H
(e) Delete four letters (f) Add I
3. The circular queue will be full only when _____.
(a) FRONT=MAX-1 and REAR=MAX-1 (b) FRONT=0 and REAR=MAX-1
(c) FRONT=MAX-1 and REAR=0. (d) FRONT=0 and REAR=0
4. The function that deletes values from a queue is called ______.
(a) Enqueue (b)dequeue
(c) Pop (d) peek
5. New nodes are added at of the queue.
6. _________ allows insertion of elements at either ends but not in the middle.
7. A queue is a _____ data structure in which the element that is inserted first is the first one to be taken out.
8. In a circular queue, the first index comes after the ______ index.
9. In the computer’s memory, queues can be implemented using _________ and _______.
10. The storage requirement of linked representation of queue with n elements is ____ and the typical time
requirement for operations is _______.