# Program to implement a Queue using Array with front always at zero

## Queues

### Program to implement a Queue using Array with front always at zero

/*Implementation of queues using arrays*/
/*Queue implementation with front always at zero*/

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

#define MAX 10

typedef struct queue
{int front, rear;
int arr[MAX];
}queue;

/*Enqueue will increment rear by one and add new element
at this subscript*/

void enqueue(queue *q, int x)
{if(q->rear==MAX-1)
{printf(“Queue Overflow!”);
exit(1);
}
q->arr[++q->rear]=x;
}

/*Front is always kept at zero.
Deletion involves shifting of elements to the left by one
position and rear gets decremented by one*/

int dequeue(queue *q)
{
int i,no;
if(q->rear<q->front)
{printf(“Queue Underflow!”);
exit(1);
}
no=q->arr[q->front];
/*fixed front at zero results in shifting of elements*/
for(i=q->front;i<q->rear;i++)
q->arr[i]=q->arr[i+1];
q->arr[q->rear]=0;
q->rear–;
return no;
}

void display(queue *q)
{int i;
for(i=q->front;i<=q->rear;i++)
printf(“%dt”,q->arr[i]);
}

void main()
{
int ch,num;
queue q;
q.front=0;
q.rear=-1;
while(1)
{
clrscr();
printf(“nn1. Enqueue”);
printf(“n2. Dequeue”);
printf(“n3. Display”);
printf(“n4. Exit”);
scanf(“%d”,&ch);
switch(ch)
{
case 1:
printf(“nEnter number?”);
scanf(“%d”,&num);
enqueue(&q,num);
break;
case 2:
num=dequeue(&q);
printf(“nElement removed from the queue is %d”,num);
break;
case 3:
display(&q);
break;
case 4:
exit(0);
default:
printf(“nInvalid choice!”);
}
getch();
}
}