When we don’t have any element then the rear should be -1 then why we are assign the rear pointer to the n-1 index?
Rear pointer In the Queue
hi @Vikaspal
as we are implementing queue using circular array
so we can’t intialize rear with -1
because -1 is not previous to 0
last index is previous to 0 so we point rear to last index
this will be more clear after understanding enqueue operation
bool enqueue(){
if(!isFull){
rear=(rear+1)%ms;
arr[rear]=data;
}
}
now for7 size array we point rear to 7-1=6
so before inserting first element
we set rear=(rear+1)%ms
so rear=(6+1)%7=0 hence we are inserting first element at 0th index
but if you set rear=-1 then this will not holds true
i hope this help
if you have more doubts regarding this feel free to ask
if your doubt is resolved mark it as resolved from your doubt section inside your course