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:

C++ Queue Array help Hello, I'm implementing a Queue Array which issupposed to keep track of todo items such as: "go to the

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: Queue empty, cannot dequeue an item. Enqueue take out the trash isEmpty() says queue is not empty isFull) says queue is not full Enqueue wash car, mow lawn, resurface the driveway, remodel the kitchen, sell house Queue full, cannot add new todo item. Peek todo should be: take out the trash Peek todo is: take out the trash Queue end todo should be: remodel the kitchen Queue end todo is: remodel the kitchen isEmpty() says queue is not empty isFull) says queue is full Inspecting queue. Queue should contain take out the trash, wash car, mow lawn, resurface the drivew take out the trash is present. wash car is present. mow 1awn is nresent

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!