Question: C++ Queue Array help Hello, I'm implementing a Queue Array which is supposed to keep track of todo items such as: go to the store
C++ Queue Array help
Hello, I'm implementing a Queue Array which is supposed to keep track of todo items such as: "go to the store" or "take out the trash" I'm not sure what is wrong with my code. It gets to the peek() function before it times out. I believe I may not be freeing up memory I need to but I'm not sure. You can make a main to test the function but solutions to the problem by adding stuff in main will not work because I have to run this through a grading program which has its own main. Here is my code along with the .hpp file I'm working with.
My code:
#include
using namespace std;
#include "HW4-Todo-QueueArray.hpp"
TodoQueueArray::TodoQueueArray() {
queueFront = -1; //the index in queue[] that will be dequeued next
queueEnd = -1; //the index in queue[] that was most recently enqueued
}
bool TodoQueueArray::isEmpty() {
return (queueFront == -1 && queueEnd == -1);
}
bool TodoQueueArray::isFull(){
if((queueEnd - MAX_QUEUE_SIZE) == queueFront)
return true;
return false;
//return (queueEnd + 1)%MAX_QUEUE_SIZE == queueFront ? true : false;
}
void TodoQueueArray::enqueue(std::string todoItem) {
if( isFull() )
{
cout
return;
}
if( isEmpty() )
{
queueFront = 0;
queueEnd = 0;
//queue[queueEnd] -> todo = todoItem;
return;
}
else
{
queueEnd = (queueEnd + 1)%MAX_QUEUE_SIZE;
}
queue[queueFront] -> todo = todoItem;
}
void TodoQueueArray::dequeue()
{
if ( isEmpty() )
{
cout
return;
}
/*
if (!isEmpty()) {
delete stack[stackTop];
stackTop--;
}
else
{
if ( queueFront > MAX_QUEUE_SIZE - 1)
queueFront = 0;
if ( queueFront == MAX_QUEUE_SIZE ){
queueFront = -1;
queueEnd = -1;
}
return;
}*/
else if (queueFront == queueEnd){
queueFront = -1;
queueEnd = -1;
//return;
}
else{
delete queue[queueFront];
queueFront = (queueFront + 1)%MAX_QUEUE_SIZE;
//return;
}
}
TodoItem* TodoQueueArray::peek() {
if (queue[queueFront] != NULL)
return queue[queueFront];
else
cout
return NULL;
}
.hpp file
#ifndef HW4_TODO_QUEUEARRAY
#define HW4_TODO_QUEUEARRAY
#include
struct TodoItem
{
std::string todo;
};
const int MAX_QUEUE_SIZE = 5;
class TodoQueueArray
{
public:
TodoQueueArray();
bool isEmpty();
bool isFull();
void enqueue(std::string todoItem);
void dequeue();
TodoItem* peek();
int getQueueFront() { return queueFront; }
int getQueueEnd() { return queueEnd; }
TodoItem** getQueue() { return queue; }
private:
int queueFront; //the index in queue[] that will be dequeued next
int queueEnd; //the index in queue[] that was most recently enqueued
TodoItem* queue[MAX_QUEUE_SIZE];
};
#endif
Output from grading program:


Will give thumbs up for correct code with explanation. Thank you
Expectec Creating queue isEmpty() says queue is empty isFull) says queue is not full Attempting to dequeue from empty queue:
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
