为什么我的程序总是在stackSize上出错啊
编辑的马的遍历问题,用到栈,写了课本上的一个栈,里面有stackSize的应用,但是程序总是运行错误,编译无错,debug之后发现是stackSize出了错误,求各位神仙指教
//自定义栈,链表描述
#include <iostream>
#include <stdlib.h>
using namespace std;
//定义节点
template <class T>
struct chainNode
{
// data members
T element;
chainNode<T> *next;
// methods
chainNode() {}
chainNode(const T& element)
{this->element = element;}
chainNode(const T& element, chainNode<T>* next)
{this->element = element;
this->next = next;}
};
/*******************************************************************************/
template<class T>
class linkedStack
{
public:
linkedStack(int iC = 90)
{ stackTop = NULL; stackSize = 0;}
bool empty() const
{ return stackSize == 0;}
T& top()
{
if(stackSize == 0)
cout<<"sorry,NULL!";
else
return stackTop->element;
}
void pop();
void push(const T& theE)
{
stackTop = new chainNode<T>(theE, stackTop);
stackSize++;
}
int size() const
{
cout<<"debug2"<<stackSize<<endl;
return stackSize;
}
private:
chainNode<T>* stackTop;
int stackSize;
};
template <class T>
void linkedStack<T>::pop()
{
if(stackSize == 0)
cout<<"wrong!";
else{
chainNode<T>* nextNode = stackTop->next;
delete stackTop;
stackTop = nextNode;
stackSize--;
}
}