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: 3592766 • Letter: C

Question

C++ Create a Stack Array

Create a Stack Array of todo items using the following struct

struct TodoItem
{
std::string todo;
};

• 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. Will give thumbs up for correct functional code. Thanks

Explanation / Answer

#include<bits/stdc++.h>

using namespace std;

#define MAX 1000

class Stack

{

    int top;

public:

    int a[MAX];    //Maximum size of Stack

    Stack() { top = -1; }

    bool push(int x);

    int pop();

    bool isEmpty();

};

bool Stack::push(int x)

{

    if (top >= MAX)

    {

        cout << "Stack Overflow";

        return false;

    }

    else

    {

        a[++top] = x;

        return true;

    }

}

int Stack::pop()

{

    if (top < 0)

    {

        cout << "Stack Underflow";

        return 0;

    }

    else

    {

        int x = a[top--];

        return x;

    }

}

bool Stack::isEmpty()

{

    return (top < 0);

}

// Driver program to test above functions

int main()

{

    struct Stack s;

    s.push(10);

    s.push(20);

    s.push(30);

    cout << s.pop() << " Popped from stack ";

    return 0;

}