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

I need help writing this program in c++. Thank You. Dynamic Stack Template Write

ID: 3669603 • Letter: I

Question

I need help writing this program in c++. Thank You.

Dynamic Stack Template

Write your own version of a class template that will create a dynamic stack of any data type. Be sure to include push, pop, and isEmpty methods.

The pop method should throw an exception if the stack is empty before attempting to remove the node from the list.

In addition, include a copy constructor and an overloaded assignment operator.

Demonstrate the class with a driver program. The program should ask the user to enter a string. The program then uses the stack to display the string in reverse order.

You may not use containers from the STL.



A possible sample run:


> Hello, World!

!dlroW ,olleH

Explanation / Answer

#include
#include "DynIntStack.h"
using namespace std;

int main()
{
DynIntStack stack;
int catchVar;
// Push values 5, 10, and 15 on the stack
for (int value = 5; value <=15; value = value + 5)
{
cout << "Push: " << value << " ";
stack.push(value);
}
cout << " ";

// Pop three values and then attempt a fourth pop
for (int k = 1; k <= 3; k++)
{
cout << "Pop: ";
stack.pop(catchVar);
cout << catchVar << endl;
}

cout << " Attempting to pop again... ";
stack.pop(catchVar);
return 0;
}

DyninStack.h
------------------------------------------------------------------------------------------------------
#ifndef DYNINTSTACK_H
#define DYNINTSTACK_H

class DynIntStack
{
private:
class StackNode
{
friend class DynIntStack;
int value;
StackNode *next;
// Constructor.
StackNode(int value1, StackNode *next1 = NULL)
{
value = value1;
next = next1;
}
};
StackNode *top;
public:
DynIntStack() { top = NULL; }
void push(int);
void pop(int &);
bool isEmpty();
};
#endif

DynIntStack.cpp
------------------------------------------------------------------------------------------
#include
#include "DynIntStack.h"
using namespace std;

//**************************************************
// Member function push pushes the argument onto *
// the stack. *
//**************************************************
void DynIntStack::push(int num)
{
top = new StackNode(num, top);
}

//*****************************************************
// Member function pop removes the value at the top *
// of the stack and copies it into the variable *
// passed as an argument. *
//*****************************************************
void DynIntStack::pop(int &num)
{
StackNode *temp;

if (isEmpty())
{
cout << "The stack is empty. ";
exit(1);
}
else // Pop value off top of stack
{
num = top->value;
temp = top;
top = top->next;
delete temp;
}
}

//*****************************************************
// Member function isEmpty returns true if the stack *
// is empty, or false otherwise. *
//*****************************************************
bool DynIntStack::isEmpty()
{
if (!top)
return true;
else
return false;
}