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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started