I come across this enqueue example from robert sedgewick's booksite http://algs4.cs.princeton.edu/13stacks/ResizingArrayQueue.java.html
public void enqueue(Item item) { // double size of array if necessary and recopy to front of array if (N == q.length) resize(2*q.length); // double size of array if necessary q[last++] = item; // add item if (last == q.length) last = 0; // wrap-around N++; } I don't know this code:
if (last == q.length) last = 0; // wrap-around Rapp around? Does this mean when our array is full it will begin replacing the previous items? Why would we do this? Shouldn't we throw an exception if the next item cannot be added?
And this is the last lines from the resize method:
q = temp; //now the array is doubled first = 0; last = n; //n is the original array size before doubling Since n and last will both be incremented in the enqueue method, and if n becomes equal to q.length the size of q will double, so it's like the if statement will never be true?