-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqStack.cpp
More file actions
85 lines (73 loc) · 1.66 KB
/
SqStack.cpp
File metadata and controls
85 lines (73 loc) · 1.66 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "SqStack.h"
template <class SElemType>
SqStack<SElemType>::SqStack()
{
base = (SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType));
if (!base) exit(OVERFLOW);
top = base;
stacksize = STACK_INIT_SIZE;
}
template <class SElemType>
SqStack<SElemType>::~SqStack()
{
free(base);
}
template <class SElemType>
Status SqStack<SElemType>::Clear()
{
if (!base) exit(BASE_IS_NULL);
free(base);
base = (SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType));
if (!base) exit(OVERFLOW);
top = base;
stacksize = STACK_INIT_SIZE;
return OK;
}//ClearStack
template <class SElemType>
bool SqStack<SElemType>::IsEmpty()
{
if (!base) exit(BASE_IS_NULL);
return base==top ? true : false;
}//StackEmpty
template <class SElemType>
int SqStack<SElemType>::GetLength()
{
if (!base) exit(BASE_IS_NULL);
return top-base;
}//StackLength
template <class SElemType>
SElemType SqStack<SElemType>::GetTop()
{
if (top == base) exit(STACK_IS_EMPTY);
return *(top - 1);
}//GetTop
template <class SElemType>
Status SqStack<SElemType>::Push(SElemType e)
{
if (top - base >= stacksize)
{
base = (SElemType *) realloc(base, (stacksize + STACKINCREMENT) * sizeof(SElemType));
if (!base) exit(OVERFLOW);
top = base + stacksize;
stacksize += STACKINCREMENT;
}
*top++ = e;
return OK;
}//Push
template <class SElemType>
Status SqStack<SElemType>::Pop(SElemType *e)
{
if (top == base) return STACK_IS_NULL;
*e = *--top;
return OK;
}//Pop
template <class SElemType>
Status SqStack<SElemType>::StackTraverse(Status visit(SElemType))
{
if (!base) exit(BASE_IS_NULL);
SElemType *p = base;
for (;p<top; p++)
{
if (visit(*p) != OK) return VISIT_FAILED;
}
}//StackTraverse