简体   繁体   中英

Understanding Stack Data Structure and Implementing it

Please have a look at the following code

template <typename T>

class Stack
{
public:
    Stack(int number)
    {
        maxSize = number;
        top = -1;
        stackData = new T*(maxSize);
    }

    ~Stack()
    {
        delete [] stackData;
    }

    int count()
    {

    }

    bool isEmpty()
    {
        if(top==-1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    bool isFull()
    {
        if(top== (maxSize-1))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    *T pop()
    {
        if(!isEmpty())
        {
            return stackData[top--]; // Remove Item From Stack
        }
    }

    *T peek();

    void push(T *pushValue)
    {
        if(!isFull())
        {
            stackData[++top] = pushValue;
        }
    }

private:
    int maxSize;
    T ** stackData;
    int top;
};

In the above code, the commented line says "Removing Item from the Stack". But actually, it is not removing, it is simply providing the value which is one value behind, right? In here, I refer removing as completely destroying that particular value from the stack.

ex: In an array which contains data 1,2,3,4 I remove '2'. So now it is 1,3,4

Second, what should happen inside the "peek()" method?

Third, are there are any errors that I didn't detect?

Please help!

They use an array for the stack. In fact top points to the top element in the stack. So when you do this:

return stackData[top--]; 

Top gets decreased and thus the size of the stack does decrease. You really pop an element.

peek should return the current element at the top of the stack - figure out how to do that for yourself.

Conceptually, there is no difference between decrementing top and "removing" the top item. The word "remove" is a conceptual abstraction to describe the idea that the top item in the stack is no longer an element in the stack. The fact that it is not literally removed from that location in memory is irrelevant.

If you mean you want to "destroy" the top item, ie invoke it's destructor and deallocate it , you need to consider the larger implications of how your Stack class manages memory. If the stack is supposed to take ownership of each T object, and each T object has been allocated using new , then you can have your pop() function delete the top item before decrementing top . (But then pop() can't return a pointer to the deleted element.) If the stack doesn't take ownership of each item, then it is up to the caller of pop() to manage the lifetime/deallocation of the element.

Next, the peek() method simply returns a pointer to the top item, without removing it from the stack.

And finally, you are not correctly allocating the array of T* pointers. The syntax should be:

stackData = new T*[maxSize];

The code you posted uses parentheses after new instead of brackets, which is not what you want here.

1) pop does pop an element, but it will remain in the array until something is pushed over it. But you have changed top so the stack doesn't "know" it's there anymore. If is a complex type, the destructor won't be called on pop .

2) peek typically shows the top of the stack without popping it.

3) you don't validate the number argument to the constructor.

Also, you could write isFUll() as simply { return (top== (maxSize-1));} .

Are you sure this is quoted correctly:

stackData = new T*(maxSize);

should it not be:

stackData = new T*[maxSize];

You are correct, the pop method doesn't "destroy" the data, it just moves the top of the stack to the next position (from the top element, to the second from top). I'm not sure why you would want to "destroy the particular value from the stack"... In general, that wouldn't serve any meaningful purpose.

Also, what you describe as "remove something in the middle" is not something you normally do with a stack - that's a list, deque or something like that.

As to writing a peek function, it would involve pretty much the same steps as pop , except you don't move the top ...

The idea is that every element at an index greater than top is invalid, while any element at an index smaller than top is valid. By decreasing top you are removing an element, by increasing top you are adding an element.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM