-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
stack.h
122 lines (105 loc) · 2.6 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
118
119
120
121
122
/*******************************************************************************
* DANIEL'S ALGORITHM IMPLEMENTAIONS
*
* /\ | _ _ ._ o _|_ |_ ._ _ _
* /--\ | (_| (_) | | |_ | | | | | _>
* _|
*
* STACK
*
* Features:
* 1. Stack with capcity
*
* http://en.wikipedia.org/wiki/Stack_(abstract_data_type)
*
******************************************************************************/
#ifndef ALGO_STACK_H__
#define ALGO_STACK_H__
#include <stdint.h>
#include <stdbool.h>
#include <exception>
namespace alg {
/**
* Stack has three properties. capacity stands for the maximum number of
* elements stack can hold. Size stands for the current size of the stack and elements
* is the array of elements
*/
template<typename T=uintptr_t>
class Stack {
private:
class StackEmptyException: public std::exception {
public:
virtual const char * what() const throw()
{
return "stack is empty";
}
} excp_empty;
class StackIndexOutOfBoundException: public std::exception {
public:
virtual const char * what() const throw()
{
return "Index out of bound.";
}
} excp_ioob;
uint32_t m_capacity; // the total capacity
uint32_t m_size; // current stack size
T * m_elements; // the elements
public:
/**
* capcity is the maximum elements the stack can hold.
*/
Stack(uint32_t capacity) {
this->m_capacity = capacity;
this->m_size = 0;
this->m_elements = new T[capacity];
}
~Stack() {
delete [] m_elements;
}
private:
Stack(const Stack&);
Stack& operator=(const Stack&);
public:
/**
* test whether the stack is empty
*/
inline bool is_empty() const { return m_size==0?true:false; }
/**
* pop stack
*/
inline void pop() {
if(m_size!=0) m_size--;
return;
}
/**
* get the top element
*/
inline const T& top() const {
if (m_size==0) throw excp_empty;
return m_elements[m_size-1];
}
/**
* push an element into the stack
* returns false when stack is full.
*/
inline bool push(const T & value) {
if(m_size==m_capacity) { return false; }
else {
m_elements[m_size++] = value;
return true;
}
}
/**
* return the stack size count.
*/
inline uint32_t count() const { return m_size; }
/**
* return value by index
*/
inline const T& operator[] (uint32_t idx) const {
if (idx >= m_capacity) throw excp_ioob;
return m_elements[m_size-1-idx];
}
};
}
#endif //