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

I need help writing these stack functions in a file named Stack.cpp that goes wi

ID: 3694348 • Letter: I

Question

I need help writing these stack functions in a file named Stack.cpp that goes with this header below

#ifndef STACK_H_
#define STACK_H_

#include <string>
#include <cstddef>
using namespace std;


class Stack
{
    public:
        /**constructors and destructors*/

        Stack();
        //initializes an empty stack
        //postcondition: an empty stack

        Stack(const Stack &stack);
        //initializes stack to have same elements as another stack
        //postcondition: a copy of stack

        ~Stack();
        //frees memory allocated to the stack
        //postcondition: memory for stack has been freed

        void pop();
        //removes an element from the top of the stack
        //postcondition: an element has been removed from the top of the stack

        void push(string data);
        //adds an element to the top of the stack
        //postcondition: an element added to the top of the stack

        /**accessors*/

        string get_top();
        //returns the element at the top of the stack
        //precondition: the stack is not empty

        int get_size();
        //returns the size of the stack

        /**additional stack operations*/
        void print();
        //prints the elements in the stack in a programmer-specified format to stdout

        bool operator==(const Stack &stack);
        //returns whether this stack is equal to another stack


    private:
        struct Node
        {
            string data;
            Node* nextnode;
            Node(): nextnode(NULL), data(""){}
            Node(string data): nextnode(NULL), data(data){}

        };
        typedef Node* Nodeptr;

        Nodeptr top;
        int size;

};



#endif /* STACK_H_ */

Explanation / Answer

main.cpp

#include <iostream>
#include "Stack.h"
using namespace std;

int main()
{
    Stack s;

    cout << " Should print 1 ";
    cout << s.get_size();

    cout << " Should print get_top: ";
    cout << s.get_top();

    cout << " Pushing first onto stack ";
    s.push("first");
    cout << " Pushing second onto stack ";
    s.push("second");
    cout << " Should print second first: ";
    s.print();

    cout << " Popping the last element added ";
    s.pop();
    s.print();

    cout << " Popping the last element within stack ";
    s.pop();
    s.print();

    return 0;
}

Stack.cpp

#include "Stack.h"
#include <iostream>

using namespace std;

Stack::Stack(): size(0), top(NULL){}

Stack::~Stack()
{
    Nodeptr temp = top;
    while (temp != NULL)
    {
        temp = top->nextnode;
        delete top;
        top = temp;
    }
}

void Stack::pop()
{
    if(size == 0)
    {
        cout << "The stack is empty. Cannot pop an element. ";
        return;
    }
    else
    {
        Nodeptr tempnode = top;
        top = top->nextnode;
        delete tempnode;
        size--;
    }
}

void Stack::push(string data)
{
    if(size == 0)
    {
        top = new Node(data);
    }
    else
    {
        Nodeptr temp = new Node(data);
        temp->nextnode = top;
        top = temp;
    }
    size++;
}

int Stack::get_size()
{
    return size;
}

string Stack::get_top()
{
    if (size == 0)
        return "The stack is empty. ";
    else return top->data;
}

void Stack::print()
{
    if(size != 0)
    {
        Nodeptr temp = top;
        while(temp != NULL)
        {
            cout << temp->data << " " << endl;
            temp = temp->nextnode;
        }
    }
}

Stack.h

#ifndef STACK_H
#define STACK_H

#include <string>
#include <cstddef>
using namespace std;

class Stack
{
    public:
        /**constructors and destructors*/

        Stack();
        //initializes an empty stack
        //postcondition: an empty stack

        Stack(const Stack &stack);
        //initializes stack to have same elements as another stack
        //postcondition: a copy of stack

        ~Stack();
        //frees memory allocated to the stack
        //postcondition: memory for stack has been freed

        void pop();
        //removes an element from the top of the stack
        //postcondition: an element has been removed from the top of the stack

        void push(string data);
        //adds an element to the top of the stack
        //postcondition: an element added to the top of the stack

        /**accessors*/

        string get_top();
        //returns the element at the top of the stack
        //precondition: the stack is not empty

        int get_size();
        //returns the size of the stack

        /**additional stack operations*/
        void print();
        //prints the elements in the stack in a programmer-specified format to stdout

        bool operator==(const Stack &stack);
        //returns whether this stack is equal to another stack


    private:
        struct Node
        {
            Node* nextnode;
            string data;
            Node(): nextnode(NULL), data(""){}
            Node(string data): nextnode(NULL), data(data){}

        };
        typedef Node* Nodeptr;

        unsigned int size;
        Nodeptr top;
};

#endif /* STACK_H_ */


sample output

Should print 1                                                                                                                                              
0                                                                                                                                                           
Should print get_top:                                                                                                                                       
The stack is empty.                                                                                                                                         
                                                                                                                                                            
Pushing first onto stack                                                                                                                                    
                                                                                                                                                            
Pushing second onto stack                                                                                                                                   
                                                                                                                                                            
Should print second first:                                                                                                                                  
second                                                                                                                                                      
first                                                                                                                                                       
                                                                                                                                                            
Popping the last element added                                                                                                                              
first                                                                                                                                                       
Should print get_top:                                                                                                                                       
The stack is empty.                                                                                                                                         
                                                                                                                                                            
Pushing first onto stack                                                                                                                                    
                                                                                                                                                            
Pushing second onto stack                                                                                                                                   
                                                                                                                                                            
Should print second first:                                                                                                                                  
second                                                                                                                                                      
first                                                                                                                                                       
                                                                                                                                                            
Popping the last element added                                                                                                                              
first                                                                                                                                                       
                                                                                                                                                            
Popping the last element within stack