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.
#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.
Contact Us