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

C++ Create a Stack Array Create a Stack Array of todo items using the following

ID: 3592973 • Letter: C

Question

C++ Create a Stack Array

Create a Stack Array of todo items using the following struct

DO NOT COPY AND PASTE IRRELEVANT CODE PLEASE USE THE HEADER FILE PROVIDED AS WELL AS MY CODE.

struct TodoItem
{
std::string todo;
};

So, you will be working on a stack array which stores todo items, for example: "Go to the grocery store, clean the house, etc..

• Do NOT add a main method to any of your submitted files.
• DO write your own test drivers to test your code, but do not submit them.
• Your code needs to be readable, efficient, and accomplish the task provided.
• Make sure you delete your dynamically allocated memory in the appropriate
methods!
• When working with array-based implementations, there is a max size
available (set to 5 for this assignment in the header files). Display an error
message if attempting to add to a full array:
“Stack full, cannot add new todo item.”
Or “Queue full, cannot add new todo item.”
Note – this does not apply to linked-list implementations.
• If the stack or queue is empty when you try to pop or peek it, display an error
message:
“Stack empty, cannot pop an item.”
“Stack empty, cannot peek.”
“Queue empty, cannot dequeue an item.”
“Queue empty, cannot peek.”
• Make sure your code is commented enough to describe what it is doing.
Include a comment block at the top of all .cpp files with your name,
assignment number, and course instructor, and anyone you worked with.
• You must implement the functions as specified. You do not need any
additional functions. Each .hpp file has one or more getter methods that are
defined in the header. You do not need to implement these.
• Use the following code for your header files, and name them as indicated. DO
NOT MODIFY.

Header file:

#ifndef HW4_TODO_STACKARRAY
#define HW4_TODO_STACKARRAY
#include
struct TodoItem
{
std::string todo;
};

const int MAX_STACK_SIZE = 5;
class TodoStackArray
{
public:
TodoStackArray();
bool isEmpty();
bool isFull();
void push(std::string todoItem);
void pop();
TodoItem* peek();
int getStackTop() { return stackTop; }
TodoItem** getStack() { return stack; }
private:
int stackTop; //the index in stack[] that will be popped next
TodoItem* stack[MAX_STACK_SIZE];
};
#endif

You must use the header file. Please add comments and explain what you are doing and why. If you could create a main function to test it that would be great. Will give thumbs up for correct functional code THAT FOLLOWS THE INSTRUCTIONS AND DO NOT COPY AND PASTE IRRELEVANT CODE PLEASE USE THE HEADER FILE PROVIDED AS WELL AS MY CODE.

Here is my code so far please correct any mistakes you see:

#include <iostream>

using namespace std;

#include "HW4-Todo-StackArray.hpp"

TodoStackArray::TodoStackArray(){

TodoItem* stack[MAX_STACK_SIZE] = 0;

stackTop = -1;

}

bool TodoStackArray::isEmpty(){

if ( stackTop == -1 )

return true;

return false;

}

bool TodoStackArray::isFull(){

if( stackTop == MAX_STACK_SIZE-1 )

return true;

return false;

}

void TodoStackArray::push(std::string todoItem){

if(! isFull()){

stackTop++;

stack[stackTop] = *todoItem;

}

else{

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

return;

}

}

void TodoStackArray::pop(){

if(! isEmpty()){

stackTop--;

}

else

cout << "Stack empty, cannot pop an item." << endl;

}

TodoItem* TodoStackArray::peek(){

if(! isEmpty())

return stack[stackTop];

return -1;

}

int TodoStackArray::getStackstackstackTop() {

return stackstackTop;

}

TodoItem** TodoStackArray::getStack() {

return stack;

}

DO NOT COPY AND PASTE IRRELEVANT CODE PLEASE USE THE HEADER FILE PROVIDED AS WELL AS MY CODE. Thank you.

Explanation / Answer

Please change the following functions in your code:

TodoStackArray::TodoStackArray() {

//stack doesn't need to be initialized here in the constructor, will allocate the memory to elements when data is pushed

stackTop = -1;

}

void TodoStackArray::push(string todoItem) {

if (!isFull()) {

stackTop++;

/*Adding a new item of TodoItem type by allocating dynamic memory and setting its string value to new item */

struct TodoItem *t = new TodoItem;

t->todo = todoItem;

stack[stackTop] = t;

cout << "Added item: "<<stack[stackTop]->todo <<endl;

}

else {

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

return;

}

}

void TodoStackArray::pop() {

if (!isEmpty()) {

// Deallocate the memory of the object being deleted

delete stack[stackTop];

stackTop--;

}

else

cout << "Stack empty, cannot pop an item." << endl;

}

TodoItem* TodoStackArray::peek() {

//If stack is not empty, return the pointer to the top element.

if (!isEmpty())

return stack[stackTop];

return NULL;

}

I have used the following main function to test the complete code:

int main()

{

TodoStackArray stack;

stack.push("Hello");

cout<< (stack.peek())->todo<< endl;

stack.push("This");

cout<< stack.getStackTop() << endl;

cout<< stack.peek()->todo<<endl;

stack.push("IS");

cout<< stack.peek()->todo<<endl;

stack.push("My");

cout<< stack.getStackTop() << endl;

cout<< stack.peek()->todo<<endl;

stack.push("List");

cout<< stack.peek()->todo<<endl;

stack.push("Stack"); //Overflow condition

stack.pop();

cout<< stack.peek()->todo<<endl;

stack.pop();

cout<< stack.getStackTop() << endl;

cout<< stack.peek()->todo<<endl;

stack.pop();

cout<< stack.peek()->todo<<endl;

stack.pop();

cout<< stack.getStackTop() << endl;

cout<< stack.peek()->todo<<endl;

stack.pop();

stack.pop();

return 0;

}

Output:

Added item: Hello
Hello
Added item: This
1
This
Added item: IS
IS
Added item: My
3
My
Added item: List
List
Stack full, cannot add new todo item.
My
2
IS
This
0
Hello

Stack empty, cannot pop an item.