Please help! it\'s not compiling. #ifndef DYNAMICSTACK_H_INCLUDED #define DYNAMI
ID: 3820360 • Letter: P
Question
Please help! it's not compiling.
#ifndef DYNAMICSTACK_H_INCLUDED
#define DYNAMICSTACK_H_INCLUDED
// Specification file for the DynamicStack class
class DynamicStack
{
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
DynamicStack()
{ top = nullptr; }
// Destructor
~DynamicStack();
// Stack operations
void push(int);
void pop(int &);
bool isEmpty();
};
#endif
(main function)
#include <iostream>
#include "DynamicStack.h"
using namespace std;
//**************************************************
// Destructor *
// This function deletes every node in the list. *
//**************************************************
DynIntStack::~DynamicStack()
{
StackNode *nodePtr = nullptr, *nextNode = nullptr;
// Position nodePtr at the top of the stack.
nodePtr = top;
// Traverse the list deleting each node.
while (nodePtr != nullptr)
{
nextNode = nodePtr->next;
delete nodePtr;
nodePtr = nextNode;
}
}
//*************************************************
// Member function push pushes the argument onto *
// the stack. *
//*************************************************
void DynamicStack::push(int num)
{
StackNode *newNode = nullptr; // 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 = nullptr;
}
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 DynamicStack::pop(int &num)
{
StackNode *temp = nullptr; // 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 DynamicStack::isEmpty()
{
bool status;
if (!top)
status = true;
else
status = false;
return status;
}
Explanation / Answer
These are the mistakes you made:
I am also including the updated code to make your life easy!!
#ifndef DYNAMICSTACK_H_INCLUDED
#define DYNAMICSTACK_H_INCLUDED
// Specification file for the DynamicStack class
#include <iostream>
class DynamicStack
{
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
DynamicStack()
{ top = NULL; }
// Destructor
~DynamicStack();
// Stack operations
void push(int);
void pop(int &);
bool isEmpty();
};
#endif
MAIN FUNCTION
#include "DynamicStack.h"
#include <iostream> //this is useless since iostream is already included through the header file
using namespace std;
//**************************************************
// Destructor *
// This function deletes every node in the list. *
//**************************************************
DynamicStack::~DynamicStack()
{
StackNode *nodePtr = NULL, *nextNode = NULL;
// 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 DynamicStack::push(int num)
{
StackNode *newNode = NULL; // 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 DynamicStack::pop(int &num)
{
StackNode *temp = NULL; // 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 DynamicStack::isEmpty()
{
bool status;
if (!top)
status = true;
else
status = false;
return status;
}
int main() {} // this function is included since compilation and linking requires a main function to be present.
I hope you like the bug fixes provided and the explanation provided. If incase you face any difficult, you may comment below.