0
0
DSA Pythonprogramming~30 mins

Circular Queue Implementation Using Array in DSA Python - Build from Scratch

Choose your learning style9 modes available
Circular Queue Implementation Using Array
📖 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 ' -> '.