What is a Double Ended Queue (Deque)?
-
A Double Ended Queue (Deque) is a linear data structure where insertion and deletion operations can be performed from both the front and rear ends.
-
It is more flexible than a normal queue (which only allows operations at one end).
✅ Think of it like a queue where you can push and pop from both sides.
✍️ Basic Operations in Deque
Operation | Meaning |
---|
insertFront(x) | Insert an element x at the front. |
insertRear(x) | Insert an element x at the rear. |
deleteFront() | Remove an element from the front. |
deleteRear() | Remove an element from the rear. |
display() | Display all elements from front to rear. |
How to Implement Deque Using Doubly Linked List
Since a doubly linked list already has next and prev pointers, it is ideal for deque operations.
Each node contains:
We maintain two pointers:
📚 Algorithms for Deque Operations
1. Insert at Front
-
Create a new node.
-
If deque is empty, front
and rear
point to the new node.
-
Else, set newNode->next = front
, front->prev = newNode
, and move front = newNode
.
2. Insert at Rear
-
Create a new node.
-
If deque is empty, front
and rear
point to the new node.
-
Else, set rear->next = newNode
, newNode->prev = rear
, and move rear = newNode
.
3. Delete from Front
-
If deque is empty, show underflow.
-
Else, move front = front->next
. If front
becomes NULL, set rear = NULL
too.
4. Delete from Rear
-
If deque is empty, show underflow.
-
Else, move rear = rear->prev
. If rear
becomes NULL, set front = NULL
too.
5. Display
C Program Deque Implemenation
#include <stdio.h>
#include <stdlib.h>
// Node structure
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
struct Node *front = NULL, *rear = NULL;
// Insert at front
void insertFront(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->prev = NULL;
newNode->next = front;
if (front == NULL)
rear = newNode;
else
front->prev = newNode;
front = newNode;
printf("%d inserted at front\n", value);
}
// Insert at rear
void insertRear(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
newNode->prev = rear;
if (rear == NULL)
front = newNode;
else
rear->next = newNode;
rear = newNode;
printf("%d inserted at rear\n", value);
}
// Delete from front
void deleteFront() {
if (front == NULL) {
printf("Deque is empty\n");
return;
}
struct Node* temp = front;
printf("Deleted %d from front\n", temp->data);
front = front->next;
if (front == NULL)
rear = NULL;
else
front->prev = NULL;
free(temp);
}
// Delete from rear
void deleteRear() {
if (rear == NULL) {
printf("Deque is empty\n");
return;
}
struct Node* temp = rear;
printf("Deleted %d from rear\n", temp->data);
rear = rear->prev;
if (rear == NULL)
front = NULL;
else
rear->next = NULL;
free(temp);
}
// Display the deque
void display() {
if (front == NULL) {
printf("Deque is empty\n");
return;
}
struct Node* temp = front;
printf("Deque elements: ");
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
// Main function
int main() {
int choice, value;
while (1) {
printf("\n--- Deque Menu ---\n");
printf("1. Insert at Front\n");
printf("2. Insert at Rear\n");
printf("3. Delete from Front\n");
printf("4. Delete from Rear\n");
printf("5. Display\n");
printf("6. Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to insert at front: ");
scanf("%d", &value);
insertFront(value);
break;
case 2:
printf("Enter value to insert at rear: ");
scanf("%d", &value);
insertRear(value);
break;
case 3:
deleteFront();
break;
case 4:
deleteRear();
break;
case 5:
display();
break;
case 6:
exit(0);
default:
printf("Invalid choice\n");
}
}
return 0;
}
Comments
Post a Comment