In: Computer Science
Define a problem utilizing Stacks. Write the design, code and display output. please in c++ oop?
####################################### StackArray.cpp ####################################### #include <iostream> #include "StackArray.hpp" using namespace std; TodoStackArray::TodoStackArray(){ stackTop=-1; } bool TodoStackArray::isEmpty(){ if(stackTop==-1){ return true; }else{ return false; } } bool TodoStackArray::isFull(){ if(stackTop==MAX_STACK_SIZE-1){ return true; }else{ return false; } } void TodoStackArray::push(std::string todoItem){ if(isFull()){ cout << "Stack full, cannot add new todo item." << endl; }else{ struct TodoItem* temp=new TodoItem; temp->todo=todoItem; stack[++stackTop]=temp; } } void TodoStackArray::pop(){ if(isEmpty()){ cout << "Stack empty, cannot pop an item"<<endl; }else{ stackTop--; } } TodoItem* TodoStackArray::peek(){ if(isEmpty()){ cout << "Stack empty, cannot peek." << endl; } return stack[stackTop]; } ####################################### StackArray.hpp ####################################### #ifndef HW4_TODO_STACKARRAY #define HW4_TODO_STACKARRAY #include <string> 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 ####################################### main.cpp ####################################### #include <iostream> #include "StackArray.hpp" using namespace std; int main() { TodoStackArray stack; stack.push("first thing"); stack.push("second thing"); stack.push("thrid thing"); stack.push("fourth thing"); stack.push("fifth thing"); cout << stack.peek()->todo << endl; // once fifth item is done: stack.pop(); cout << stack.peek()->todo << endl; //cout << stack.isFull() << endl; }
************************************************** Created a todo list using stacks.. Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.