📖 Scenario: Imagine you are managing a small parking lot with a fixed number of parking spots arranged in a circle. Cars enter and leave in order, and when the last spot is reached, the next car goes back to the first spot if it is free. This is like a circular queue.
🎯 Goal: You will build a simple circular queue using an array to manage the parking spots. You will add cars, remove cars, and show the current state of the parking lot.
📋 What You'll Learn
Create an array to hold the queue elements with a fixed size
Use two pointers: front and rear to track the queue
Implement enqueue operation to add a car if space is available
Implement dequeue operation to remove a car if the queue is not empty
Print the current queue elements in order
💡 Why This Matters
🌍 Real World
Circular queues are used in real-world systems like traffic lights, CPU scheduling, and buffering data streams where resources are reused in a cycle.
💼 Career
Understanding circular queues helps in roles involving system design, embedded systems, and performance optimization where fixed-size buffers are common.
Progress0 / 4 steps
1
Create the Circular Queue Array and Pointers
Create a list called queue with 5 None elements to represent empty spots. Also create two variables front and rear and set both to -1 to indicate the queue is empty.
DSA Python
Hint
Use a list with 5 None values. Set front and rear to -1 to show the queue is empty.
2
Add a Car to the Circular Queue (Enqueue)
Create a function called enqueue(car) that adds car to the queue. Update rear to the next position in a circular way using modulo. If the queue is empty, set front to 0. Do not add if the queue is full (when next rear equals front).
DSA Python
Hint
Check if queue is full by comparing next rear position with front. Use modulo to wrap around. Update front if queue was empty.
3
Remove a Car from the Circular Queue (Dequeue)
Create a function called dequeue() that removes and returns the car at front. If the queue becomes empty after removal, reset front and rear to -1. Otherwise, move front to the next position circularly.
DSA Python
Hint
Check if queue is empty first. Remove the car at front. If only one car was there, reset pointers. Otherwise, move front forward using modulo.
4
Print the Current Circular Queue
Write code to print the current elements in the queue from front to rear in order, separated by ' -> '. If the queue is empty, print 'Queue is empty'.
DSA Python
Hint
Start from front and keep moving forward using modulo until you reach rear. Collect elements and join them with ' -> '.
Practice
(1/5)
1. What is the main advantage of using a circular queue over a simple linear queue when implemented using an array?
easy
A. It efficiently uses the available space by reusing freed slots.
B. It allows storing different data types in the same queue.
C. It automatically sorts the elements in the queue.
D. It eliminates the need for front and rear pointers.
Solution
Step 1: Understand linear queue limitation
In a linear queue using an array, once rear reaches the end, no more elements can be added even if there is space at the front.
Step 2: Circular queue reuses space
Circular queue connects the end of the array back to the front, allowing reuse of freed slots and efficient space usage.
Final Answer:
It efficiently uses the available space by reusing freed slots. -> Option A
Quick Check:
Space reuse = Advantage [OK]
Hint: Circular queue reuses space unlike linear queue [OK]
Common Mistakes:
Thinking circular queue sorts elements
Believing circular queue removes need for pointers
Assuming it stores multiple data types automatically
2. Which of the following is the correct way to check if a circular queue is full when using an array of size max_size with front and rear pointers?
easy
A. if rear == max_size - 1
B. if rear == front
C. if front == -1
D. if (rear + 1) % max_size == front
Solution
Step 1: Understand full condition in circular queue
The queue is full when the next position of rear (circularly) is front.
Step 2: Use modulo for circular increment
Using modulo operator, (rear + 1) % max_size gives the next position circularly.
Final Answer:
if (rear + 1) % max_size == front -> Option D
Quick Check:
Full condition = (rear + 1) % max_size == front [OK]
Hint: Full when next rear equals front using modulo [OK]
Common Mistakes:
Checking rear == front means full (actually empty)
Using rear == max_size - 1 ignores circular wrap
Checking front == -1 means empty, not full
3. Consider this circular queue code snippet with max_size = 5: