Trying not to lose it here. As you can see below I have assigned intFrontPtr to point to the first cell in the array. And intBackPtr to point to the last cell in the array…:
bool quack::popFront(int& nPopFront)
{
nPopFront = items[top+1].n;
if ( count >= maxSize ) return false;
else
{
items[0].n = nPopFront;
intFrontPtr = &items[0].n;
intBackPtr = &items[count-1].n;
}
for (int temp; intFrontPtr < intBackPtr ;)
{
++intFrontPtr;
temp = *intFrontPtr;
*intFrontPtr = temp;
}
return true;
}
In the else statement I’m simply reassigning to ensure that my ptrs are where I want them. For some reason I’m popping off the back instead of off the front.
Anyone care to explain?
I’m not entirely sure I understand what you’re trying to do, but if I;m guessing right you’re trying to ‘pop’ the 1st element of the array (
items[0]) into thenPopFrontint reference, then move all the subsequent elements of the array over by one so that the 1st element is replaced by the 2nd, the 2nd by the 3rd, and so on. After this operation, the array will contain one less total number of elements.Not having the full declaration of the
quackclass makes most of the following guesswork, but here goes:I’m assuming that
item[0]represents the ‘front’ of your array (so it’s the element you want ‘popped’).I’m also assuming that ‘count` is the number of valid elements (so item[count-1] is the last valid element, or the ‘back’ of the array).
Given these assumptions, I’m honestly not sure what
topis supposed to represent (so I might be entirely wrong on these guesses).Problem #1: your
nPopFrontassignment is reversed, it should be:Problem #2; your
forloop is a big no-op. It walks through the array assigning elements back to their original location. I think you want it to look more like:Finally, you’ll want to adjust
count(and probablytop– if you need it at all) before you return to adjust the new number of elements in the data structure. The whole thing might look like: