Just so you all know this is a homework problem and I have spent hours trying to figure this little thing out. My pop()
won't work. This is my current code for pop()
:
StackElement Stack::Pop(){
Position temp;
temp = stack.CurrentEntry();
stack.Delete();
return temp;}
I am using Delete()
which is a linked list based delete()
function that goes like this:
void LinkedList::Delete(){
if (current == first && AtEnd()){
// Remove the memory allocation for the element's data
delete ¤t->data;
// Reset all values of the linked list to original (null) state
current = NULL;
pred = NULL;
first = NULL;
}
else
// Checks if the current element is the first node in the lists
if (current == first){
// Make new first element to be the next element
first = current->next;
// Remove the memory allocation for the element's data
delete ¤t->data;
// The new current entry is the successor of the deleted node.
current = first;
}
// When the element you're deleting is not the first node in list
else{
assert(!Empty());
// Temporary node to prevent current from being marroned
Node *tempNode = current->next;
pred->next = tempNode;
// Remove the memory allocation for the element's data
delete ¤t->data;
current = tempNode;
}
}
When I compile, it throws me this error right here:
Unhandled exception at 0x003E5F79 in Program5_test.exe: 0xC0000005: Access violation writing location 0xF495EE12.
You didn't show us CurrentEntry, but I bet it just returns a pointer instead of making a full copy. Then you delete the original leaving the pointer pointing at nothing. It crashes when you use the pointer returned from Pop.
User contributions licensed under CC BY-SA 3.0