Last active
August 29, 2015 14:05
-
-
Save calebreister/f8a94ddab4004efe826a to your computer and use it in GitHub Desktop.
These files contain simple array-based stack and queue templates. I designed them to be Arduino-compatible. They do not use any dynamic memory (which can cause various issues on Arduino boards due to their low specs) and I have kept features to a minimum. Note that they can have multiple sizes via template. This is not necessarily efficient on m…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef QUEUE_H | |
#define QUEUE_H | |
template <class type, unsigned int SIZE> | |
class Queue { | |
private: | |
type data[SIZE]; | |
int b; | |
int f; | |
void inc(int& x); | |
public: | |
Queue(); | |
bool push(const type& value); | |
type* front(); | |
bool pop(); | |
bool empty(); | |
bool full(); | |
unsigned int count(); | |
unsigned int size(); | |
}; | |
////////////////////////////////////////////////////// | |
template <class type, unsigned int SIZE> | |
void Queue<type, SIZE>::inc(int& x) { //increment | |
x++; | |
if (x >= SIZE) | |
x = 0; | |
//equivalent: (x+1) % SIZE | |
} | |
////////////////////////////////////////////////////// | |
template <class type, unsigned int SIZE> | |
Queue<type, SIZE>::Queue() { | |
b = 0; | |
f = 0; | |
} | |
template <class type, unsigned int SIZE> | |
bool Queue<type, SIZE>::push(const type& value) { | |
if (full()) | |
return false; | |
data[b] = value; | |
inc(b); | |
return true; | |
} | |
template <class type, unsigned int SIZE> | |
type* Queue<type, SIZE>::front() { | |
return empty() ? NULL : &data[f]; | |
} | |
template <class type, unsigned int SIZE> | |
bool Queue<type, SIZE>::pop() { | |
if (empty()) | |
return false; | |
inc(f); | |
return true; | |
} | |
template <class type, unsigned int SIZE> | |
bool Queue<type, SIZE>::empty() { | |
return (b == f) ? true : false; | |
} | |
template <class type, unsigned int SIZE> | |
bool Queue<type, SIZE>::full() { | |
int b2 = b; | |
inc(b2); | |
return (b2 == f) ? true : false; | |
} | |
template <class type, unsigned int SIZE> | |
unsigned int Queue<type, SIZE>::count() { | |
if (full()) | |
return SIZE - 1; | |
else if (f > b) | |
return f - b; | |
else | |
return b - f; | |
} | |
template <class type, unsigned int SIZE> | |
unsigned int Queue<type, SIZE>::size() { | |
return SIZE - 1; | |
} | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef STACK_H | |
#define STACK_H | |
template <class type, int SIZE> | |
class Stack { | |
private: | |
type data[SIZE]; | |
int t; | |
public: | |
Stack(); | |
bool push(const type& value); | |
type* top(); | |
bool pop(); | |
bool empty(); | |
bool full(); | |
int count(); | |
int size(); | |
}; | |
/////////////////////////////////////////////////////////// | |
template <class type, int SIZE> | |
Stack<type, SIZE>::Stack() { | |
t = -1; | |
} | |
template <class type, int SIZE> | |
bool Stack<type, SIZE>::push(const type& value) { | |
if (!full()) | |
{ | |
data[++t] = value; | |
return true; | |
} | |
else | |
return false; | |
} | |
template <class type, int SIZE> | |
type* Stack<type, SIZE>::top() { | |
return (t == -1) ? NULL : &data[t]; | |
} | |
template <class type, int SIZE> | |
bool Stack<type, SIZE>::pop() { | |
if (!empty()) | |
{ | |
t--; | |
return true; | |
} | |
else | |
return false; | |
} | |
template <class type, int SIZE> | |
bool Stack<type, SIZE>::empty() { | |
if (t == -1) | |
return true; | |
else | |
return false; | |
} | |
template <class type, int SIZE> | |
bool Stack<type, SIZE>::full() { | |
return ((t + 1) == SIZE) ? true : false; | |
} | |
template <class type, int SIZE> | |
int Stack<type, SIZE>::count() { | |
return t + 1; | |
} | |
template <class type, int SIZE> | |
int Stack<type, SIZE>::size() { | |
return SIZE; | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment