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

A commonly used data structure is a doubly-linked list, where each node has a po

ID: 3664522 • Letter: A

Question

A commonly used data structure is a doubly-linked list, where each node has a pointer to the next node, as well as to the node that precedes it in the list. Here is how the nodes would be declared:

Given a pointer Node * foo that points to a node in such a doubly-linked list, write code to delete that node from the list (maintaining that the remaining nodes are still in a doubly-linked list). Be sure to handle the cases where the node foo points to has no previous node, or no next node. Also, remember not to leak memory!

struct Node t int data; // In real life, this would be whatever data you want to store Node prev; I/ Pointer to the previous node in the list. Node * next; // Pointer to the next node in the list.

Explanation / Answer

Answer:

void double_llist::delete_element(int selectedvalue)
{
struct node *foo, *k;

if (start->data == selectedvalue)
{
foo = start;
start = start->next;
start->prev = NULL;
cout<<"Element Selected Is Deleted"<<endl;
free(foo);
return;
}
k = start;
while (k->next->next != NULL)
{   
  
if (k->next->data == selectedvalue)
{
foo = k->next;
k->next = foo->next;
foo->next->prev = k;
cout<<"Element Selected Is Deleted"<<endl;
free(foo);
return;
}
k = k->next;
}

if (k->next->data == selectedvalue)
{   
foo = k->next;
free(foo);
k->next = NULL;
cout<<"Element Selected Is Deleted"<<endl;
return;
}
cout<<"Element is "<<selectedvalue<<" not found"<<endl;
}

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