-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathstack.h
117 lines (89 loc) · 2.79 KB
/
stack.h
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//*****************************************************************************************************
//
// This header file defines a class template for an array-based list using a stack structure.
// Last-in, first-out (LIFO)
//
//*****************************************************************************************************
#ifndef STACK_H
#define STACK_H
//*****************************************************************************************************
template <typename T>
class Stack {
private:
T *stack;
int top;
int capacity;
public:
Stack(int cap = 100);
~Stack();
bool push(const T &dataIn);
bool pop(T &dataOut);
bool peek(T &dataOut) const;
int getNumValues() const;
bool isFull() const;
bool isEmpty() const;
};
//*****************************************************************************************************
template <typename T>
Stack<T>::Stack(int cap) {
capacity = cap;
top = -1;
stack = new T[capacity];
}
//*****************************************************************************************************
template <typename T>
Stack<T>::~Stack() {
delete[] stack;
stack = nullptr;
capacity = 0;
top = -1;
}
//*****************************************************************************************************
template <typename T>
bool Stack<T>::push(const T &dataIn) {
bool success = false;
if ((top + 1) < capacity) {
top++;
stack[top] = dataIn;
success = true;
}
return success;
}
//*****************************************************************************************************
template <typename T>
bool Stack<T>::pop(T &dataOut) {
bool success = false;
if (top >= 0) {
dataOut = stack[top];
top--;
success = true;
}
return success;
}
//*****************************************************************************************************
template <typename T>
bool Stack<T>::peek(T &dataOut) const {
bool success = false;
if (top >= 0) {
dataOut = stack[top];
success = true;
}
return success;
}
//*****************************************************************************************************
template <typename T>
int Stack<T>::getNumValues() const {
return (top + 1);
}
//*****************************************************************************************************
template <typename T>
bool Stack<T>::isFull() const {
return ((top + 1) == capacity);
}
//*****************************************************************************************************
template <typename T>
bool Stack<T>::isEmpty() const {
return (top < 0);
}
//*****************************************************************************************************
#endif