-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack.h
128 lines (113 loc) · 3.05 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
123
124
125
126
127
128
/*
Author : Atanu Kumar Dey
University of Chittagong
Department of CSE (19th batch)
*/
//#ifndef STACK_H
//#define STACK_H
#include <Arduino.h>
/*#ifdef __cplusplus
extern "C++"{
#endif*/
template<typename _DATA_TYPE_,uint16_t _size, uint8_t id>
class Stack
{
private:
_DATA_TYPE_ arr[_size]; // array to store Stack elements
//_size // maximum capacity of the Stack
uint16_t top = 0; // front points to front element in the Stack (if any)
public:
Stack(); // constructor
~Stack(); // destructor
Stack(const Stack&) = delete;
Stack(Stack&&) = delete;
bool pop(_DATA_TYPE_ *var);
_DATA_TYPE_ pop();
bool push(_DATA_TYPE_ item);
_DATA_TYPE_ peek();
int16_t size();
bool clear();
bool isEmpty();
bool isFull();
};
// Constructor to initialize Stack
template<typename _DATA_TYPE_, uint16_t _size, uint8_t id>
Stack<_DATA_TYPE_, _size, id>::Stack() {}
// Destructor to free memory allocated to the Stack
template<typename _DATA_TYPE_, uint16_t _size, uint8_t id>
Stack<_DATA_TYPE_, _size, id>::~Stack(){}
// Utility function to remove front element from the Stack
template<typename _DATA_TYPE_, uint16_t _size, uint8_t id>
bool Stack<_DATA_TYPE_, _size, id>::pop(_DATA_TYPE_ *var)
{
if (isEmpty())
{
#ifdef _Stack_DEBUG
// Serial.println(F("UnderFlow\nProgram Terminated\n"));
#endif
return false;
}
*var = arr[--top];
return true;
}
template<typename _DATA_TYPE_, uint16_t _size, uint8_t id>
_DATA_TYPE_ Stack<_DATA_TYPE_, _size, id>::pop() {
if(top == 0)
return 0.00;
return arr[--top];
}
// Utility function to add an item to the Stack
template<typename _DATA_TYPE_, uint16_t _size, uint8_t id>
bool Stack<_DATA_TYPE_, _size, id>::push(_DATA_TYPE_ item)
{
// check for Stack overflow
if (isFull())
{
#ifdef _Stack_DEBUG
// Serial.println(F("OverFlow\nProgram Terminated\n"));
#endif
return false;
}
arr[top++] = item;
return true;
}
// Utility function to return front element in the Stack
template<typename _DATA_TYPE_, uint16_t _size, uint8_t id>
_DATA_TYPE_ Stack<_DATA_TYPE_, _size, id>::peek()
{
if (isEmpty())
{
#ifdef _Stack_DEBUG
//Serial.println(F("UnderFlow\nProgram Terminated\n"));
#endif
}
return arr[top - 1];
}
// Utility function to return the size of the Stack
template<typename _DATA_TYPE_, uint16_t _size, uint8_t id>
int16_t Stack<_DATA_TYPE_, _size, id>::size()
{
return top;
}
// Utility function to check if the Stack is empty or not
template<typename _DATA_TYPE_, uint16_t _size, uint8_t id>
bool Stack<_DATA_TYPE_, _size, id>::isEmpty()
{
return (top == 0);
}
// Utility function to check if the Stack is full or not
template<typename _DATA_TYPE_, uint16_t _size, uint8_t id>
bool Stack<_DATA_TYPE_, _size, id>::isFull()
{
/*Serial.print(top);
Serial.print(" ");
Serial.print(_size);
Serial.print(" id - ");
Serial.println(id);*/
return (top == _size);
}
/*#ifdef __cplusplus
}
#endif*/
//#endif