开发者

PopFront Delimma C++

开发者 https://www.devze.com 2022-12-10 02:46 出处:网络
Strange programming problems as of now..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...:

Strange programming problems as of now..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 &popFront )
{   
    //items[count-1].n = { 9,4,3,2,1,0 };
    nPopFront = items[0].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;
    }
    --count;
    return true;
}

Its just my implementation of a cross between a queue and a stack..PopFront is a public method of the class object quack..The items is a private struct type 'item', it is within the quack.h. It has one member, 'int n'..But, that is irrelevent.

the comment in the code is the contents of my integer array, 'items'.

I am trying to Pop elements off the front of my array. WHat im thinking is that after i get the first item, i'll just incrememnt the frontPtr and transfer the item i got previously to the frontPtr i incremented!...

I cannot, for any reason, us开发者_如何学JAVAe a + or - shift by 1 or the use of stls, boosts, std's and the like..

Can someone help me with my homework assignment?


My suggestion are :

1). Put statement --count where it keeps object's state valid on exceptional condition.

2). clear your concepts of pointers which will help you a lot.


Generally, I would not recommend using an array if you want to pop items off the front. You would be much better off using a linked list, or some other structure which will allow you to remove items in O(1) time. It will be much easier to remove (pop) items this way.

As for your current code, I really can't comment without a better idea of what your class looks like. Please post the class definition at least so we can tell what all your variables are referring to, and what you're code is actually doing.


There are quite a few problems. But I believe your major one is this loop:

for (int temp; intFrontPtr < intBackPtr ;)
{
     intFrontPtr++;
     temp = *intFrontPtr;
     *intFrontPtr = temp;
}

It looks to me like you are trying to shift all your items down. Since this is homework, I'm not going to give you the answer but I'll give you some hints to help debug this.

What you want to do is examine your items array at the beginning and end of the loop.

for (int temp; intFrontPtr < intBackPtr ;)
{
     // whats does array look like here
     intFrontPtr++;
     temp = *intFrontPtr;
     *intFrontPtr = temp;
     // and what does array look like here
}

If you don't have experience with a debugger, add a function call that will dump your array.

0

精彩评论

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