Double Ended Queue
Double Ended Queue
Double Ended Queue
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
int dequeue[MAX];
int front = -1;
int rear = -1;
void deleteFront()
{
if (front == -1)
{
printf("Dequeue is empty\n");
return;
}
printf("Deleted %d from the front\n", dequeue[front]);
if (front == rear)
{
front = rear = -1;
}
else
{
front++;
}
}
void deleteRear()
{
if (rear == -1)
{
printf("Dequeue is empty \n");
return;
}
printf("Deleted %d from the rear\n", dequeue[rear]);
if (front == rear)
{
front = rear = -1;
}
else
{
rear--;
}
}
void display()
{
if (front == -1)
{
printf("Dequeue is empty \n");
return;
}
printf("Dequeue elements: ");
for (int i = front; i <= rear; i++)
{
printf("%d ", dequeue[i]);
}
printf("\n");
}
int main()
{
int choice, value;
while (1)
{
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 dequeue\n");
printf("6. Exit\n");
printf("Enter your 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:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid input.\n");
}
}
return 0;
}
Output: