Question: My issue: Exception Error: Access violation reading location 0xDDDDDDDD I am wanting to program a function that will remove an element from a sorted doubly

My issue: Exception Error: Access violation reading location 0xDDDDDDDD

I am wanting to program a function that will remove an element from a sorted doubly link list in recursion. My function below works if I remove any element beside the head of the list. When I try to remove the head, it gives me an exception error. It gives me an exception error because I remove the head of the list which I lose the access to the head of the list but I am sure how to modify my program so that I doesn't lose the head of my list even after removing the head of my list. Program is in c++

Any help would be appreciated

Code:

DNode* DoublyLinkedList::removeFromSortedRec(DNode* head, int target){

if (head == nullptr)

{

return nullptr;

}

else if (target == head->data)

{

DNode* current = head;

DNode* nextNode = head->next;

delete current; //Error occurs

return nextNode;

}

DNode* nextNode = removeFromSortedRec(head->next, target);

head->next = nextNode;

if (nextNode)

{

nextNode->prev = head;

}

return head;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!