I have to implement a queue using a circular array and am having difficulty. This is what should happen: The enqueue() function returns a bool value: true if the queue is non-full prior to enqueuing the value, false if the queue is already full in which case the value is not enqueued. The dequeue() function returns the dequeued value if the queue is non-empty, or 0 if the queue is empty.
here is IntegerArrayQueue.h (Don't change):
`#pragma once
#include <iostream>
using namespace std;
class IntegerArrayQueue
{
private:
int* array; //pointer to array of integers
int front; //index of item to dequeue
int back; //index of item to enqueue
int size;
public:
IntegerArrayQueue() :
array(new int[10]), front(0), back(9), size(10)
{
for (int i = 0; i<10; i++) array[i]=0;
}
IntegerArrayQueue(int size) :
array(new int[size]), front(0), back(size-1), size(size)
{
for (int i = 0; i<size; i++) array[i]=0;
}
~IntegerArrayQueue() { delete [] array;}
void printArrayQueue()
{
for (int i = 0; i<size; i++) cout << array[i] << " ";
cout << endl;
cout << "front: " << front << endl;
cout << "back: " << back << endl;
cout << "size: " << size << endl;
}
//Implement the enqueue and dequeue functions
//enqueue: if there is space available enqueue value and
//return true, otherwise return false
bool enqueue(int value);
//dequeue: if there is a value at the front of the queue
//return the value and remove from the queue,
//otherwise return 0
int dequeue();
};`
IntegerArrayQueue.cpp:
`#include "IntegerArrayQueue.h"
bool IntegerArrayQueue::enqueue(int value)
{
if ((back + 1) % size == front) {
return false;
}
else {
back = (back + 1) % size;
array[back] = value;
return true;
}
}
int IntegerArrayQueue::dequeue()
{
if (front == 0) {
return 0;
}
else {
int val = array[front];
if (front == back) {
front = 0;
back = 0;
}
else {
front = (front) % size;
}
return val;
}
}`
anyone have any guidance on what i'm doing wrong in IntegerArrayQueue.cpp:
confused on what i'm doing wrong
dequeue()returns0iffrontis0but regardless ofback. Indequeue()you never movefrontforward. Why not to use standard container instead of raw pointer to C-style array?