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

Write a template that will create a dynamic stack of any data type. Demonstrate

ID: 3627243 • Letter: W

Question

Write a template that will create a dynamic stack of any data type. Demonstrate the class with a driver program.

Note: It has to be written in Microsoft Visual 2010 or 2008 and C++ language. I will only vote for working and full codes.


18-03 cpp
---------------------------------------------------------------------------
#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;
}

Explanation / Answer

// Push 5, 10, and 15 onto the stack.

// Pop the values off the stack and dispaly them.

// Pop another value off the stack.

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote