void List<DT>:: writeSub ( ListNode<DT> *p ) const // Recursive partner of the w
ID: 441576 • Letter: V
Question
void List<DT>:: writeSub ( ListNode<DT> *p ) const
// Recursive partner of the write() function. Processes the sublist
// that begins with the node pointed to by p.
{
if ( p != 0 )
{
cout << p->data; // Output data
writeSub(p->next); // Continue with next node
cout << p->dataItem; // Output data item
// Continue with next node
cout << p->dataItem; // Output data item
}
}
Explanation / Answer
method1: /* Function to reverse the linked list */ static void reverse(struct node** head_ref) { struct node* prev = NULL; struct node* current = *head_ref; struct node* next; while (current != NULL) { next = current->next; current->next = prev; prev = current; current = next; } *head_ref = prev; } method 2: void recursiveReverse(struct node** head_ref) { struct node* first; struct node* rest; /* empty list */ if (*head_ref == NULL) return; /* suppose first = {1, 2, 3}, rest = {2, 3} */ first = *head_ref; rest = first->next; /* List has only one node */ if (rest == NULL) return; /* reverse the rest list and put the first element at the end */ recursiveReverse(&rest;); first->next->next = first; /* tricky step -- see the diagram */ first->next = NULL; /* fix the head pointer */ *head_ref = rest; }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.