C ++动态,基于数组的堆栈

时间:2015-02-15 12:25:35

标签: c++ arrays

我刚刚开始学习C ++编程和练习,我找到了这个任务。我必须编写一个基于数组的动态堆栈。这是我到目前为止所得到的。

#include <iostream>
using namespace std;
class CStack
{
private:
    char *bottom_;
    char *top_;
    int size_;
public:
    CStack(int n = 20){
        bottom_ = new char[n];
        top_ = bottom_;
        size_ = n;
    }
    int getSize(){ return size_; }
    void push(char c){
        if (!full()){
            *top_ = c;
            top_++;
        }
        else{
            resize(size_ * 2);
            *top_ = c;
            top_++;
        }
    }
    void resize(int newSize){

            //Allocate new array and copy in data
            char *newArray = new char[newSize];
            memcpy(newArray, bottom_, size_);

            //Delete old array
            delete[] bottom_;
            //Swap pointers and new size
            memcpy(bottom_, newArray, newSize);
            size_ = newSize;
            cout << "array has been resized" << endl;
    }

    int num_items() {
        return (top_ - bottom_);
    }
    char pop(){
        top_--;
        return *top_;
    }
    int full() {
        return (num_items() >= size_);
    }
    int empty() {
        return (num_items() <= 0);
    }
    void print(){
        cout << "Stack currently holds " << num_items() << " items: ";
        for (char *element = bottom_; element<top_; element++) {
            cout << " " << *element;
        }
        cout << "\n";
    }
    ~CStack(){ // stacks when exiting functions
        delete[] bottom_;
    }
};
int main(){
    CStack s(5);
    s.print(); cout << "\n";
    s.push('s'); s.push('t'); s.push('a'); s.push('c'); s.push('k');
    s.print(); cout << "\n";
    s.push('='); 
    s.print(); cout << "\n";
    cout << "Popped value is: " << s.pop() << "\n";
    s.print(); cout << "\n";
    s.push('!');
    s.print(); cout << "\n";
    s.pop();
    s.pop();
    s.print(); cout << "\n";
    while (!s.empty()) s.pop();
    if (s.num_items() != 0) {
        cout << "Error: Stack is corrupt!\n";
    }
    s.print(); cout << "\n";
    // destructor for s automatically called
    system("pause"); // execute M$-DOS' pause command
    return 0;
}

它工作正常,直到数组已满并且我调整它的大小。之后,它开始打印此printout而不是字母。当程序完成并且我必须按任意键退出它时会出现以下错误error

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

您应该在resize()中将top_设置为new storage。

void resize(int newSize){

            //Allocate new array and copy in data
            char *newArray = new char[newSize];
            memcpy(newArray, bottom_, size_);

            top_ = newArray + (top_ - bottom_);

            //Delete old array
            delete[] bottom_;

            size_ = newSize;
            bottom_ = newArray;

            cout << "array has been resized" << endl;
    }

答案 1 :(得分:0)

错误确实在resize方法中:

void resize(int newSize){
    //Allocate new array and copy in data
    char *newArray = new char[newSize];
    memcpy(newArray, bottom_, size_);

    // Set the top to the new array
    top_ = newArray + (top_ - bottom_);

    // Delete old array
    delete[] bottom_;

    // Update pointers and size
    bottom_ = newArray;
    size_ = newsize;

    cout << "array has been resized" << endl;
}

您的代码忘记修复top_指针,并在需要简单分配时使用memcpy更新指针。

相关问题