I'm having trouble implementing generic queue template using circular array. The size of array is specified as a constructor parameter. I need to keep track of the index of the first and last elements in the array. When removing an element from the front of the array, instead of shifting the rest of the elements towards the front, i increment the index of the first element so that it points to the next element.I'm just trying to enqueue and dequeue now. My code is below, any help is appreciated.
#include <iostream>
template<class T>
class Queue{
public:
Queue();
Queue(int size);
virtual void enqueue(T ob);
virtual T dequeue();
protected:
T* current;
int first;
int last;
template <class T>Queue<t>::Queue(){
return;
}
template <class T>Queue<t>::Queue(int size){
current = new T[size];
}
template <class T>Queue<t>::enqueue(T a){
*current = a;
++current;
if (current ==last){
current = first;
}
}
template <class T>Queue<t>::Queue(){
T output;
output = *current;
--current;
if (current == first-1){
current = last;
--current;
}
return output;
}
int main(int argc, char* argv[]){
Queue <int> myQueue (3);
for (int i=0; i<3; i++){
myQueue.enqueue(i);
}
for (int j=0; j<3; j++){
cout<<myQueue.dequeue()<<endl;
}
#include <iostream>
template<class T>
class Queue{
public:
Queue();
Queue(int size);
virtual void enqueue(T ob);
virtual T dequeue();
protected:
T* current;
int first;
int last;
template <class T>Queue<t>::Queue(){
return;
}
template <class T>Queue<t>::Queue(int size){
current = new T[size];
}
template <class T>Queue<t>::enqueue(T a){
*current = a;
++current;
if (current ==last){
current = first;
}
}
template <class T>Queue<t>::Queue(){
T output;
output = *current;
--current;
if (current == first-1){
current = last;
--current;
}
return output;
}
int main(int argc, char* argv[]){
Queue <int> myQueue (3);
for (int i=0; i<3; i++){
myQueue.enqueue(i);
}
for (int j=0; j<3; j++){
cout<<myQueue.dequeue()<<endl;
}