I wrote my implementation to Queue array based. And I need a review for it to improve it and improve my coding skill. I also will put this implementation on my GitHub account. Thanks in advance.
//======================================================
// Author : Omar_Hafez
// Created : 29 July 2022 (Friday) 5:42:53 AM
//======================================================
#include <iostream>
enum InsertStatus { FailedQueueEmpty = -1, FailedQueueFull = -2, OK = 0 };
template <class T>
class Queue {
private:
int MAX_SIZE;
T* array;
int left = 0, right = 0, elementsCount = 0;
public:
Queue(int MAX_SIZE = 1000000)
: MAX_SIZE(MAX_SIZE), array((T*)malloc(MAX_SIZE * sizeof(T))) {}
bool empty() const {
return elementsCount == 0;
}
bool full() const {
return elementsCount == MAX_SIZE;
}
int size() const { return elementsCount; }
InsertStatus push(T const& t) {
if (full()) return FailedQueueFull;
array[right] = t;
right = (right+1)%MAX_SIZE;
elementsCount++;
return OK;
}
InsertStatus pop() {
if (empty()) return FailedQueueEmpty;
left = (left+1)%MAX_SIZE;
elementsCount--;
return OK;
}
T top() const { return array[left]; }
void clear() {
left = 0;
right = 0;
elementsCount = 0;
}
~Queue() {
delete(array);
array = nullptr;
}
};