أحد يقدر يشرح ليي هالمسألة وحلها موجودة في ملف في لمكتبة
Write a membership function
Mix that has two parameters of linkedListType list1 and list2 . The method combines the two linked lists in one list in the following way. It takes the right half (half is the number of nodes by 2 if they are even, otherwise number of nodes by 2 plus 1) the linked list 1 and put it at the beginning of the newly created linked list, then it takes the alternate nodes of linked list 2 and connect it to the newly created linked list. You can use any member function.
List 1
[TABLE="width: 100%"]
[TR]
[TD]8
[/TD]
[/TR]
[/TABLE]
[TABLE="width: 100%"]
[TR]
[TD]10
[/TD]
[/TR]
[/TABLE]
[TABLE="width: 100%"]
[TR]
[TD]7
[/TD]
[/TR]
[/TABLE]
[TABLE="width: 100%"]
[TR]
[TD]12
[/TD]
[/TR]
[/TABLE]
[TABLE="width: 100%"]
[TR]
[TD]22
[/TD]
[/TR]
[/TABLE]
[TABLE="width: 100%"]
[TR]
[TD]90
[/TD]
[/TR]
[/TABLE]
[TABLE="width: 100%"]
[TR]
[TD]4
[/TD]
[/TR]
[/TABLE]
first last
[TABLE="width: 100%"]
[TR]
[TD]2
[/TD]
[/TR]
[/TABLE]
[TABLE="width: 100%"]
[TR]
[TD]19
[/TD]
[/TR]
[/TABLE]
[TABLE="width: 100%"]
[TR]
[TD]70
[/TD]
[/TR]
[/TABLE]
[TABLE="width: 100%"]
[TR]
[TD]82
[/TD]
[/TR]
[/TABLE]
[TABLE="width: 100%"]
[TR]
[TD]24
[/TD]
[/TR]
[/TABLE]
[TABLE="width: 100%"]
[TR]
[TD]90
[/TD]
[/TR]
[/TABLE]
List 2 first last
The produced list is
[TABLE="width: 100%"]
[TR]
[TD]12
[/TD]
[/TR]
[/TABLE]
[TABLE="width: 100%"]
[TR]
[TD]22
[/TD]
[/TR]
[/TABLE]
[TABLE="width: 100%"]
[TR]
[TD]90
[/TD]
[/TR]
[/TABLE]
[TABLE="width: 100%"]
[TR]
[TD]4
[/TD]
[/TR]
[/TABLE]
[TABLE="width: 100%"]
[TR]
[TD]2
[/TD]
[/TR]
[/TABLE]
[TABLE="width: 100%"]
[TR]
[TD]70
[/TD]
[/TR]
[/TABLE]
[TABLE="width: 100%"]
[TR]
[TD]24
[/TD]
[/TR]
[/TABLE]
first last
The prototype of the method is
void linkedListType <Type> :: Mix (linkedListType <Type> &list1, linkedListType<Type> &list2);
template <class Type>
void linkedListType <Type> :: Mix (linkedListType <Type> &list1, linkedListType<Type> &list2)
{ nodeType <Type> *current;
Int l, mid;
l = list1.length();
current = list1.first;
if (l % 2 == 0) mid= l/2;
else mid = (l+1) / 2;
for (int count = 0; count < mid; count++)
current = current ->link; // move current until we reach the middle
while (current != NULL)
{ insertLast(current-> info);
Current = current->link;
}
current = list2.first;
while (current !=NULL)
{ insertLast(current ->info);
current = current ->link;
/* we check to be sure the current in not null (no to have a error message) */
if (current ! = NULL) current = current ->link;
}
}
مافهمت الحل
