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

Dynamic Stack Template In this chapter you studied DynIntStack, a class that imp

ID: 3625877 • Letter: D

Question

Dynamic Stack Template In this chapter you studied DynIntStack, a class that implements a dynamic stack of integers. Write a template that will create a dynamic stack of any data type. Demonstrate the class with a driver program. Static Queue Template In this chapter you studied IntQueue, a class that implements a static queue of integers. Write a template that will create a static queue of any data type. Demonstrate the class with a driver program. Error Testing The DynIntStaek and DynIntQueue classes shown in this chapter are abstract data types using a dynamic stack and dynamic queue, respectively. The classes do not currently test for memory allocation errors. Modify the classes so they determine if new nodes cannot be created by handling the bad_alloc exception. NOTE: If you have already done Programming Challenges 2 and 4, modify the templates you created.

Explanation / Answer

#include<iostream>

using namespace std;

template<class T>

class DynIntStack

{

private:

struct StackNode

{

T value;

StackNode *next;

};

StackNode *top;

public:

DynIntStack()

{

top = NULL;

}

~DynIntStack();

void push(T);

void pop(T &);

bool isEmpty();

};

template<class T>

DynIntStack<T>::~DynIntStack()

{

StackNode *nodePtr, *nextNode;

nodePtr = top;

while (nodePtr != NULL)

{

nextNode = nodePtr->next;

delete nodePtr;

nodePtr = nextNode;

}

}

template<class T>

void DynIntStack<T>::push(T num)

{

StackNode *newNode;

newNode = new StackNode;

newNode->value = num;

if (isEmpty())

{

top = newNode;

newNode->next = NULL;

}

else

{

newNode->next = top;

top = newNode;

}

}

template<class T>

void DynIntStack<T>::pop(T &num)

{

StackNode *temp;

if (isEmpty())

{

cout << " The stack is empty.";

}

else

{

num = top->value;

temp = top->next;

delete top;

top = temp;

}

}

template<class T>

bool DynIntStack<T>::isEmpty()

{

bool status;

if (!top)

status = true;

else

status = false;

return status;

}

int main()

{

int catchVar;

char catchVar1;

DynIntStack<int> stack;

DynIntStack<char> stack1;

cout << " Pushing O";

stack1.push('O');

cout << "Pushing L ";

stack1.push('L');

cout << "Pushing L ";

stack1.push('L');

cout << "Pushing E ";

stack1.push('E');

cout << "Pushing H ";

stack1.push('H');

cout << "Popping... ";

stack1.pop(catchVar1);

cout << catchVar1 << endl;

stack1.pop(catchVar1);

cout << catchVar1 << endl;

stack1.pop(catchVar1);

cout << catchVar1 << endl;

stack1.pop(catchVar1);

cout << catchVar1 << endl;

stack1.pop(catchVar1);

cout << catchVar1 << endl;

cout << "Pushing 5 ";

stack.push(5);

cout << "Pushing 10 ";

stack.push(10);

cout << "Pushing 15 ";

stack.push(15);

cout << "Popping... ";

stack.pop(catchVar);

cout << catchVar << endl;

stack.pop(catchVar);

cout << catchVar << endl;

stack.pop(catchVar);

cout << catchVar << endl;

cout << " Attempting to pop again... ";

stack.pop(catchVar);

system("pause");

return 0;