-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathCStack.cpp
58 lines (52 loc) · 1.04 KB
/
CStack.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include "CStack.h"
CStack::CStack()
{
//ctor
//you can't use this constructor
//use CStack(int nStackSize) instead
assert(m_p == -1 && m_sizeLimit > 0);
}
CStack::CStack(const int nStackSize)
{
m_p = -1;
m_sizeLimit = nStackSize;
array = NULL;
array = new Type[nStackSize];
assert( array != NULL );
}
CStack::~CStack()
{
//dtor
assert(array == NULL);/*you need to call func free to free the memory*/
}
void CStack::clear()
{
m_p = -1;
}
void CStack::free()
{
if(array != NULL) delete [] array; array = NULL; clear();
}
Type CStack::top(){
if( m_p > -1 && m_p < m_sizeLimit ){return array[m_p];}
assert(m_p > -1 && m_p < m_sizeLimit);/*test whether it's empty or not*/
return 0;
}
void CStack::push(Type v)
{
if(m_p + 1 < m_sizeLimit){ array[++m_p] = v;}
assert(m_p+1 < m_sizeLimit);/*memory exceed limit*/
}
bool CStack::empty()
{
if(m_p < 0)return true;
else return false;
}
void CStack::pop()
{
m_p--;
}
int CStack::size()
{
return m_p + 1;
}