簡體   English   中英

C ++動態,基於數組的堆棧

[英]C++ dynamic, array based stack

我剛剛開始學習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;
}

它工作正常,直到陣列已滿並且我調整它的大小。 之后,它開始打印這個而不是字母 打印 當程序完成並且我必須按任意鍵退出它時會出現以下錯誤 錯誤

在此先感謝您的幫助。

您應該在resize()中將top_設置為new storage top。

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;
    }

錯誤確實在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來更新指針,而只需要簡單的賦值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM