C Program to Implement Double-Ended Queue

The below program demonstrates all the major operations of a double-ended queue: insertion (at the front and at the rear), deletion (at the front and at the end), check empty and check full.

C
#include <stdio.h>
#include <stdlib.h>

#define MAX 5  // Define maximum size of the deque

int deque[MAX];
int front = -1;
int rear = -1;

// Function to check if the deque is full
int isFull() {
    return ((front == 0 && rear == MAX - 1) || (front == rear + 1));
}

// Function to check if the deque is empty
int isEmpty() {
    return (front == -1);
}

// Function to insert an element at the front of the deque
void insertFront(int key) {
    if (isFull()) {
        printf("Overflow: Unable to insert element at the front. Deque is full.\n");
        return;
    }

    if (front == -1) {  // If deque is initially empty
        front = 0;
        rear = 0;
    } else if (front == 0) {
        front = MAX - 1;  // wrap around
    } else {
        front = front - 1;
    }

    deque[front] = key;
    printf("Inserted %d at the front.\n", key);
}

// Function to insert an element at the rear of the deque
void insertRear(int key) {
    if (isFull()) {
        printf("Overflow: Unable to insert element at the rear. Deque is full.\n");
        return;
    }

    if (rear == -1) {  // If deque is initially empty
        front = 0;
        rear = 0;
    } else if (rear == MAX - 1) {
        rear = 0;  // wrap around
    } else {
        rear = rear + 1;
    }

    deque[rear] = key;
    printf("Inserted %d at the rear.\n", key);
}

// Function to delete an element from the front of the deque
void deleteFront() {
    if (isEmpty()) {
        printf("Underflow: Unable to delete element from the front. Deque is empty.\n");
        return;
    }

    int removed = deque[front];

    if (front == rear) {  // Deque has only one element
        front = -1;
        rear = -1;
    } else if (front == MAX - 1) {
        front = 0;  // wrap around
    } else {
        front = front + 1;
    }

    printf("Deleted %d from the front.\n", removed);
}

// Function to delete an element from the rear of the deque
void deleteRear() {
    if (isEmpty()) {
        printf("Underflow: Unable to delete element from the rear. Deque is empty.\n");
        return;
    }

    int removed = deque[rear];

    if (front == rear) {  // Deque has only one element
        front = -1;
        rear = -1;
    } else if (rear == 0) {
        rear = MAX - 1;  // wrap around
    } else {
        rear = rear - 1;
    }

    printf("Deleted %d from the rear.\n", removed);
}

// Function to display the deque
void displayDeque() {
    if (isEmpty()) {
        printf("Deque is empty.\n");
        return;
    }

    printf("Deque elements are: ");
    int i = front;
    while (1) {
        printf("%d ", deque[i]);
        if (i == rear)
            break;
        i = (i + 1) % MAX;
    }
    printf("\n");
}

// Main function to test the operations
int main() {
    insertRear(5);
    displayDeque();

    insertFront(15);
    displayDeque();
    
    insertRear(25);
    displayDeque();

    deleteFront();
    displayDeque();

    deleteRear();
    displayDeque();


    return 0;
}


Output

Inserted 5 at the rear.
Deque elements are: 5 
Inserted 15 at the front.
Deque elements are: 15 5 
Inserted 25 at the rear.
Deque elements are: 15 5 25 
Deleted 15 from the front.
Deque elements are: 5 25 
Deleted 25 from the rear.
Deque elements are: 5

Time Complexity: O(1), as InsertRear(), InsertFront() , DeleteRear(), DeleteFront() have time complexity of O(1) (Constant).
Auxiliary Space: O(n)

C implementation Double-Ended Queue

The double-ended queues, called deques for short, are a generalized form of the queue. It is exactly like a queue except that it does not follow the FIFO rule (First in first out) so, the elements can be added to or removed from either the front(head) or back(tail) of the deque.

In this article, we will learn about the double-ended queue implementation in C. We will also look at the working of the double-ended queue and the basic operations that can be performed using the double-ended queue in C.

Similar Reads

Double-Ended Queue Representation in C

In a deque we maintain two pointers front and rear, that point to either end of the deque. The elements in a deque extend from the front end to the rear end and since it is circular, dequeue[n–1] is followed by dequeue[0]....

Types of Deque

There are two variants of a double-ended queue. They include :...

Implementation of Double-Ended Queue in C

Dequeues can be implemented using two data structures....

Basic Operations on C Double-Ended Queue

We can perform the following basic operations in a double-ended queue:...

C Program to Implement Double-Ended Queue

The below program demonstrates all the major operations of a double-ended queue: insertion (at the front and at the rear), deletion (at the front and at the end), check empty and check full....

Applications of Deque

It can be used in job scheduling algorithm called A-Steal algorithm.It is be used for to store a software application’s list of undo operations.It can be used to implement both stack and queue.It can also be used to store the browsing history that includes recently visited URLs.It can be used in graph traversal BFS to store nodes and perform operations such as adding or removing nodes from both ends of the deque....

Advantages of Deque

It allows us to add and remove elements from both ends, hence providing more flexibility than a regular queue or stack.It can be used to implement both stacks and queues.It can efficiently provide the functionality of both LIFO and FIFO data structures.It is efficient as it takes O(1) time complexity to work with both ends.Deques are dynamic in size an d can grow and reduce in size dynamically.It is cache friendly as deque have cache-friendly contiguous subsequence....

Disadvantages of Deque

Deque is less memory efficient than a normal queue.It can cause synchronization issues multi-thread.It may not be supported by all platforms in such we need to implement it manually.It has limited functionality as compared to other data structures.Not suitable for sorting or searching, as these operations require linear time....

Frequently Asked Questions on Double-Ended Queue

What is a Deque in C?...

Contact Us