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

Looking for an assist to modify this program based on the following info: Using

ID: 3584143 • Letter: L

Question

Looking for an assist to modify this program based on the following info:

Using two temporary linked list iterators:

linkedListIterator<int> tmp1; // Use for the topHalf of the list

linkedListIterator<int> tmp2; // Use for the lowerHalf of the list

and three for-loops modify a linked list with a perfectShuffle (); function (only modify the TODO line of code)..

Here is the driver code:

Use a for-loop to find the middle element in the list (e.g. list.length()/2) and insert it into the top half temporary list. Then delete if from the main list. Use another for-loop to divide the main list into two lists by inserting the elements into the two temporary lists. Use a third for-loop to merge the lists.

Output: The output of the program after both functions are implemented will be:
List:
121 3 541 108 634 171 189 311 234 876
Perfect Shuffled:
171 634 121 189 3 311 541 234 108 876
** Press any key to continue **

Explanation / Answer

  1. #include <iostream>
  2. #include <stdio.h>
  3. #include "linkedList.h"
  4. using namespace std;
  5. template <class Type>
  6. void printList(linkedListType<Type>& list);
  7. template <class Type>
  8. void perfectShuffle(linkedListType<Type>& list);
  9. int main(int argc, char **argv)
  10. {
  11. // Declare list variables
  12. linkedListType<int> list;
  13. // Add some data to list 1
  14. list.insertLast(121);
  15. list.insertLast(3);
  16. list.insertLast(541);
  17. list.insertLast(108);
  18. list.insertLast(634);
  19. list.insertLast(171);
  20. list.insertLast(189);
  21. list.insertLast(311);
  22. list.insertLast(234);
  23. list.insertLast(876);
  24. // Print out the lists
  25. cout << " List: ";
  26. printList(list);
  27. cout << endl;
  28. perfectShuffle(list);
  29. cout << " Perfect Shuffled: ";
  30. printList(list);
  31. cout << " ** Press any key to continue ** ";
  32. return 0;
  33. }
  34. template <class Type>
  35. void printList(linkedListType<Type>& list)
  36. {
  37. for (linkedListIterator<int> itr = list.begin(); itr != list.end(); ++itr)
  38. {
  39. cout << *itr << " ";
  40. }
  41. return;
  42. }
  43. template <class Type>
  44. void perfectShuffle(linkedListType<Type>& list)
  45. {
  46. // TODO: implement the details for a perfect shuffle
  47. linkedListIterator<int> tmp1; // Use for the topHalf of the list
  48. linkedListIterator<int> tmp2; // Use for the lowerHalf of the list
  49. linkedListIterator<int> tmp3; // the shuffled list
  50. int length = list.length();
  51. int i =0;
  52. for (linkedListIterator<int> itr = list.begin(); itr != list.end(); ++itr)
  53. {
  54.      if( i <= length/2) {
  55.                  //the first half contain elements
  56.                 tmp2.insertLast(*itr);
  57. }
  58. else {
  59.               tmp2.insertLast(*itr);
  60. }
  61. i++;
  62. }
  63. //now shuffle the list using the temperary nodes
  64. list.destroyList();
  65. for (linkedListIterator<int> itr = tmp2.begin(); itr != tmp2.end(); ++itr) {
  66.       list.insertFirst(*itr); //insert the first one last, i.e last one becomes first one
  67. }
  68. for (linkedListIterator<int> itr = tmp1.begin(); itr != tmp1.end(); ++itr) {
  69.        list.insertFirst(*itr); //insert the first one last, i.e last one becomes first one
  70. }
  71. return;
  72. }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote