Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a function divideMid, which divides a list at its middle. Consider the ex

ID: 3626089 • Letter: C

Question

Create a function divideMid, which divides a list at its middle. Consider the example:
1-list;
2-List ;
Suppose l1 points to the list with elements 1 2 3 4 5 6 7 8. Then the statement
divideMid( l1, l2 );
divides the list into two sublists: l1 will have 1 2 3 4, and l2 will have 5 6 7 8.
Do not use any of the merge, sort or splice methods from the library. The above operation should be done by you completely.

By C++

Explanation / Answer

void divideMid(list &list1, list &list2) { // First Get the middle of the list 1 int mid = (list1.size())/2; int temp = 0; // temporary variable list tempList; // Create temporary list so we rewrite list 1 list::iterator it; // Iterator // Loop through the content of list 1 for(it = list1.begin(); it != list1.end(); it++) { // Copy All content of list 1 to tempList (as a temp backup) tempList.push_front(*it); temp++;// Just to check the items order // Check if the temp variable is greater than the mid so we assign values to List 2 if (temp > mid) list2.push_back(*it); } // Clear List 1, to get it prebared fo new values list1.clear(); temp = 0; // Loop throught the tempList then copy its values to List 1 for(it = tempList.begin(); it != tempList.end(); it++) { temp++; // Just to check the items order // Check if the temp > mid so we get only the values of the second section if (temp > mid) list1.push_front(*it); } }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote