Hi All,
I'm trying to make my own Stack Data Structure using classes. Unfortunately I ran into a very awkward problem that involves with the delete operator (No, I didn't free memory that was not supposed to be freed.)
In my own implementation of the stack, I have inputStack and removeStack as my key member function of the class "LL_Stack".
If you please take a look at the removeStack, I have commented out the statement :
delete deletePtr
Unfortunately, this totally messed up the entire output of the stack (ie. giving me weird output data). Below is the sample output without the delete statement and with the delete statement. I have pushed in the values: 4, 23, 21, 1000.
With the delete statement
1000
134520848
134520832
134520864
Without the delete statement
1000
21
23
4
I'm not exactly sure what went wrong in my removeElement function since I am deleting the deletePtr at the end of the function and all of a sudden, the next time I execute that same function, the output went beserk.
Is there something I'm missing in my code? Please take a look at the sample code below:
Class Structure
Code:
class Node
{
public:
int data_info;
Node *nextPtr;
Node ()
{
data_info = 0;
nextPtr = NULL;
}
Node (int data, Node *def_ptr = NULL)
{
data_info = data;
nextPtr = def_ptr;
}
~Node()
{
data_info;
delete nextPtr;
}
};
class LL_Stack
{
private:
Node *top_stack_ptr;
int num_elements;
public:
LL_Stack()
{
top_stack_ptr = NULL;
num_elements = 0;
}
~LL_Stack()
{
delete top_stack_ptr;
num_elements = 0;
}
void insertStack (int data_in);
void outputStack();
void removeStack();
int get_num_stack_elements();
};
Class Declarations
Code:
void LL_Stack :: outputStack()
{
Node *currentPtr;
currentPtr = top_stack_ptr;
cout << "The Following Stack Elements are: " << endl;
while (currentPtr != NULL)
{
cout << currentPtr->data_info << endl;
currentPtr = currentPtr->nextPtr;
}
}
void LL_Stack :: insertStack (int data_in)
{
Node *newNode;
newNode = new Node;
newNode->data_info = data_in;
newNode->nextPtr = NULL;
if (top_stack_ptr == NULL)
{
top_stack_ptr = newNode;
}
else
{
newNode->nextPtr = top_stack_ptr;
top_stack_ptr = newNode;
}
num_elements += 1;
}
void LL_Stack :: removeStack()
{
Node *deletePtr;
int retrieve_data;
deletePtr = top_stack_ptr;
retrieve_data = deletePtr->data_info;
cout << retrieve_data << endl;
top_stack_ptr = top_stack_ptr->nextPtr;
//delete deletePtr;
}
int LL_Stack :: get_num_stack_elements()
{
return (num_elements);
}
Main Program
Code:
int main ()
{
LL_Stack A;
A.insertStack (4);
cout << "Number of elements: " << A.get_num_stack_elements() << endl;
A.insertStack (23);
A.insertStack (21);
A.insertStack (1000);
cout << "Number of elements: " << A.get_num_stack_elements() << endl;
cout << "Elements in Stack: " << endl;
A.outputStack();
A.removeStack();
A.removeStack();
A.removeStack();
A.removeStack();
return (0);
}
I feel that not putting in the delete statement in my removeElement function will cause a major memory leak. If someone can please rectify this problem, it'll be greatly appreciated.
Thanks