5.1k views

Suppose a circular queue of capacity $(n −1)$ elements is implemented with an array of $n$ elements. Assume that the insertion and deletion operations are carried out using REAR and FRONT as array index variables, respectively. Initially, $REAR = FRONT = 0$. The conditions to detect queue full and queue empty are

1. full: $(REAR+1) \mod n == FRONT$
empty: $REAR == FRONT$

2.  full: $(REAR+1) \mod n == FRONT$
empty: $(FRONT+1) \mod n == REAR$

3.  full: $REAR == FRONT$
empty: $(REAR+1) \mod n == FRONT$

4.  full: $(FRONT+1) \mod n == REAR$
empty: $REAR == FRONT$
in DS
edited | 5.1k views
+4
Notice -> In this implementation we are wasting one unit of memory.
0
Yes, otherwise we can store n elements into a circular queue of size n

$\text{REAR} =\text{Write}$

$\text{FRONT} = \text{Read}$

full: $(\text{REAR}+1) \mod n == \text{FRONT}$

empty: $\text{REAR} == \text{FRONT}$

Only option (A) matches.

by Veteran (62.6k points)
edited
0
From the Given options it is clear that  Option A is Ans.

@Anirudh but

Initially when queue is empty @time Front=Rear=0

Now after Insertion of 1 element into empty Q at which index does front and rear point?? Can u plz explain??
+1
First of all, i did not understand - circular queue of size = n-1, while array has n elements ...
Does it mean that circular queue can only accomodate n-1 element or its index can varry from 0 to n-1 ??
0
Same [email protected] this was my next confusion ;p
+12
in this implementation of circular queue it is given that "Suppose a circular queue of capacity (n−1) elements is implemented with an array of n elements. " so it means that its index varies from 1 to n{array contains n slots} but it can accomodate n-1 elements
now initially rear=front=0 // means queue is empty
in this implementation rear is always points to an empty location where a new element will be inserted suppose n=10 front is at 6 and rear is at 5 this means currently 5 is an empty location and new element will be inserted at location 5 but before insertion at loc 5 it will check for full condition "(rear+1)modn=front" so for this case (5+1)mod 10=6 so this meets the full condition as  "(rear+1)modn=front"  but here is still an empty slot so that is the reason that why we can accomodate only n-1 elements.
and now if f=r so as i told u before that rear is always pointed to an empty location so at ths tme both front and rear pointing to an empty location so this meets the condtion that circular queue is empty.
-------- i hope tht this will help u if u have any doubt after then plzz refer to CLRS (Ed 3) Pg:234-235and excercise 10.1.4
+3
it means that the no of elements in the circular array in max on n-1 . Yes it means that the no of elements is of maximum of n-1 . this is done to make sure that rear==front means that the array is empty.

if we allow maximum of n elements in the circular array then if the circular array is full in that case rear would be equal to front. which by the convention means the array is empty.
so in order to avoid this confusion . we allow a maximum of n-1 elements.
0
@ Saurabh, front never points to any queue element right? Queue starts from front+1 , i mean if front is currently at say 4 and the size of queue is 5, then queue will contain elements at 5,0,1,2,3 right?
0

Prashant. this figure is not compitable with ques.

you take the array index from 0 to n now there will be n+1 slots are available in array but in ques  circular queue of capacity (n−1) elements is implemented with an array of n elements.

then how can u implement a circular queue of capacity (n−1) elements with an array of n+1elements.

initially rear=front=0 { means queue is empty}

if(rear=front=0) {

F++

R++}

1st element should be inserted at location 1

0
in case of just 1element in the queue front and rear both will be 1 ,   according to option A  if front=rear then queue is empty  .  here  contradiction    .... plz clear it
0
Can you please tell me how you are considering that rear will always point to the next empty location.As rear always point to the newly inserted element not to the next memory location of newly inserted element
0

If the capacity of the queue itself is "n-1", are we not supposed to use

(rear +1)mod(n-1) == front?

D -> This is incorrect, because if you have two elements in Queue of size 5, Front + 1 will not point to Rear. full: (FRONT+1) mod n == REAR is incorrect.

C-> full: REAR == FRONT This is wrong part. Even initially we had rear = front = 0 , does this means queue is full ?

B-> empty: (FRONT+1) mod n == REAR , Initially queuee is empty, still  this heuristic will say that queue is not empty. As Rear = front = 0, So Front + 1 % mod n = 1 != 0

A is correct option.

by Boss (41.4k points)
A will be the correct option just dry run it with smaller size of array..i did a programme on it earlier so i just answered to it by looking at it..
by Boss (14.4k points)
0
after one insertion the queue will not be empty and still rear and front will point to the same location?
+1

No.
FRONT will be pointing to the first item in the queue, while REAR will be pointing to the next empty location.

See this :

In Circular Queue

If Queue is full then first element should 0th element and last element should be n-1th element , lets assume an example if circular queue contains 5 element then :-

1st element should be 0th element or front =0 and last will be 4th element or rear =4

so according to option A :- full: (REAR+1) mod n == FRONT

(4+1) mod 5 = 0 = Front then this condition satisfies so option C and D eliminated

now we have to check 2nd condition of option A whether it is correct or not

empty: $REAR == FRONT$

As we know in queue

if Front = Rear = -1 then queue is Empty

So option B is eliminated

Option A will be right option

by Boss (11.2k points)
edited