# include "Mystack. H"
Int main () {
Mystack Mystack (3);
Mystack. Push (' a ');
Mystack. Push (" b ");
Mystack. Push (' a ');
Mystack. StackTraverse ();
return 0;
}
Mystack. H file: # pragma once
The class Mystack {
Public:
Mystack (int size);//allocate memory initialization stack space, setting capacity, stack top
~ Mystack ();//recycle stack space memory
Bool stackEmpty ();//determine whether the stack is empty, empty - & gt; True, not empty - & gt; False
Bool stackFull ();//whether the stack is full, full - & gt; True, the full - & gt; False
Void clearStack ();//to empty stack
Int stackLength ();//element existing number
Void push (char elem);//element into the stack, stack up
Pop (char is void & amp; Elem);//element out of the stack, stack down
Void stackTraverse ();//traversal stack element
Private:
Char * m_pBuffer;//stack space pointer
Int m_iSize;//stack capacity
Int m_iTop;
//stack};
Mystack. CPP file:
# include "Mystack. H"
#include
using namespace std;
Mystack: : Mystack (int size) {
M_iSize=size;
M_iTop=0;
M_pBuffer=new char (size);//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
}
Mystack: : ~ Mystack () {
Cout & lt; <"~ Mystack ()" & lt;
M_pBuffer=NULL;
}
Bool Mystack: : stackEmpty () {
If (0==m_iTop) {
return true;
Cout & lt; <"Wrong! Space have nothing to clean up!"
return false;
}
Bool Mystack: : stackFull () {
If (m_iSize==m_iTop) {
return true;
Cout & lt; <"The space is full, please clean up the memory!"
return false;
}
Void Mystack: : clearStack () {
M_iTop=0;
}
Int Mystack: : stackLength () {
Return m_iTop;
}
Void Mystack: : push (char elem) {
if (! StackFull ()) {
M_pBuffer [m_iTop]=elem;
M_iTop + +;
}
}
Void Mystack: : pop (char& Elem) {
if (! StackEmpty ()) {
M_iTop -;
Elem=m_pBuffer [m_iTop];
}
}
Void Mystack: : stackTraverse () {
for (int i=0; i
Cout & lt;
}
CodePudding user response:
New char (size) should be changed to new char [size]