Run the following code that implements stacks and report on

Run the following code that implements stacks and report on your results. Then, fully document the code itself using comments. from top to bottom everything documented.

#ifndef STACK_H
#define STACK_H

template<typename T>
class Stack
{
public:
Stack();
bool empty() const;
T peek() const;
void push(T value);
T pop();
int getSize() const;

private:
T elements[100];
int size;
};

template<typename T>
Stack<T>::Stack()
{
size = 0;
}

template<typename T>
bool Stack<T>::empty() const
{
return (size == 0);
}

template<typename T>
T Stack<T>::peek() const
{
return elements[size - 1];
}

template<typename T>
void Stack<T>::push(T value)
{
elements[size++] = value;
}

template<typename T>
T Stack<T>::pop()
{
return elements[--size];
}

template<typename T>
int Stack<T>::getSize() const
{
return size;
}

#endif

//////////////////////////// Test Program

#include <iostream>
#include <string>
#include \"GenericStack.h\"
using namespace std;

int main()
{
// Create a stack of int values
Stack<int> intStack;
for (int i = 0; i < 10; i++)
    intStack.push(i);

while (!intStack.empty())
    cout << intStack.pop() << \" \";
cout << endl;

// Create a stack of strings
Stack<string> stringStack;
stringStack.push(\"Chicago\");
stringStack.push(\"Denver\");
stringStack.push(\"London\");

while (!stringStack.empty())
    cout << stringStack.pop() << \" \";
cout << endl;

return 0;
}

Solution

Tested on ubuntu, Linux

/*********************GenericStack.h**************/

#ifndef STACK_H
#define STACK_H

template<typename T>
class Stack
{
public:
/* Default constructure declaration*/
Stack();
/* empty function declaration for checking stack is empty or not*/
bool empty() const;
/* peek function declaration for checking top value*/
T peek() const;
/* push function declaration for inserting value into stack*/
void push(T value);
/* pop function declaration for getting value from stack and remove value from stack as well*/
T pop();
/* getSize function declaration for checking stack size*/
int getSize() const;

private:
/*creating an array of 100 type of T*/
T elements[100];
int size;
};
/* Defalt constructure implementation and initializing size to 0*/
template<typename T>
Stack<T>::Stack()
{
size = 0;
}
/* Empty function implementation , returning value true if size is 0 else returning false*/
template<typename T>
bool Stack<T>::empty() const
{
return (size == 0);
}

/* peek function implementation , returning top value from stack*/
template<typename T>
T Stack<T>::peek() const
{
return elements[size - 1];
}

/*push function implementation for inserting value into stack*/
template<typename T>
void Stack<T>::push(T value)
{
elements[size++] = value;
}
/* pop function implementation , returns the value from stack and remove from stack as well*/
template<typename T>
T Stack<T>::pop()
{
return elements[--size];
}
/*getSize function implementation , Returning value of size*/
template<typename T>
int Stack<T>::getSize() const
{
return size;
}

#endif


/**************************Test Program*************/

#include <iostream>
#include <string>
#include \"GenericStack.h\"
using namespace std;

int main()
{
    // Create a stack of int values
    Stack<int> intStack;
    /*for loop start for inserting value into stack*/
    for (int i = 0; i < 10; i++)
    {
        intStack.push(i);//calling push method for inserting element into stack
    }
    /*while loop for printing element from stack
    *it will print value from stack until it is not empty*/
    while (!intStack.empty())
    {
        /*pop function returns value from stack and remove value from stack as well*/
        cout << intStack.pop() << \" \";
    }
    cout << endl;

    // Create a stack of strings
    Stack<string> stringStack;
    /*Inserting string value into string stack*/
    stringStack.push(\"Chicago\");
    stringStack.push(\"Denver\");
    stringStack.push(\"London\");
    /*while loop for printing element from stack
    *it will print value from stack until it is not empty*/
    while (!stringStack.empty())
    {
        /*pop function returns value from stack and remove value from stack as well*/
        cout << stringStack.pop() << \" \";
    }
    cout << endl;

    return 0;
}


/***************output***************/

raj@raj:~/Desktop/chegg$ g++ StackImpl.cpp
raj@raj:~/Desktop/chegg$ ./a.out
9 8 7 6 5 4 3 2 1 0
London Denver Chicago

If you have any query please feel free to ask

Thanks a lot

Run the following code that implements stacks and report on your results. Then, fully document the code itself using comments. from top to bottom everything doc
Run the following code that implements stacks and report on your results. Then, fully document the code itself using comments. from top to bottom everything doc
Run the following code that implements stacks and report on your results. Then, fully document the code itself using comments. from top to bottom everything doc
Run the following code that implements stacks and report on your results. Then, fully document the code itself using comments. from top to bottom everything doc

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site