2(6) / Assume you have a doubly linked list with the following structure: class
ID: 3607008 • Letter: 2
Question
2(6) / Assume you have a doubly linked list with the following structure: class dnode f public: Head EFORE Tail dnode ) dnode (string); -dnode (); void insert (dnode ) void remove me )i Prev Next Prev Next Prev Next Prev Next Prev Next AFTER Tail Head private: string s; dnode *prev, *next; Prev Next Prev Next Prev Next Prev Next typedef dnode dnode_ptr: Prev Next Write a remove_me() function that will "unlink" the current node from the list, as shown in the pictures above. Update the head and tail pointers if needed. void dnode: :remove me (dnode_ptr &head;, dnode ptr &tai1;)Explanation / Answer
void dnode::remove_me(dnode_ptr &head, dnode_ptr &tail) {
if(head == NULL) {
return;
}
if(this == head) {
// we need to remove head pointer
dnode_ptr ptr = head;
head = head->next;
if(head == NULL) {
tail = NULL;
}
head->prev = NULL;
delete ptr;
} else {
// take 2 pointers, one last and one current
// current will always be next node of last
// last will be used to delete current node, if needed
dnode_ptr last = head;
dnode_ptr current = head->next;
while(current != null) {
if(current == this) {
last->next = current->next;
if(current->next != NULL) {
current->next->prev = last;
} else {
// if we are deleting last node, need to update tail
tail = last;
}
delete this;
return;
}
// update last and current pointers
last = current;
current = current->next;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.