I'm coding this for a class (yes, this is homework). I'm just lost as to what to do to get it to perfectly shuffle the way the assignment wants it to. I've done all of the coding, even compiled and ran the code, but the shuffle isn't exactly coming out like it should. Here is the code so far, pretty much completed.
So when it runs, the shuffled numbers, in this order, are 171 634 108 541 3 121 876 234 311 189 but they should shuffle into this order: 171 634 121 189 3 311 541 234 108 876. I have a feeling it is something to do with deleting nodes that I'm not doing, but a fellow classmate mentioned something about a middle variable being taken out and then building the two tmp lists without it. That's the reason for temp3 but not using it; I have no idea how or where to put that in. Any pseudocode or guidance is greatly appreciated. Please, no code. I want to be able to code this on my own. My code is strictly for you to get an idea of where I'm at currently.
Thanks!
#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 << endl; system("PAUSE"); 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 linkedListType<int> temp1; // Use for the topHalf of the list linkedListType<int> temp2; // Use for the lowerHalf of the list linkedListType<int> temp3; // Use for the middle variable of the list int listLength = list.length(); int i = 0; for (linkedListIterator<int> itr = list.begin(); itr != list.end(); ++itr) { if( i <= listLength/2) { //the first half contain elements temp1.insertLast(*itr); } else { temp2.insertLast(*itr); } i++; } //now shuffle the list using the temperary nodes list.destroyList(); for (linkedListIterator<int> itr = temp2.begin(); itr != temp2.end(); ++itr) { list.insertFirst(*itr); //insert the first one last, i.e last one becomes first one } for (linkedListIterator<int> itr = temp1.begin(); itr != temp1.end(); ++itr) { list.insertFirst(*itr); //insert the first one last, i.e last one becomes first one } return; }
So when it runs, the shuffled numbers, in this order, are 171 634 108 541 3 121 876 234 311 189 but they should shuffle into this order: 171 634 121 189 3 311 541 234 108 876. I have a feeling it is something to do with deleting nodes that I'm not doing, but a fellow classmate mentioned something about a middle variable being taken out and then building the two tmp lists without it. That's the reason for temp3 but not using it; I have no idea how or where to put that in. Any pseudocode or guidance is greatly appreciated. Please, no code. I want to be able to code this on my own. My code is strictly for you to get an idea of where I'm at currently.
Thanks!