diff options
[SDP/HEP] Added preparation for "homework" during class
1 files changed, 37 insertions, 0 deletions
| -rw-r--r-- | Year 2/Structures and data algorithms/HomeworkExamPrep/Queue/Queue_Static.hpp | 37 |
diff --git a/Year 2/Structures and data algorithms/HomeworkExamPrep/Queue/Queue_Static.hpp b/Year 2/Structures and data algorithms/HomeworkExamPrep/Queue/Queue_Static.hpp new file mode 100644 index 0000000..2b9dc8d --- /dev/null +++ b/Year 2/Structures and data algorithms/HomeworkExamPrep/Queue/Queue_Static.hpp @@ -0,0 +1,37 @@ +#ifndef QUEUE_STATIC +#define QUEUE_STATIC + +#include <stdexcept> +#define QUEUE_SIZE 1024 + +template <class T> +class Queue { + T vals[QUEUE_SIZE]; + unsigned frontInd; + unsigned backInd; + +public: + void push(const T& v); + void pop(); + T& front(); +}; + +template <class T> +void Queue<T>::push(const T& v) { + vals[backInd++ % QUEUE_SIZE] = v; + + if (backInd == frontInd) + throw std::logic_error("Queue overfill!"); +} + +template <class T> +void Queue<T>::pop() { + frontInd++; +} + +template <class T> +T& Queue<T>::front() { + return vals[frontInd]; +} + +#endif |
