This is really interesting because our instructor was teaching this to us yesterday and he couldn't figure it out himself. So, we're kinda left hanging on this without knowing the actual reason why.
Here is the array implementation of a Queue in a famous book (which I don't have, but that's what my instructor said. The author is very reputed):
class QUEUE {
private:
int* q;
int N;
int head;
int tail;
public:
QUEUE(int maxN) {
q = new int[maxN + 1];
N = maxN + 1; head = N; tail = 0;
}
int empty() const {
return head % N == tail;
}
void put(int item) {
q[tail++] = item; tail = tail % N;
}
int get() {
head = head % N; return q[head++];
}
};
Inside the constructor, you see q = new int[maxN + 1];
. But why the '+ 1' ? Why is he allocating one extra int block of memory?