Professional Documents
Culture Documents
q.item
4
3
2 C q.rear=2
1 B
0 A q.front=0
A,B & C inserted
A, B deleted
4
3
2 C q.rear=2, q.front=2
1
0
D & E inserted
4 E q.rear=4
3 D
2 C q.front=2
1
0
C & D deleted
4 E q.rear=4, q.front=4
3
2
1
0
E deleted
4 q.rear=4, q.front=5
3
2
1
0
Now, if we attempt to add more elements, the elts cannot be inserted because in a queue the new
elts are always inserted from the rear. Here, rear indicates to last location of the array(q.rear=4).
Thus, although there is space in the queue we are unable to insert the elts. To remove this problem,
we use circular queue.
Circular Queue:
Physically, a circular array is same as ordinary array, say a[n], but logically it implies that a[0] comes
after a[n-1] or after a[n-1], a[0] appears.
#include<stdio.h>
#include<conio.h>
#define MAX 5
struct queue_tag
{
int rear;
int front;
int items[MAX];
};
typedef struct queue_tag queue ;
void main()
{
queue *q;
q1.front=-1;
q1.rear=-1;
}
int Isempty(queue *q)
{
If(q->front==-1)
return 1;
else
return 0;
}
Int Isfull(queue *q)
{
If(q->front==(q->rear+1)%MAX)
return 1;
else
return 0;
}
void insert(queue *q, int x)
{
if(Isfull(q))
{
printf(“overflow”);
return;
}
if((q->front==-1)
{
q->front=0;
q->rear=0;
}
else
{
q->rear=(q->rear+1)%MAX;
}
q->items[q->rear]=x;
}