Given the algorithm for deleting nodes from an ordered double linked list (or a
ID: 3802597 • Letter: G
Question
Given the algorithm for deleting nodes from an ordered double linked list (or a portion of the algorithm) in real code, be able to fill in the blanks. List Node *MyDoubleLinkedList::Delete (int Key) { //Assume ListNode is a structure and contains the variable int key; // and ListNode *prev, *next; //Assume the function returns a pointer to the node if it successfully removes the node // or NULL if the node was not found ListNode *temp = head; //Search for node to delete while ((___________) && (___________)) { _____________; //Advance to next node if (________________) //Check for node to delete not found return Null; else if (______________) //Check for deleting head of the list { _____________; //Remove the head of the list if(head ! = NULL) //Make sure head is not now NULL _______________; Set head's prev pointer return temp;// Return the node removed } else //Delete node elsewhere in the list { __________________; //Move pointer of node before temp if(temp rightarrow next ! = NULL) ______________; return temp; } return NULL; //This line will never be reached but it keeps the compiler from complainingExplanation / Answer
ListNode *MyDoublyLinkedList::Delete(int Key)
{
// Assume ListNode is a structure and contains the variable int key;
// and ListNode *prev, *next;
// Assume the function returns a pointer to the node if it successfully removes the node
// or NULL if the node was not found.
ListNode *temp = head; //Initializes the temp pointer to the start of the list.
//Search for node to delete.
while((temp->next != NULL) && (temp->key <= newNode->key)) //Till you either reach the end of list, or the position to insert.
temp = temp->next; //Keep moving forward.
if(temp->key > Key) //Check for node to delete not found.
return NULL; //Confirm the node is not in the List.
else if(head->key == Key) //Check for deleting head of the list.
{
head = head->next; //Bypasses the first node.
if(head != NULL) //Make sure head is not now NULL.
head->prev = NULL; //Set head's prev pointer to NULL.
return temp; //Return the node removed.
}
else //Delete node elsewhere in the list.
{
temp->prev->next = temp->next; //Move pointer of node before temp.
if(temp->next != NULL) //If the removed node is not the last node.
temp->next->prev = temp->prev; //Set the next pointer -> prev point to the previous node.
return temp;
}
return NULL; //This line will never be reached but it keeps the compiler from complaining.
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.