#include <cstdlib>
#include <iostream>
#include <stdexcept>
#include <cmath>
template<typename T, unsigned int MAX_SIZE>
class Queue {
protected:
T array[MAX_SIZE];
unsigned int size;
unsigned int start;
unsigned int end;
public:
Queue() {
size = 0;
start = 0;
end = 0;
}
void enqueue(T val) {
if (size < MAX_SIZE) {
size++;
end = end < MAX_SIZE ? end + 1 : 0;
array[end] = val;
} else throw std::overflow_error("Queue is full");
}
T dequeue() {
if (size > 0) {
T result = array[start];
size--;
start++;
start = start < MAX_SIZE ? start + 1 : 0;
return result;
} else throw std::underflow_error("Queue is empty");
}
};
int main() {
Queue<int, 1000> q;
try {
for(int i = 0; i < 990; i++) q.enqueue(rand() % 1000);
for(int i = 0; i < 900; i++) q.dequeue();
for(int i = 0; i < 500; i++) q.enqueue(rand() % 1000);
} catch (std::exception &e) {
printf("Exception thrown: %s\n", e.what());
}
return 0;
}