I study the lines from the book. I had a problem while learning round robin. The author I'm learning from uses the following code snippet to explain how an element is inserted in a circular queue.
char *p[MAX];
int spos = 0;
int rpos = 0;
void qstore(char *q)
{
if(spos+1= =rpos || (spos+1==MAX && !rpos)) <--
{
printf(''List Full\n");
return;
}
p[spos] = q;
spos++;
if(spos==MAX)
spos = 0; /* loop back */
}
He further states that: the queue is full when the store index is one less than the load index; otherwise, there is room for another event in the queue.
SO acc. to the author, if spos (which contains the next free storage location index ) is 4 and rpos = 5, then the queue is full. Is that not so? Since spos = 3 means that the memory cell in p [3] is empty.
So, I decided to change the program.
#define MAX 100
char *p[MAX];
int spos = 0;
int rpos = 0;
void qstore(char *q)
{
if((spos+1==rpos) || (spos+1==MAX && rpos==0))
{
printf("Queue Full\n");
}
spos++
if((spos+1==MAX) && (rpos!=0))
{
spos=0;
}
else
{
spos++;
}
p[spos]=q;
}
Is my code correct?