FULL CONDITION FOR A QUEUE

In a circular queue of size n, do we declare the queue full if we have n-1 items in it? Why do we do so if we can declare it full when it is actually full, i.e. when it has n items?

P.S.- if u want to say that we do this because we cannot differentiate between the empty and full condition otherwise, then i want to say that i think it is possible according to the code i wrote.
Last edited on
Let's see the code.
just wait for a sec..m cming up with the algo
f-front, r-rear, x-element to be queued, n=size of queue

initially, f=-1 r=-1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
enqueue(x)
{
   if (queue is full)
       print overflow
   else
       if (f==-1)
          f++,r++
       else
          r=(r+1)%n , queue[r]=x
}

dequeue(x)
{
   if(queue is empty)
      print underflow
   else
      queue[f]=null
      if(f==r)
          f=-1, r=-1
      else
          f=(f+1)%n
}


full condition: if (f==(r+1)%n)

empty condition: if (f==-1 && r==-1)
Last edited on
is the concept correct???..so it is possible right??
Topic archived. No new replies allowed.