/* * squeue.cpp * * Created on: 11.11.2014 г. * Author: trifon */ #include "queue.h" int const MAX_SIZE = 100; template class SQueue : public Queue { private: T arr[MAX_SIZE]; int front, back, size; bool full() const; public: SQueue(); bool empty() const; bool push(T const&); T pop(); bool pop(T&); T head() const; bool head(T&) const; int length() const { return size; } }; template SQueue::SQueue() : front(0), back(0), size(0) {} template bool SQueue::empty() const { return size == 0; } template bool SQueue::full() const { return size == MAX_SIZE; } template bool SQueue::push(T const& x) { if (full()) return false; arr[back] = x; (++back) %= MAX_SIZE; size++; return true; } template T SQueue::pop() { if (empty()) return T(); T x = head(); (++front) %= MAX_SIZE; size--; // front++; if (front == MAX_SIZE) front = 0; return x; } template bool SQueue::pop(T& x) { x = pop(); return !empty(); } template bool SQueue::head(T& x) const { if (empty()) return false; x = arr[front]; return true; } template T SQueue::head() const { T x; head(x); return x; }