繁体   English   中英

C ++ Stack实现为数组,程序崩溃

[英]C++ Stack implemented as an array, program crash

我正在阅读《 DS Malik使用C ++,第二版数据结构》一书,在这本书中,堆栈(ADT)被实现为数组(第400页)。 我完全遵循了代码,但是当我尝试调用.push()时,程序崩溃了。 所有其他成员函数都将正确执行。 知道为什么它崩溃了吗? 代码如下:

抽象类:

#include <iostream>

template <class T>
class stackADT
{
public:
    virtual void initialiseStack() = 0;
    virtual bool isEmpty() const = 0;
    virtual bool isFull() const = 0;
    virtual void push(const T& newItem) = 0;
    virtual T top() const = 0;
    virtual void pop() = 0;
}; 

StackType类定义:(在书中的定义和实现放在一个.h文件中)

#ifndef STACKTYPE_H
#define STACKTYPE_H

#include <iostream>
#include <cassert>

#include "StackADT.h"

using namespace std;

template <class T>
class stackType: public stackADT<T>
{
public:
    const stackType<T>& operator=(const stackType&); //
    void initialiseStack(); // initialises stack to size of 100
    bool isEmpty() const;
    bool isFull() const;
    void push(const T& newItem);
    T top() const;
    void pop();
    stackType(int stackSize = 100);
    stackType(const stackType<T>& otherStack);
    ~stackType();
    void update(T t);
    int getStackTop() const;
private:
    int maxStackSize; //holds max num of elements in stack
    int stackTop;    //holds num of elements in stack
    T *list;        //pointer to the array holding stack elements
    void copyStack(const stackType<T>& otherStack);
};

StackType实现:

template <class T>
void stackType<T>::initialiseStack()
{
   stackTop = 100;
}

template <class T>
bool stackType<T>::isEmpty() const
{
    return(stackTop == 0);
}

template <class T>
bool stackType<T>::isFull() const
{
    return(stackTop == maxStackSize);
}

template <class T>
void stackType<T>::push(const T& item)
{
    if(!isFull())
    {
       list[stackTop] = item;
       stackTop++;
    }
    else
        cout << "Stack is full." << endl;
}

template <class T>
T stackType<T>::top() const
{
    assert(stackTop != 0);

    return (list[stackTop - 1]);
}

template <class T>
void stackType<T>::pop()
{
    stackTop--;
}

template <class T>
void stackType<T>::copyStack(const stackType<T>& otherStack)
{
    delete [] list;
    maxStackSize = otherStack.maxStackSize;
    stackTop = otherStack.stackTop;

    list = new T[maxStackSize];

    for(int i = 0; i < stackTop; i++)
    {
        list[i] = otherStack.list[i];
    }
}

template <class T>
stackType<T>::stackType(int stackSize)
{
    if(stackSize <= 0)
    {
        cout << "Size must be positive. Creating array of size 100" << endl;
        maxStackSize = 100;
    }
    else
    {
        maxStackSize = stackSize;
    }
}

template <class T>
stackType<T>::~stackType()
{
    delete [] list;
}

template <class T>
stackType<T>::stackType(const stackType<T>& otherStack)
{
    list = NULL;

    copyStack(otherStack);
}


template <class T>
void stackType<T>::update(T t)
{
    stackType tempStack(5);
    int i = 0;
    T itemFound;

    while(!isEmpty())
    {
        if(top() != t)
        {
            tempStack.list[i] = top();
            pop();
        }
        else
        {
            itemFound = stackTop - 1;
            pop();
        }
        i++;
    }

    i = 0;

    while( i < 4)
    {
        list[i] = tempStack.top();
        tempStack.pop();
        i++;
    }

    this->push(itemFound);

}

template <class T>
int stackType<T>::getStackTop() const
{
    return stackTop;
}

template <class T>
const stackType<T>& stackType<T>::operator=(const stackType<T>& otherStack)
{
    if(this != &otherStack)
        copyStack(otherStack);

    return *this;
}

#endif // STACKTYPE_H    

list似乎没有初始化-导致NULL指针。 构造函数应类似于:

template <class T>
stackType<T>::stackType(int stackSize)
{
    if(stackSize <= 0)
    {
        cout << "Size must be positive. Creating array of size 100" << endl;
        maxStackSize = 100;
    }
    else
    {
        maxStackSize = stackSize;
    }

    list = new T[maxStackSize];  // <<<< initialize list
}

暂无
暂无

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

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