Double Ended Queue using Array
What is a Double Ended Queue (Deque)?
A Deque stands for Double Ended Queue.
It is a linear data structure in which insertion and deletion operations can be performed from both front and rear ends.
🔵 Unlike a simple queue (where insert at rear and delete from front only), in Deque:
-
Insert front ✅
-
Insert rear ✅
-
Delete front ✅
-
Delete rear ✅
Types of Deques
There are two common types:
-
Input Restricted Deque
-
Insertion at only one end .
-
Deletion from both ends.
-
Output Restricted Deque
-
Deletion at only one end.
-
Insertion from both ends.
-
Basic Operations in a General Deque:
Operation | Description |
---|---|
Insert Front | Insert an element at the front end. |
Insert Rear | Insert an element at the rear end. |
Delete Front | Delete an element from the front end. |
Delete Rear | Delete an element from the rear end. |
Peek Front | Get the front element without removing it. |
Peek Rear | Get the rear element without removing it. |
isFull() | Check if the deque is full. |
isEmpty() | Check if the deque is empty. |
Algorithms (assuming circular array with front = -1, rear = -1 initially)
Algorithm Initialize Deque
front = -1rear= -1
Algorithm for isEmpty()
1. If front ==-1 AND rear== -1
Return TRUE
2. Else
Return FALSE
Algorithm for isFull()
1. If (rear + 1) mod MAX = front then
Return TRUE
2. Else
Return FALSE
Algorithm for Insert Front-x is the element to be inserted
1. If Deque is Full
Output "Deque Overflow"
Exit
2. If Deque is Empty
Set front ← 0
Set rear ← 0
3. Else
if front==0
front=MAX-1
else
front ← front - 1
4. Set deque[front] ← x
1. If Deque is Full
Output "Deque Overflow"
Exit
2. If Deque is Empty
Set front ← 0
Set rear ← 0
3. Else
Set rear ← (rear + 1) mod MAX
4. Set deque[rear] ← item
Algorithm for Delete Front
1. If Deque is Empty
Output "Deque Underflow"
Exit
2. Output deque[front] as deleted element
3. If front = rear
Set front ← -1
Set rear ← -1
4. Else
Set front ← (front + 1) mod MAX
Algorithm for Delete Rear
1. If Deque is Empty
Output "Deque Underflow"
Exit
2. Output deque[rear] as deleted element
3. If front = rear
Set front ← -1
Set rear ← -1
4. Else
if rear==0
rear=MAX-1
else
rear ← rear - 1
Display Deque Elements
1. If Deque is Empty
Output "Deque is empty"
Exit
2. Set i ← front
3. Repeat
Output deque[i]
Set i ← (i + 1) mod MAX
if i==rear
break
Peek Front Element
1. If front = -1 then
Output "Deque is empty"
Exit
2. Else
Output deque[front] as the front element
Peek Rear Element
1. If rear = -1 then
Output "Deque is empty"
Exit
2. Else
Output deque[rear] as the rear element
Note:
-
Wrapping is handled using
% MAX
(modulus operator). -
In Deque, we can move front and rear both ways (increment/decrement).
-
You can design Deque using Arrays (fixed size) or Linked Lists (dynamic size).
#include <stdio.h>
#include <stdlib.h>
#define MAX 5
int deque[MAX];
int front = -1, rear = -1;
// Function to check if deque is full
int isFull() {
return ((rear + 1) % MAX == front);
}
// Function to check if deque is empty
int isEmpty() {
return (front == -1);
}
// Insert element at front
void insertFront(int item) {
if (isFull()) {
printf("Deque Overflow! Cannot insert at front.\n");
return;
}
if (isEmpty()) {
front = rear = 0;
} else { //front = (front - 1 + MAX) % MAX; you can also use this
if (front==0)
front=MAX-1;
else
front=front-1;
}
deque[front] = item;
printf("Inserted %d at front.\n", item);
}
// Insert element at rear
void insertRear(int item) {
if (isFull()) {
printf("Deque Overflow! Cannot insert at rear.\n");
return;
}
if (isEmpty()) {
front = rear = 0;
} else {
rear = (rear + 1) % MAX;
}
deque[rear] = item;
printf("Inserted %d at rear.\n", item);
}
// Delete element from front
void deleteFront() {
if (isEmpty()) {
printf("Deque Underflow! Cannot delete from front.\n");
return;
}
printf("Deleted element from front: %d\n", deque[front]);
if (front == rear) {
front = rear = -1; // Only one element was present
} else {
front = (front + 1) % MAX;
}
}
// Delete element from rear
void deleteRear() {
if (isEmpty()) {
printf("Deque Underflow! Cannot delete from rear.\n");
return;
}
printf("Deleted element from rear: %d\n", deque[rear]);
if (front == rear) {
front = rear = -1; // Only one element was present
} else { // rear = (rear - 1 + MAX) % MAX; you can also use this
if (rear==0)
rear=MAX-1;
else
rear=rear-1;
}
}
// Display deque elements
void display() {
int i;
if (isEmpty()) {
printf("Deque is empty.\n");
return;
}
printf("Deque elements: ");
i = front;
while (1) {
printf("%d ", deque[i]);
if (i == rear)
break;
i = (i + 1) % MAX;
}
printf("\n");
}
// Main function
int main() {
int choice, item;
while (1) {
printf("\n***** MENU *****\n");
printf("1. Insert Front\n");
printf("2. Insert Rear\n");
printf("3. Delete Front\n");
printf("4. Delete Rear\n");
printf("5. Display\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the element to insert at front: ");
scanf("%d", &item);
insertFront(item);
break;
case 2:
printf("Enter the element to insert at rear: ");
scanf("%d", &item);
insertRear(item);
break;
case 3:
deleteFront();
break;
case 4:
deleteRear();
break;
case 5:
display();
break;
case 6:
printf("Exiting program.\n");
exit(0);
default:
printf("Invalid choice! Please try again.\n");
}
}
return 0;
}
Comments
Post a Comment