Bird
0
0
DSA Cprogramming~30 mins

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

Choose your learning style9 modes available
Circular Queue Implementation Using Array
📖 Scenario: Imagine a small bakery that uses a circular queue to manage orders. The queue holds up to 5 orders at a time. When the queue is full, new orders wait until space is available. When an order is completed, it is removed from the queue, and the next order moves forward.
🎯 Goal: You will build a circular queue using an array in C. You will create the queue, add orders, remove orders, and finally print the queue state.
📋 What You'll Learn
Create an array of size 5 to hold the queue
Use two integer variables front and rear to track the queue ends
Implement enqueue operation to add an order
Implement dequeue operation to remove an order
Print the queue contents in order from front to rear
💡 Why This Matters
🌍 Real World
Circular queues are used in real-world systems like task scheduling, buffering data streams, and managing resources efficiently in limited space.
💼 Career
Understanding circular queues is important for software developers working on embedded systems, operating systems, and performance-critical applications.
Progress0 / 4 steps
1
Create the Circular Queue Data Structure
Create an integer array called queue of size 5, and two integer variables front and rear both initialized to -1.
DSA C
Hint

Use int queue[5]; to create the array. Set front and rear to -1 to show the queue is empty.

2
Add Helper Variables for Queue Size
Create an integer variable called max_size and set it to 5 to represent the maximum size of the queue.
DSA C
Hint

This variable helps to check when the queue is full or empty.

3
Implement Enqueue and Dequeue Operations
Write two functions: enqueue that takes an integer value and adds it to the queue, and dequeue that removes and returns the front value. Use front, rear, and max_size to handle wrapping around the array.
DSA C
Hint

Use modulo % to wrap rear and front around the array size.

4
Print the Queue Contents
Write a function called printQueue that prints all elements from front to rear in order, separated by spaces. Then, enqueue the values 10, 20, 30, dequeue one element, enqueue 40, and finally call printQueue.
DSA C
Hint

Loop from front to rear using modulo to print all elements. Then test by adding and removing elements as described.