开发者

C++ Stack datastructure. What's wrong with this piece of code?

开发者 https://www.devze.com 2023-03-29 18:13 出处:网络
so I\'m currently trying to migrate my Java experience to C++ by implementing various Data Structures for the sake of having them implemented at least once.

so I'm currently trying to migrate my Java experience to C++ by implementing various Data Structures for the sake of having them implemented at least once.

Would you mind giving me some advise? The problem I am having is mainly concentrated around the pointers in push(int value) and especially pop(). As push seems to be working correctly I found myself struggling to get the correct value back when pop'ing things. What's the matter?

PS: I also think, that since I allocate my array space manually I'd need to delete it aswell. How do I do that?

#ifndef STACK_H
#define STACK_H

class Stack
{
private:
    int *stackArray;
    int elementsInArray;
    int allocatedArraySize;
    int alpha;
    int beta;

public:
    Stack();
    void push(int aValue);
    int pop();
    bool isEmpty();
    int size() const;
};

#endif

and the implementation:

#include <iostream>
#include "Stack.h"

Stack::Stack() 
{
    alpha = 4;
    beta = 2;
    elementsInArray = 0;
    allocatedArraySize = 1;
    stackArray = new int[1];
}

void Stack::push(int aValue)
{
    if (elementsInArray == allocatedArraySize) 
    {
        int temporaryArray[allocatedArraySize*beta];

        for (int i = 0; i < elementsInArray; i++)
            temporaryArray[i] = stackArray[i];

        stackArray = temporaryArray;
        allocatedArraySize *= beta;
    }

    elementsInArray++;
    stackArray[elementsInArray] = aValue;
}

int Stack::pop()
{
    int result = -INT_MAX;

    if (elementsInArray == 0)
        return result;

  开发者_如何学JAVA  if (elementsInArray > 0) 
    {
        result = stackArray[elementsInArray-1];
        elementsInArray--;

        if (elementsInArray <= allocatedArraySize/alpha) 
        {
            int temporaryArray[allocatedArraySize/alpha];

            for (int i = 0; i < elementsInArray; i++) 
                temporaryArray[i] = stackArray[i];

            stackArray = temporaryArray;
            allocatedArraySize /= beta;
        }
    }

    return result;
}

bool Stack::isEmpty()
{
    if (elementsInArray == 0) 
        return true;

    return false;
}

int Stack::size() const
{
    return allocatedArraySize;
}


For starters, you should be post incrementing the index on the array, so change:

elementsInArray++;
stackArray[elementsInArray] = aValue;

to:

stackArray[elementsInArray++] = aValue;

or:

stackArray[elementsInArray] = aValue;
elementsInArray++;

Second, when you create the new temp array you are doing it inside the if statement... therefore it is a local variable and placed on the system stack and lost after you exit the if statement. So change

int temporaryArray[allocatedArraySize*beta];

to:

int *temporaryArray = new int[allocatedArraySize*beta];

Third, add in the delete you were talking about by saving the original pointer from stackArray before copying the location of tempArray and then perform the delete after you've made the pointer copy.

Finally, you'll have to make similar changes to your pop function...


You are using an array on the stack (not your stack - the program execution stack). It's the one called temporaryArray in your push function. The address of that array will be invalid when you return from that function (because other functions will use the stack to hold other data).

what you want to do is allocate that array on the heap. This is memory that stays around for your program as long as you need it. To do this, you would allocate your temporaryArray like

int * temporaryArray(new int[allocatedArraySize*beta]);

Then, after copying the elements from your old array, you would delete it by using:

delete [] stackArray;

Do this before assigning the stackArray with the temporaryArray.

There may be other issues with your data structure, but you are doing the basic indexing correctly and incrementing / decrementing the current index appropriately (though I would suggest preferring to use the preincrement / decrement forms when not using the temporary as a good habit to get in - ie. ++elementsInArray / --elementsInArray).


well, i'm sure you already know that you have stack as a generic (c++ call it templates) in the STD library. Assuming you are doing this as a code kata, i would start writing it as a template, so it can't take object types others than integers.

Also, if you are going to write more of these low-level structures (as part of your kata), write a thin class where you delegate all allocation, reallocation and allocated size tracking, and use that instead of using arrays directly.

0

精彩评论

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