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

时间:2015-08-17 09:09:04

标签: c++ stack

我正在阅读“使用C ++进行数据结构”,第2版,D.S Malik,并在书中将堆栈(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    

1 个答案:

答案 0 :(得分:2)

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
}