Head Meets Tail
Open Data Structures implements ArrayQueue with three fields: the backing array a, a head index j, and a count n. add(x) writes a[(j + n) % a.length] then increments n; remove() reads a[j], advances j, and decrements n.
An engineer drops n to save a word and tracks a tail index instead:
int a[8];
int head = 0, tail = 0;
bool empty(void) { return head == tail; }
void push(int x) { a[tail] = x; tail = (tail + 1) % 8; }
int pop(void) { int x = a[head]; head = (head + 1) % 8; return x; }
Eight push calls run with no pop. What is the state?
Sign in to answer questions and track your progress
Sign In