240173099
2016-04-30 21:22
#include <iostream> #include "MyStack.h" using namespace std; /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int main(int argc, char** argv) { MyStack *pStack=new MyStack(5); if(pStack->stackEmpty()) { cout<<"kong"<<endl; } else { cout<<"mei kong"<<endl; } if(pStack->stackFull()) { cout<<"man"<<endl; } else { cout<<"mei man"<<endl; } delete []pStack; pStack=NULL; return 0; } class MyStack { public: MyStack(int size); ~MyStack(); bool stackEmpty();//ÅÐ¿Õ bool stackFull();//ÅÐÂú void clearStack();//Çå¿Õ int stackLength();//ÔªËظöÊý bool push(char elem);//ÈëÕ» bool pop(char &elem);//³öÕ» void stackTraverse(bool isFromButtom);//±éÀú private: char *m_pBuffer; int m_iSize; int m_iTop; }; #include "MyStack.h" #include <iostream> using namespace std; MyStack::MyStack(int size) { m_iSize=size; m_pBuffer=new char[m_iSize]; m_iTop=0; } MyStack::~MyStack() { delete []m_pBuffer; m_pBuffer=NULL; } bool MyStack::stackEmpty() { if(0==m_iTop) { return true; } else { return false; } } bool MyStack::stackFull() { if(m_iSize==m_iTop) { return true; } else { return false; } } void MyStack::clearStack() { m_iTop=0; } int MyStack::stackLength() { return m_iTop; } bool MyStack::push(char elem) { if(!stackFull()) { m_pBuffer[m_iTop]=elem; m_iTop++; return true; } else { return false; } } bool MyStack::pop(char &elem) { if(!stackEmpty()) { m_iTop--; elem=m_pBuffer[m_iTop]; return true; } else { return false; } } void MyStack::stackTraverse(bool isFromButtom) { if(isFromButtom) { for(int i=0;i<m_iTop;i++) { cout<<m_pBuffer[i]<<endl; } } else { for(int i=m_iTop-1;i>=0;i--) { cout<<m_pBuffer[i]<<endl; } } }
最后应该写成delete pStack;,
我看你写成delete []pStack;
看来你和我一样,一开始以为这个是五个长度的数组了所以加了[]
试试吧,我改过这个就对了
这个问题我不知道,可是我能问一个小白问题嘛,你们平常用什么软件写代码与运行啊?。。。
数据结构探险—栈篇
62733 学习 · 102 问题
相似问题