The code is supposed to do queue functions which I got right.
The only problem I am having is: I am supposed to double the array size to twice its original size once the array gets completely filled.
I have coded for it but still getting garbage values when I try to put in more values than the original array size. So the problem seems to be in the inc() function below:
#ifndef Q_H_
#define Q_H_
#include <iostream>
using namespace std;
template <class elemType>
class arrayQueue
{
int size;
int *array;
int front;
int back;
int count;
public:
arrayQueue(elemType size)
{
this->size = size;
array = new int[size];
front = 0;
back = -1;
count=0;
}
bool isEmpty()
{
return (max()==0);
}
bool isFull() {
return (max()==size);
}
void enqueue(elemType entry)
{
cout << "enqueue " << entry;
if(isEmpty())
{
front = back = 0;
array[back] = entry;
count++;
}
else
{
back = (back+1) % size;
array[back] = entry;
count++;
}
cout << endl;
}
int maxsize()
{
return count;
}
void dequeue() {
cout << "dequeue : " << Front();
if(isEmpty())
{
cout << " error : empty";
}
else if(back == front)
{
back = front = -1;
}
else
{
front = (front+1) % size;
count--;
}
cout << endl;
}
void print()
{
if(isEmpty())
{
cout << "Queue is empty";
}
else
{
for(int i = front; i<count; i++)
{
cout << array[i] << " ";
}
cout << array[back];
}
//cout<<"count is:" <<count<<endl;
cout << endl;
}
int Front()
{
if(front == -1)
{
cout<<"Queue is empty\n";
return -1;
}
return array[front];
}
int Back()
{
if(back==-1)
{
cout<<"Queue is full";
}
return array[back];
}
int max()
{
return count;
cout <<"count: " <<count;
}
void inc()
{
int newsize = this->size*2;
elemType *temp = new elemType[newsize];
for (int i=0; i<this->count;i++)
{
temp[i]=this->array[(front+i) % size];
}
delete [] this->array;
this->array=temp;
this->count=newsize;
// front=array[front]; //0
//front = 0;
//back=count;
}
};
#endif /* Q_H_ */
I would really appreciate help with this.
incyou don’t updatethis->size