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.

IntermediateQueueFIFOfront/rear

PROBLEM UNDERSTANDING

Input and expected output

Sample input
No input required
Sample output
Front = 5
Rear = 20

COMPLETE C PROGRAM

Complete C implementation

dsa-deque-circular-array.c
Open in compiler
#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;
}

GUIDED CODE TOUR • NOT LIVE EXECUTION

Study the program line by line

Use the real compiler button above to run and debug with different inputs.

CURRENT STEP

Select Start to walk through the important lines.

SELECTED LINE

No line selected

EXPECTED OUTPUT FOR THE SAMPLE

Front = 5
Rear = 20
0%Step 0 of 0

PROGRAM EXPLANATION

Algorithm and explanation

  1. Read the required input values.
  2. Use circular indices to allow insertion and removal at both ends.
  3. 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.