Make two improvements to the DynIntStack class (provided): 1. Write a copy const
ID: 3552422 • Letter: M
Question
Make two improvements to the DynIntStack class (provided):
1. Write a copy constructor for the DynIntStack class. Explicitly call copy
constructor in main().
2. Make pop member function throw an exception of type int when someone tries
to pop element from an empty stack. Run a test in main(): pop from an empty
stack, use try-catch block to handle the exception.
Here is the code
// Specification file for the DynIntStack class
#ifndef DYNINTSTACK_H
#define DYNINTSTACK_H
class DynIntStack
{
private:
// Structure for stack nodes
struct StackNode
{
int value; // Value in the node
StackNode *next; // Pointer to the next node
};
StackNode *top; // Pointer to the stack top
public:
// Constructor
DynIntStack()
{ top = NULL; }
// Destructor
~DynIntStack();
// Stack operations
void push(int);
void pop(int &);
bool isEmpty();
};
#endif
#include <iostream>
#include "DynIntStack.h"
using namespace std;
//**************************************************
// Destructor *
// This function deletes every node in the list. *
//**************************************************
DynIntStack::~DynIntStack()
{
StackNode *nodePtr, *nextNode;
// Position nodePtr at the top of the stack.
nodePtr = top;
// Traverse the list deleting each node.
while (nodePtr != NULL)
{
nextNode = nodePtr->next;
delete nodePtr;
nodePtr = nextNode;
}
}
//************************************************
// Member function push pushes the argument onto *
// the stack. *
//************************************************
void DynIntStack::push(int num)
{
StackNode *newNode; // Pointer to a new node
// Allocate a new node and store num there.
newNode = new StackNode;
newNode->value = num;
// If there are no nodes in the list
// make newNode the first node.
if (isEmpty())
{
top = newNode;
newNode->next = NULL;
}
else // Otherwise, insert NewNode before top.
{
newNode->next = top;
top = newNode;
}
}
//****************************************************
// Member function pop pops the value at the top *
// of the stack off, and copies it into the variable *
// passed as an argument. *
//****************************************************
void DynIntStack::pop(int &num)
{
StackNode *temp; // Temporary pointer
// First make sure the stack isn't empty.
if (isEmpty())
{
cout << "The stack is empty. ";
}
else // pop value off top of stack
{
num = top->value;
temp = top->next;
delete top;
top = temp;
}
}
//****************************************************
// Member function isEmpty returns true if the stack *
// is empty, or false otherwise. *
//****************************************************
bool DynIntStack::isEmpty()
{
bool status;
if (!top)
status = true;
else
status = false;
return status;
}
Explanation / Answer
Dear,
#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
//**************************************************
// 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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.