-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharraystack.cpp
More file actions
37 lines (28 loc) · 859 Bytes
/
arraystack.cpp
File metadata and controls
37 lines (28 loc) · 859 Bytes
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
#include "arraystack.h"
template <typename E>
ArrayStack<E>::ArrayStack(int cap)
: S(new E[cap]), capacity(cap), t(-1) { }
template <typename E>
ArrayStack<E>::~ArrayStack() {
delete [] S;
}
template <typename E>
int ArrayStack<E>::size() const
{ return (t + 1); }
template <typename E>
bool ArrayStack<E>::empty() const
{ return (t < 0); }
template <typename E> // return top of stack
const E& ArrayStack<E>::top() const throw(StackEmpty) {
if (empty()) throw StackEmpty("Topofemptystack");
return S[t]; }
template <typename E> // push element onto the stack
void ArrayStack<E>::push(const E& e) throw(StackFull) {
if (size() == capacity) throw StackFull("Pushtofullstack");
S[++t] = e;
}
template <typename E> // pop the stack
void ArrayStack<E>::pop() throw(StackEmpty) {
if (empty()) throw StackEmpty("Popfromemptystack");
--t;
}