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

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
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...
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 programming to write the code to solve the following problem. Also, please use...
Please use C programming to write the code to solve the following problem. Also, please use the instructions, functions, syntax and any other required part of the problem. Thanks in advance. Use these functions below especially: void inputStringFromUser(char *prompt, char *s, int arraySize); void songNameDuplicate(char *songName); void songNameFound(char *songName); void songNameNotFound(char *songName); void songNameDeleted(char *songName); void artistFound(char *artist); void artistNotFound(char *artist); void printMusicLibraryEmpty(void); void printMusicLibraryTitle(void); const int MAX_LENGTH = 1024; You will write a program that maintains information about your...
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...
Please answer using MSP design and program for C. Write CCS code. outline the program for...
Please answer using MSP design and program for C. Write CCS code. outline the program for MSP design, and program for C. Make a project that uses one timer on the board to control the blinking of the LED. Basically setup a timer interupt. Whenever the timer interrupt happens, the LED blinks. The frequency of the blinking is chosen by the user. Next modify the program above and make a project that uses one of the pushbuttons on the board...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT