Looking for an assist to modify this program based on the following info:
Using two temporary linked list iterators and three for-loops modify a linked list with a perfectShuffle (); function (only modify the TODO line of code)..
Here is the driver code:
#include <iostream>
#include <stdio.h>
#include "linkedList.h"
using namespace std;
template <class Type>
void printList(linkedListType<Type>& list);
template <class Type>
void perfectShuffle(linkedListType<Type>& list);
int main(int argc, char **argv)
{
// Declare list variables
linkedListType<int> list;
// Add some data to list 1
list.insertLast(121);
list.insertLast(3);
list.insertLast(541);
list.insertLast(108);
list.insertLast(634);
list.insertLast(171);
list.insertLast(189);
list.insertLast(311);
list.insertLast(234);
list.insertLast(876);
// Print out the lists
cout << "\nList:\n ";
printList(list);
cout << endl;
perfectShuffle(list);
cout << "\nPerfect Shuffled:\n ";
printList(list);
cout << "\n\n** Press any key to continue **\n";
getchar();
cin.ignore(256, '\n');
cout << "Press ENTER to continue..." << endl;
cin.get();
return 0;
}
template <class Type>
void printList(linkedListType<Type>& list)
{
for (linkedListIterator<int> itr = list.begin(); itr != list.end(); ++itr)
{
cout << *itr << " ";
}
return;
}
template <class Type>
void perfectShuffle(linkedListType<Type>& list)
{
// TODO: implement the details for a perfect shuffle
return;
}
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 **