Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Queue Array help Hello, I\'m implementing a Queue Array which is supposed to

ID: 3594153 • Letter: C

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" 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 <iostream>

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 << "Queue full, cannot add new todo item." << endl;

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 << "Queue empty, cannot dequeue an item. " << endl;

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 << "queue empty, cannot peek." <<endl;

return NULL;

}

.hpp file

#ifndef HW4_TODO_QUEUEARRAY

#define HW4_TODO_QUEUEARRAY

#include <string>

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: 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

Explanation / Answer

#include <iostream>

using namespace std;

#include "ans.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

               for(int i=0;i<MAX_QUEUE_SIZE;i++)

                              queue[i]=NULL;//Intializing all memory as NULL to avoid Grabage addressess

}

bool TodoQueueArray::isEmpty()

{

               return (queueFront == -1 && queueEnd == -1);

}

bool TodoQueueArray::isFull()

{

               if ((queueFront == 0 && queueEnd == MAX_QUEUE_SIZE-1) ||

            (queueEnd == queueFront-1))

                              return true;

               return false;

//return (queueEnd + 1)%MAX_QUEUE_SIZE == queueFront ? true : false;

}

void TodoQueueArray::enqueue(std::string todoItem)

{

               if( isFull() )

               {

                              cout << "Queue full, cannot add new todo item." << endl;

                              return;

               }

               if( isEmpty() )

               {

                              queueFront = 0;

                              queueEnd = 0;

                              if(queue[queueEnd]==NULL)//allocating memory if not already allocated

                              queue[queueEnd]=new TodoItem();

                              queue[queueEnd] -> todo = todoItem;

                              return;

               }

               else

               {

                              queueEnd = (queueEnd + 1)%MAX_QUEUE_SIZE;

               }

               if(queue[queueEnd]==NULL)//allocating memory if not already allocated

                              queue[queueEnd]=new TodoItem();

               queue[queueEnd] -> todo = todoItem;

}

void TodoQueueArray::dequeue()

{

               if ( isEmpty() )

               {

                              cout << "Queue empty, cannot dequeue an item. " << endl;

                              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)

               {

                              delete queue[queueFront];//Deleting memory

  queue[queueFront]=NULL;

                              queueFront = -1;

                              queueEnd = -1;

                              //return;

               }

               else

               {

                              delete queue[queueFront];//Deleting memory

  queue[queueFront]=NULL;

                              queueFront = (queueFront + 1)%MAX_QUEUE_SIZE;

                              //return;             

               }

}

TodoItem* TodoQueueArray::peek()

{

               if (queue[queueFront] != NULL)

                              return queue[queueFront];

               else

                              cout << "queue empty, cannot peek." <<endl;

return NULL;

}

.hpp file

#ifndef HW4_TODO_QUEUEARRAY

#define HW4_TODO_QUEUEARRAY

#include <string>

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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote