开发者

Array PopFront Method C++

开发者 https://www.devze.com 2022-12-10 11:31 出处:网络
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...:

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 the nPopFront int 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 quack class 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 top is supposed to represent (so I might be entirely wrong on these guesses).

Problem #1: your nPopFront assignment is reversed, it should be:

nPopFront = items[0].n;

Problem #2; your for loop 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:

for (int i = 1; i < count; ++i)
{
    items[i-1].n = items[i].n;  // move elements from back to front
}

Finally, you'll want to adjust count (and probably top - 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:

bool quack::popFront(int& nPopFront)
{   
    if ( count >= maxSize ) return false;
    if ( count == 0 ) return false; // nothing to pop

    nPopFront = items[0].n;
    intFrontPtr = &items[0].n;      // do we really need to maintain these pointers?
    intBackPtr  = &items[count-1].n;

    for (int i = 1; i < count; ++i)
    {
        items[i-1].n = items[i].n;  // move elements from back to front
    }

    count -= 1;     // one less item in the array
    return true;
}


The original question seems to be that you don't understand why the function popFront returns 3 times when there are 3 elements?

If that's the case, I think you are missing the point of recursion.

When you make a recursive call, you are calling the same function again, basically creating a new stack frame and jumping back to the same function. So if there are 3 elements, it will recurse by encountering the first element, encountering the second element, encountering the third element, returning from the third encounter, returning from the second encounter, and returning from the first encounter (assuming you are properly consuming your array, which you don't appear to be).

The current function cannot return until the recursive call has iterated, thus it may appear to return from the last element before the second, and the second before the first.

That is how recursion works.

I wasn't able to make sense of your example, so I whipped one up real fast:

#include <iostream>

using namespace std;

bool popfront(int* ptr_, int* back_) {
    cerr << ptr_[0] << endl;
    if(ptr_ != back_) {
        popfront(++ptr_, back_);
    }
    return true;
}

int main() {
   int ar[4] = {4,3,2,1};
   popfront(ar, ar + 3);
   return 0;
}

That's not great, but it should get the point across.


Can't you just use a std::list?

That makes it really to pop from either end using pop_front or pop_back. You can also add to the front and the back. It also has the advantage that after popping from the front (or even removing from the middle of the list) you don't have to shift anything around (The link is simply removed) which makes it much more efficient than what you are, seemingly, proposing.


I'm assuming you're trying to assign the popped value to nPopFront?

bool stack::popFront(int& nPopFront) 
{    
    //items[4] = {4,3,2,1} 
    if ( intFrontPtr < intBackPtr ) 
    {    
            nPopFront = *intFrontPtr; 
            ++intFrontPtr; 
    } 
    return true; 
} 


bool quack::popFront(int& nPopFront)
{
   if(items.n==0) throw WhateverYouUseToSignalError;
   nPopFront = items[0];
   for (int =0;i<items.n-1,++i){
      items[i]=items[i+1]
   }
   //update size of items array
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号