DATA STRUCTURES PROGRAM • QUEUES & DEQUES
Implement a Deque with a Circular Array
Learn how to implement a deque with a circular array using a clear C program.
PROBLEM UNDERSTANDING
Input and expected output
Sample input
No input required
Sample output
Front = 5 Rear = 20
COMPLETE C PROGRAM
Complete C implementation
#include <stdio.h>
#include <stdlib.h>
void push_front(int deque[], int *front, int *count, int value)
{
if (*count == 6) exit(EXIT_FAILURE);
*front = (*front + 5) % 6;
deque[*front] = value; (*count)++;
}
void push_back(int deque[], int *front, int *count, int value)
{
if (*count == 6) exit(EXIT_FAILURE);
deque[(*front + *count) % 6] = value; (*count)++;
}
int main(void)
{
int deque[6] = {0}, front = 0, count = 0;
push_back(deque, &front, &count, 10);
push_back(deque, &front, &count, 20);
push_front(deque, &front, &count, 5);
printf("Front = %d\n", deque[front]);
int rear = (front + count - 1) % 6;
printf("Rear = %d\n", deque[rear]);
return 0;
}CURRENT STEP
SELECTED LINE
EXPECTED OUTPUT FOR THE SAMPLE
Front = 5 Rear = 20
Step 0 of 0
PROGRAM EXPLANATION
Algorithm and explanation
- Read the required input values.
- Use circular indices to allow insertion and removal at both ends.
- Display the computed result.
Use circular indices to allow insertion and removal at both ends.
EFFICIENCY
Time and space complexity
Time complexity
O(1) per operation
Auxiliary space
O(n)
DEBUGGING CHECKLIST
Common mistakes
Check this
Use the correct format specifier for every variable.
Check this
Initialize variables before using their values.
Check this
Check braces, semicolons and input order carefully.
Try it yourself
Practice: Run the program with the sample input, predict its output, and then test one boundary case of your own.
