Question

In: Computer Science

Define a problem utilizing Stacks. Write the design, code and display output. please in c++ oop?

Define a problem utilizing Stacks. Write the design, code and display output. please in c++ oop?

Solutions

Expert Solution

#######################################
      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.


Related Solutions

C++ program. Please explain how the code resulted in the output. The code and output is...
C++ program. Please explain how the code resulted in the output. The code and output is listed below. Code: #include <iostream> #include <string> using namespace std; int f(int& a, int b) {    int tmp = a;    a = b;    if (tmp == 0) { cout << tmp << ' ' << a << ' ' << b << endl; }    b = tmp;    return b;    return a; } int main() {    int a...
Define a problem with user input, user output, -> operator and destructors. C ++ please
Define a problem with user input, user output, -> operator and destructors. C ++ please
please submit the C code( no third party library). the C code will create output to...
please submit the C code( no third party library). the C code will create output to a file, and iterate in a loop 60 times and each iteration is 1 second, and if any key from your keyboard is pressed will write a 1 in the file, for every second no key is pressed, will write a 0 into the output file.
What will be the expected output of the following pseudo code? Write exactly what would display...
What will be the expected output of the following pseudo code? Write exactly what would display when you execute the statements. Module main() Declare Integer a = 5 Declare Integer b = 2 Declare Integer c = 3 Declare Integer result = 0 Display "The value of result is" Display result Set result = a + b * c - a Display "Changed value is: ", result End Module
C++ please. Define a problem with user input, user output, Pointer, with const and sizeof operator.
C++ please. Define a problem with user input, user output, Pointer, with const and sizeof operator.
Please write code for C language Problem: Write a couple of functions to process arrays. Note...
Please write code for C language Problem: Write a couple of functions to process arrays. Note that from the description of the function you have to identify what would be the return type and what would be part of the parameter. display(): The function takes an int array and it’s size and prints the data in the array. sumArray(): It takes an int array and size, and returns the sum of the elements of the array. findMax(): It takes an...
Please use C++: Data Abstraction, Bags and Stacks: Define a class DoublyLinkedBag that implements the ADT...
Please use C++: Data Abstraction, Bags and Stacks: Define a class DoublyLinkedBag that implements the ADT BagInterface by using a doubly linked chain, as shown in Figure 4-10 of your textbook. You will also need to define the class Node described in Excercise 10 of Chapter 4. Your solution to this problem requires the creation/development of four files: Node.h, Node.cpp, DoublyLinkedBag.h and DoublyLinkedBag.cpp. This repository already contains BagInterface.h that contains the declaration of the BagInterface needed by this problem. Convert...
C Programming Please modify this code to display the "guess" value from the calculation and the...
C Programming Please modify this code to display the "guess" value from the calculation and the "iterations" it took to complete. Please also add the input to the program. Input: Program will prompt the user to provide the desired decimal-place accuracy, in the range of [1-15], inclusive. User prompt must clearly indicate what is a valid input. User input must be validated - ensuring that the value entered is an integer in the above range. Sample output for one perfect...
IN C++ LANGUAGE PLEASE::: Design and implement a program (name it Rectangle) to calculate and display...
IN C++ LANGUAGE PLEASE::: Design and implement a program (name it Rectangle) to calculate and display the area and perimeter of a rectangle with width = 4 and height = 8.
PLEASE DO IN C++ AND USE REPL TO WRITE CODE The following problem statement is based...
PLEASE DO IN C++ AND USE REPL TO WRITE CODE The following problem statement is based on a problem in the C++ text by Friedman & Koffman: The results of a survey of the households in your township are available for public scrutiny. Each record (struct-type entity) contains input data for one household, including a four-digit integer identification number the annual income for the household the number of household members. Assuming that no more than 25 households were surveyed, write...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT