a) Iif we implement Mergesort on a linked list, it is obvious that dividing the
ID: 3706586 • Letter: A
Question
a) Iif we implement Mergesort on a linked list, it is obvious that dividing the list in two parts cannot be accomplished by a simple assignment as with arrays (int middle (first+ last)/2). We need to have a divideList method, which finds the middle point and returns a pointer to the beginning of the middle point. For example, if the list is: Function divideList divides the list in two parts and returns a pointer to the beginning of the second half: d le IVS?? Write the divideList function. Here is the header of the function: void divideList (node first, node & middle)Explanation / Answer
void divideList(node<T> *first,node<T>*& middle)
{
node<T> *temp=first;
// first counting the length of the list...
int n=0;
while(temp!=NULL)
{
n++;
temp=temp->next;
}
n=n/2;//making it to half...
temp=first;
while(n>0)//moving upto the half of the list..
{
temp=temp->next;
n--;
}
//now assigning to middle
middle =&temp;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.