Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I asked this question a bit differently earlier but I\'m trying to trim it down.

ID: 3709925 • Letter: I

Question

I asked this question a bit differently earlier but I'm trying to trim it down. I wrote a program to delete a variable from a linked list by iterating through it and deleting on match. I'd like to change it so that I iterate through recursively but I'm struggling. Any help would be appreciated.

My header has a class ...

class LinkedList{

//some other stuff

bool del(char ch);

struct node
{
char data;
node * next;
node * prev;
};
node * head, *tail;

}

My source contains the iterative delete that I'd like to make recursive.

bool LinkedList::del(char var){

struct node *curr;
struct node *prev;
struct node *next;

//empty?
if (head == NULL){
cout << "You gave me an empty list." << endl;
return false;
}

if (head->data == var){
next = head->next;
delete head;
head = next;
return true;
}
curr = head->next;

while (curr != NULL){
if (curr->data == var)
{
prev = curr->prev;
next = curr->next;
delete curr;
prev->next = next;
return true;
}

curr = curr->next;

}

return false;

}

Explanation / Answer

bool LinkedList::del(struct node *tempHead,char var){

struct node *curr;

struct node *prev;

struct node *next;

curr = tempHead;

//empty?

//

if (tempHead == NULL){

cout << "You gave me an empty list." << endl;

return false;

}

// Head contains data;

if (tempHead->data == var){

next = head->next;

delete head;

head = next;

return true;

}

// If current is null return

if(curr==NULL){

return false;

}

// if current has data then delete node update pointer

if (curr->data == var)

{

prev = curr->prev;

next = curr->next;

delete curr;

prev->next = next;

next->prev = prev;

return true;

}

// If data does not match then get next pointer call del again

else{

curr = curr->next;

del(curr,var);

}

return false;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote