1

This is the part of a program for implementing queue with ring buffer in C. And I don't understand the line 8-9. What exactly does this line if(q->rear == q->max) q->rear = 0; mean?

if the rear index equals the max capacity... then assign zero to rear? T_T Help me please!

int Enque(IntQueue* q,int x)
{
    if (q->num >= q->max)
        return -1;
    else {
        q->num++;
        q->que[q->rear++] = x;
        if(q->rear == q->max)
            q->rear = 0;
        return 0;
    }
}

1 Answers1

1

q->que[] is an array of integers. Individual array q->que[] integer elements are accessed by specifying their index in the array such as: q->que[n]; where n is a value from 0 to (q->max - 1).

q->rear represents an index into the array q->que[]. The value of q->rear may be anywhere from 0 through (q->max -1). Hence, if q->rear ever becomes equal to q->max, it would represent an index that is beyond the end of the q->que[] array, and (being a circular queue) must be positioned back to the beginning of the array (q->que[0]).

Hence, the logic of:

if (q->rear == q->max)
    q->rear = 0;
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Mahonri Moriancumer
  • 5,993
  • 2
  • 18
  • 28
  • 1
    Please use the backticks for code excerpts in flowing text – Antti Haapala -- Слава Україні May 12 '19 at 06:50
  • Oh, thank you for very useful comment! Now I understand a bit, but if the rear index becomes 'zero', then the rear itself has nothing to do after then, right? And front index is still there where it used to be? – Juyoung Park May 12 '19 at 06:55
  • @JuyoungPark, The code doesn't indicate the existence of a front index. Perhaps the definition of the IntQueue includes a front pointer, as well as the rear pointer. Obviously, the code provided does not manipulate what is not referenced. – Mahonri Moriancumer May 12 '19 at 06:59
  • @JuyoungPark, the purpose of the Enque() function seems to be to store an integer in a circular ring buffer. Each call to Enque() will store the integer (supplied by the parameter x) in the ring buffer at index q->rear; and then advance (increment) q->rear. When q->rear reaches the end of the q->que[] array, it circles back around so the calling Enque() will store the next integer (x) at the beginning of the array (offset 0). After that next call to Enque() will store the integer at offset 1, then 2, 3, and so-on. – Mahonri Moriancumer May 12 '19 at 07:12
  • @JuyoungPark, most likely there is another function that reads the integers out of the ring buffer at q->front, and then advances (increments) q->front to the next index. Like q->rear, q->front circles back to 0 when it reaches q->max. Hence, que->front chases q->rear around the ring buffer like a dog chasing it's tail. – Mahonri Moriancumer May 12 '19 at 07:16
  • @JuyoungPark, of course, it is important that q->rear never catches up to q->front, and over-writes integers that have not yet been read by the circular buffer's read function. To keep this from happening, the actual number of integers currently stored in the circular queue is indicated in q->num. This is why the value of q->num is checked before manipulating the circular queue, and why the value of q->num is incremented when a new integer value (x) is successfully stored in the queue. Most likely, the circular queue read function decrements q->num each time an integer is read. – Mahonri Moriancumer May 12 '19 at 07:23