Data Structures_ Queue to Circular Queue Notes pt01
Data Structures_ Queue to Circular Queue Notes pt01
III. Queue
Real-life example: Think of people standing in line for movie tickets. The person who came first gets
served first.
Show Image
Basic Operations:
Operation Description
// Global variables
int queue[MAX]; // Array to store queue elements
int front = -1; // Index of front element
int rear = -1; // Index of rear element
// Sample usage
int main() {
enqueue(10); // Add 10 to queue
enqueue(20); // Add 20 to queue
enqueue(30); // Add 30 to queue
return 0;
}
2. isEmpty() function:
Returns true if front is -1 (queue never started) or front > rear (all elements dequeued)
4. enqueue() function:
Check if queue is full
If not full:
If queue was empty (front == -1), set front to 0
Increment rear
5. dequeue() function:
Check if queue is empty
If not empty:
Get the item at front position
Increment front
If front crosses rear (last item dequeued), reset front and rear to -1
6. getFront() function:
Check if queue is empty
Memory Visualization:
A: A Queue is a linear data structure that follows the First-In-First-Out (FIFO) principle, meaning the element
that is inserted first will be the first one to be removed.
A: A Stack follows the Last-In-First-Out (LIFO) principle, where the last element added is the first one to be
removed. A Queue follows the First-In-First-Out (FIFO) principle, where the first element added is the first
one to be removed.
Traffic management
A: We can solve it by using a Circular Queue implementation, which reuses the space at the beginning after
dequeue operations.
The Problem:
3. The space at the beginning of the array (from index 0 to front-1) remains unused.
Illustration:
Initial state:
Now, if we try to enqueue a new element, we'll get "Queue Overflow" even though there are two unused
spaces at the beginning!
Solution:
Definition: A Circular Queue is a linear data structure in which operations are performed based on FIFO
principle and the last position is connected back to the first position to make a circle.
How it works:
1. When we reach the end of the array, we wrap around to the beginning.
2. We use modulo arithmetic ( % ) to implement this circular behavior.
Visualization:
front
↓
[10] [20] [30]
↑ ↑
rear → [50] [40] ← items
In this circular arrangement, after element 50, the next element would be at position 0 (making it circular).
// Global variables
int cqueue[MAX]; // Array to store circular queue elements
int front = -1; // Index of front element
int rear = -1; // Index of rear element
// Sample usage
int main() {
enqueue(10); // Add 10 to queue
enqueue(20); // Add 20 to queue
enqueue(30); // Add 30 to queue
enqueue(40); // Add 40 to queue
// Now we can add more elements even though the array is "full"
enqueue(50); // Add 50 to queue
enqueue(60); // Add 60 to queue
return 0;
}
2. isEmpty() function:
Returns true if front is -1 (queue never started)
Otherwise returns false
3. isFull() function:
Returns true if front is at position 0 AND rear is at last position (MAX-1)
4. enqueue() function:
Check if queue is full
If not full:
If queue was empty (front == -1), set front to 0
5. dequeue() function:
Check if queue is empty
If empty, display underflow message and return error value
If not empty:
Get the item at front position
6. getFront() function:
Check if queue is empty
If empty, display error message and return error value
Memory Visualization:
Array: [_, _, _, _, _]
Indices: 0 1 2 3 4
front = -1, rear = -1
A: A Circular Queue is a linear data structure that follows the FIFO principle and connects the last position to
the first position to form a circle, allowing better memory utilization by reusing the empty spaces.
A: Circular Queue solves the problem of inefficient space utilization in a regular queue implementation. In a
regular queue, after dequeuing elements, the space at the beginning cannot be reused without resetting the
entire queue, while a Circular Queue allows reusing those spaces.
A: Circular Queue implements wrapping around using the modulo operator (%). When incrementing front
or rear, it uses (index + 1) % MAX which makes the index go back to 0 after reaching MAX-1.
CPU scheduling
Memory management
Traffic system management
Q6: What is the difference between enqueue operation in regular Queue and Circular Queue?
A: In a regular Queue, the enqueue operation simply increments the rear index (rear++), while in a Circular
Queue, it uses modulo arithmetic to wrap around: rear = (rear + 1) % MAX .
Q7: What are the advantages of Circular Queue over regular Queue?
A: Advantages include:
Better memory utilization (reuses empty spaces)
No need to shift elements
Q8: Can a Circular Queue become full even if there are empty spaces?
A: No, that's the main advantage of a Circular Queue. It can detect and utilize any empty spaces, becoming
full only when all spaces are actually occupied.