Q. Let us consider a queue is housed in an array in circular fashion or trend. It is required to add new items to the queue. Write down a method ENQ to achieve this also check whether the queue is full. Write down another procedure DQ to delete an element after checking queue empty status.
Ans.
The Method To Add an element in Circular Queue
# define MAXQUEUE 100 struct queue{
int items[MAXQUEUE];
int front, rear;
}
struct queue q;
q.front=q.rear=MAXQUEUE -1;
void ENQ(struct queue *pq, int x)
{
/* make room for new element*/
if(pq ->rear = MAXQUEUE - 1)
pq-> rear = 0;
else
(pq->rear)++;
/* check for overflow */
if(pq ->rear==pq->front)
{
printf("queue overflow);
exit(1);
}
pq->items[pq->rear]=x;
return;
}/* end of ENQ*/
A Method to Delete an element from Circular Queue
int DQ(struct queue *pq)
{
if(pq-> rear == pq-> front)
{
printf("queue underflow");
exit(1);
}/*end if*/
if(pq->front = = MAXQUEUE-1)
pq->front=0;
else
(pq->front)++;
return(pq->items[pq->front]);
}/*end DQ*/