Write a program to implement four numbers of D-queues in an array.
#include <stdio.h>
#include<stdlib.h>
#define MAX 50
void insert();
void delete();
void display();
int Q_arr[MAX];
int end = - 1;
int top = - 1;
int main()
{
int option;
while (1)
{
printf("\n1.Insert \n");
printf("2.Delete \n");
printf("3.Display\n");
printf("4.Quit \n");
printf("\nEnter your option: ");
scanf("%d", &option);
switch (option)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("\nWrong option \n");
}
}
return 0;
}
void insert()
{
int add_item;
if (end == MAX - 1)
printf("Queue Overflow \n");
else
{
if (top == - 1)
top = 0;
printf("\nInset the element: ");
scanf("%d", &add_item);
end = end + 1;
Q_arr[end] = add_item;
}
}
void delete()
{
if (top == - 1 || top > end)
{
printf("\nQueue Underflow \n");
return ;
}
else
{
printf("\nElement deleted from queue is : %d\n", Q_arr[top]);
top = top + 1;
}
}
void display()
{
int i;
if (top == - 1)
printf("\nQueue is empty \n");
else
{
printf("\nQueue is : ");
for (i = top; i <= end; i++)
printf("%d ", Q_arr[i]);
printf("\n");
}
}
Comments
Leave a comment