基于数组的堆栈实现

Stack implementation based on arrays

本文关键字:堆栈 实现 数组 于数组      更新时间:2023-10-16

这是我的代码,但是有一个我无法理解的问题:每次当我在堆栈中push一个整数时,堆栈的其余部分(未填充的部分)由0值填充。请解释一下我的错误。

#include <iostream>
using namespace std;
class Stack
{
private:
    int* base;
    int max_size;
    int top;
public:
    Stack(int size = 100)
    {
        base = new int[size];
        max_size = size;
        top = -1;
    }
    ~Stack()
    {
        delete[] base;
    }
    bool empty() const
    {
        return top == -1;
    }
    bool full() const
    {
        return top == max_size - 1;
    }
    void push(int element)
    { 
        if (top == max_size-1) {
            cout<< "Stack overflow" << endl;
            return;
        }
        base[++top] = element; 
    }
    void pop()
    { 
        if(top == -1) {
            cout << "Stack underflow" << endl;
            return;
        } 
        top--; 
    }
    int & read_top() const
    {
        return base[top];
    }
    void print()
    {
        int i = 0 ;
        cout << "Stack is ";
        while(i <= max_size)
        {
            cout << base[i] <<" ";
            i++;
        }
        cout << endl;
    }
};
int main()
{
    Stack first(10);
    for(int i = 0; i<=5; i++)
    {
        first.push(i*i);
        first.print();
    }
    return 0;
}

考虑:

Stack s(10);
for(int i = 0; i<=5; i++)
    s.push(i);

print中你有:

std::cout << "Stack is ";
while(i <= max_size)
    std::cout << base[i] << ' ';

在上面的例子中,s.max_size是10,所以你要打印base[0,10)的值,其中5个是未初始化的。因为应用程序还没有对内存做太多的处理,所以当你执行分配时,你很可能会得到一个原始的(零)内存块。

你的代码可能的变化:

#include <iostream>
// using namespace std ;  // unlearn the habbit of doing this.
#include <memory>  // for unique_ptr
class Stack
{
    // note: class is private by default.
        // unique_ptr will automatically free memory for us.
        using  ptr_t = std::unique_ptr<int[]>;
        ptr_t  base_;
        int*   top_;
        int*   end_;
    public :
        Stack(int size=100)
          : base_(new int[size])
          , top_(base_.get())
          , end_(top_ + size)
        {}
        bool empty() const
        { return top_ == base_.get(); }
        bool full() const
        { return top_ == end_; }
        // don't print errors from a library
        bool push(int value)
        { 
            if (full())  // written in terms of our own members
                return false;
            *(top_++) = value;
            return true; 
        }
        bool pop()
        { 
            if (empty())  // written in terms of our own members
                return false;
            --top_;
            return true;
        }
        const int& read_top() const  // const ref so it's not modifiable
        { return *(top_-1); }
        int size() const { return end_ - top_; }
        int capacity() const { return end_ - base_.get(); }
        std::ostream& print(std::ostream& to)
        {
            to << "Stack is ";
            for (int* ptr = base_.get(); ptr != top_; ++ptr)
                to << *ptr << ' ';
            to << 'n';
            return to;
        }
        // or we could do this:
        friend std::ostream& operator << (std::ostream& to, const Stack& s)
        {
            to << '[';
            for (int* ptr = s.base_.get(); ptr != s.top_; ++ptr) {
                to << *ptr << ',';
            }
            return to << ']';
        }
};
int main()
{
    Stack s(5);
    s.push(1);
    s.push(2);
    s.push(3);
    s.print(std::cout);
    std::cout << "stack is: " << s << 'n';
}

实时演示:http://ideone.com/JsA4EU

我不太确定我得到你的问题是正确的:你的意思是"每次当我推一个整数在我的堆栈"?据我所知,您初始化堆栈一次,然后进行一些推入,而不改变中间的底层数组。这里发生的事情是,你的数组被初始化为0(不要指望你的编译器每次都这样做,但这次它会这样做-很可能你在编译时没有优化),然后你添加单个元素,正如有人已经回答的那样,你不只是打印堆栈(即从0到顶),而是整个底层数组。如果编译优化,您可能会得到垃圾值而不是零(数组没有在构造函数中初始化为一些初始值,并且未赋值的可能包含任何值)。希望能有所帮助。

代码中的一个bug:在print()方法中,您应该有while(i < max_size)