public class Queue {
private int [] queue ; // here we define an array for queue
private int size , rear ,front; // it's size front and rear
private static final int CAP = 15; // default capacity
public Queue(int cap){
queue = new int[cap];
size = 0;
front = 0;
rear = 0;
}// end of Queue
public void enQueue(int data){
if(size == CAP) // size of queue is full
System.out.println("Queue is full");
else{
size++; // first we increment the size because it's zero
queue[rear] = data;// and add data to rear of circular array
rear = ( rear + 1 ) % CAP;
/* **but here i don't know this line of code could some one please help me here **
i don't know why here they take % of ( rear + 1 ) of circular array
---------------------------------------------------------
*/
} // end of else
}// end of enQueue
This assignment
rear = ( rear + 1 ) % CAP;
advances rear
by one, and drops it back to zero if it reaches CAP
using a single expression. You can rewrite this using two operations:
rear++;
if (rear == CAP) {
rear = 0;
}
%
is modulo operator. When rear + 1
is under CAP
, you get rear + 1
. Once rear + 1
reaches CAP
, modulo produces zero.
Using conditional requires no prior knowledge to read. However, once you know the modulo trick, it becomes easy to understand as well.
% (Modulo) operation gives the reminder of the operation. Here, rear will gets assigned with a value which always less than the capacity. So to make sure that, % being used here
You are implementing a queue using a circular array. When you start adding elements, you should move to the following cell, where the next element will be added.
If you have a capacity of 15, then if you aplied the operator %
to a number between 0 to 14, all you get is the same number.
2 % 15 --> 2
14 % 15 --> 14
However, in certain point you should get back to the start of the array. If you don´t, you will get an ArrayIndexOutOfBoundsException
. Then,
15 % 15 --> 0
You always dequeue at the front and enqueue at the rear. The nature of a circular queue is such that it wraps around. If you have 7 slots then the "8th slot" is 0 (the beginning). If you get back to the front then it's full. Both the front and rear indexes/pointers move though because when you dequeue the front moves and when you enqueue the rear moves.
The code you have is trying to solve going beyond CAP by using the modulus operator. That is it just keeps wrapping the rear around while allowing the actual value of rear to keep increasing. Without any bound checking this will keep overwriting forever.
Eg
if rear=14 then rear+1=15
( rear + 1 ) % CAP -> (14 + 1) % CAP -> (15) % 15 = 0
This link may be useful to you:
https://www.geeksforgeeks.org/circular-queue-set-1-introduction-array-implementation/
^ suggest you have another go before looking at the code it shows if this is for a class. otherwise look at the first picture and then skip to the code for a better understanding.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.