简体   繁体   English

使用数组 C++ 的堆栈实现

[英]Stack implementation using array C++

I am trying to implement some of the methods of the class "stack".我正在尝试实现 class “堆栈”的一些方法。 For the push() method I am trying to duplicate the capacity of the array if the top of the stack equals the capacity.对于 push() 方法,如果堆栈顶部等于容量,我试图复制数组的容量。 The top is the item of the next slot.顶部是下一个插槽的项目。 I was doing this by creating a new array with double the capacity of the original array and then copying the content over.我通过创建一个容量是原始数组两倍的新数组然后复制内容来做到这一点。 All the other methods I implemented (empty(), pop(), top()) seem to be working fine but the push function prints random values for the first 4 items of the stack for some reason if the stack has more than 10 elements (The capacity had to be increased).我实现的所有其他方法(empty()、pop()、top())似乎工作正常,但如果堆栈有超过 10 个元素,则推送 function 出于某种原因打印堆栈的前 4 个项目的随机值(必须增加容量)。 Why is this problem happening?为什么会出现这个问题?

#include <iostream>
using namespace std;

class stack
{
    public:
        stack();
        bool empty();
        void pop();
        void push(int x);
        int &topElem();
    
    private:
        int *buffer;
        int top;                          // Top element of stack
        int capacity = 10;                // Capacity of array

};

stack::stack()
{
    int *val = new int[capacity];
    buffer = val;
    top = 0;
}

bool stack::empty()
{
    if(top == 0)
        return true;
    else
        return false;
}

void stack::push(int x)
{
    if(top == capacity)
    {
        int *newArray = new int[capacity * 2];
        for(int i = 0; i < capacity; i++)
        {
            newArray[i] = buffer[i];
            //cout << "newArray[" << i << "]: " << newArray[i] << endl;
        }
        buffer = newArray;
        delete[] newArray;
        newArray = NULL;
    }
    buffer[top] = x;
    top++;
}

void stack::pop()
{
    if(!empty())
    {
        top--;
    }
    else
        cout << "Stack is empty!" << endl;
}

int& stack::topElem()
{
    return buffer[top - 1];
}

int main()
{
    stack plates;
    
    for (int i = 0; i < 20; i++)  // Add 20 elements to the stack
    {
        plates.push(i);
    }

    while (!plates.empty())
    {
        cout << plates.topElem() << endl;      // Prints the elemtents of the stack
        plates.pop();                          // Pops the last element of the stack
    }
    return 0;
}

// Output 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 393 -1250224288 393 -1250206816 // Output 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 393 -1250224288 393 -1250206816

buffer = newArray;
delete[] newArray;

this doesn't do what you expect.这不符合您的期望。 It points buffer to the new array, leaking the old one, then deleting the memory the buffer is pointing to.它将buffer指向新数组,泄漏旧数组,然后删除缓冲区指向的 memory。

You probably want something like:你可能想要这样的东西:

delete[] buffer; // free the old buffer
buffer = newArray; // point it to the newly allocated memory

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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