Removing dups from doubly linked list in C++ -
while dry run on page works fine, when tried run code in c++, throw exception "access violation reading locaiton", please me...
void dll::compress(){ node *temp1, *temp2, *dup; temp1 = head; while (temp1 && temp1->next) { temp2 = temp1->next; while (temp2) { if (temp1->data == temp2->data) { dup = temp2; if (temp2->next != null){ temp2 = temp2->next; temp2->prev = temp1; temp1->next = temp2; delete dup; } else { temp1->next = null; delete temp2; } } else { temp2 = temp2->next; } } temp1 = temp1->next; } }
after delete temp2;
keep looping, temp2
both non-null
, invalid.
you need either set null
or break
out of loop.
Comments
Post a Comment