I'm kind of confused about how the modulus operator works.
Suppose i have x % y with x > y. So if x was 6 and y was 2, then 6 % 2 would produce 0.
But i don't really get what happens when x < y :eek:
In this program some crazy **** happens that's gettin me a bit confused here.
The book says that the program basically wraps around the queue back to 0 when the end of the array is encountered.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define LEN 100
void initqueue(int);
void put(char);
char get();
void queueerror();
static char *q;
static int N, head, tail, cnt;
int main(void)
{
char input[LEN];
puts("Enter a string:");
fgets(input, LEN, stdin);
int maxN = strlen(input);
initqueue(maxN);
char *token, delim[] = " ";
token = strtok(input, delim);
while (token != NULL)
{
if (isalnum(*token))
put(*token);
if (*token == '*')
printf("I released %c\n", get());
token = strtok(NULL, delim);
}
return 0;
}
void initqueue(int maxN)
{
q = (char *)malloc((maxN+1)*sizeof(char));
N = maxN + 1; head = N;
}
void put(char item)
{
q[tail++] = item;
tail = tail % N;
}
char get()
{
head = head % N;
return q[head++];
}
void queueerror()
{
puts("Stack is either full or empty!");
exit(0);
}