Bird
0
0
DSA Cprogramming~30 mins

Double Ended Queue Deque in DSA C - Build from Scratch

Choose your learning style9 modes available
Double Ended Queue (Deque) Implementation in C
📖 Scenario: Imagine you are managing a line of customers at a store where people can join or leave from both the front and the back of the line. This line is called a double ended queue or deque.
🎯 Goal: You will build a simple double ended queue (deque) using an array in C. You will add elements to the front and back, remove elements from the front and back, and finally print the current state of the deque.
📋 What You'll Learn
Create an array-based deque with fixed size 5
Use two integer variables front and rear to track ends
Implement functions to add to front and rear
Implement functions to remove from front and rear
Print the deque elements from front to rear
💡 Why This Matters
🌍 Real World
Deques are used in real-world scenarios like managing tasks where you can add or remove from both ends, such as undo-redo operations or browser history.
💼 Career
Understanding deque implementation helps in software development roles where efficient data handling and queue management are required.
Progress0 / 4 steps
1
Create the deque data structure
Create an integer array called deque with size 5. Also create two integer variables called front and rear and set both to -1.
DSA C
Hint

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

2
Add elements to the rear of the deque
Write a function called addRear that takes an integer value and adds it to the rear of the deque. Update rear and front accordingly when adding the first element.
DSA C
Hint

Check if rear reached 4 (end of array). If empty, set front to 0. Then increase rear and store the value.

3
Add elements to the front of the deque
Write a function called addFront that takes an integer value and adds it to the front of the deque. Update front accordingly. If the deque is empty, set front and rear to 0.
DSA C
Hint

If front is 0, no space to add front. If empty, set both front and rear to 0 and add value. Otherwise, decrease front and add value.

4
Print the current deque elements
Write a function called printDeque that prints all elements from front to rear separated by spaces. Then call addRear(10), addRear(20), addFront(5), and finally print the deque.
DSA C
Hint

Loop from front to rear and print each element with a space. Call the add functions as instructed before printing.