0
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
PM 77-1
  • 12,933
  • 21
  • 68
  • 111
  • Do you know what `%` operator does? [What does the percentage symbol (%) mean?](https://stackoverflow.com/q/45771243) – Pshemo Jan 22 '18 at 19:32
  • 1
    Possible duplicate of [What does the percentage symbol (%) mean?](https://stackoverflow.com/questions/45771243/what-does-the-percentage-symbol-mean) – Bernhard Barker Jan 22 '18 at 19:33
  • And in a logically circular array, what do you need to do when advancing would take you past the end of the physical linear array? – Andy Thomas Jan 22 '18 at 19:34
  • Possible duplicate of [What's the syntax for mod in java](https://stackoverflow.com/questions/90238/whats-the-syntax-for-mod-in-java) – PM 77-1 Jan 22 '18 at 19:35
  • See also: [How could I have the index of an array 'roll over' when incrementing?](https://stackoverflow.com/q/6826826/5221149) – Andreas Jan 22 '18 at 19:37

4 Answers4

1

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.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • @Maseeh.Niazai You are welcome. Since the problem is solved, consider accepting the answer by clicking the gray check mark on the left. This would earn you a new badge on Stack Overflow. – Sergey Kalinichenko Jan 25 '18 at 12:28
0

% (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

0

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
Santiago Salem
  • 577
  • 2
  • 12
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.

E.g.
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.