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 **

Taywin commented: It is your assignment. Read the forum rules before posting. -3

Recommended Answers

All 2 Replies

This smells like homework where you were given everything except the implementation for perfectShuffle(). As such, I'm going to ask that you prove you've made an honest attempt at completing the assignment. Do you understand how a perfect shuffle works? That's the first step, because you can't tell a computer to do something when you don't know how to do it manually.

Yes, I have no reason to hide that it is homework. here is what I have but it has errors. I have no coding experience that is why I'm posting to this site. I'm just hacking through the book for class to put something together but I have not worked with linked lists yet so I'm struggling here

 #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();

   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
linkedListIterator<int> tmp1; // Use for the topHalf of the list
linkedListIterator<int> tmp2; // Use for the lowerHalf of the list
linkedListIterator<int> tmp3; // the shuffled list
int length = list.length();
int i =0;
for ( linkedListIterator<int> itr = list.begin(); itr != list.end(); ++itr)
{
     if( i <=  length/2) {
                 //the first half contain elements 
                tmp2.insertLast(*itr);
}
          else {
              tmp2.insertLast(*itr);
          }
          i++; 
}
 //now shuffle the list using the temperary nodes
list.destroyList();
for ( linkedListIterator<int> itr = tmp2.begin(); itr != tmp2.end(); ++itr) {
      list.insertFirst(*itr); //insert the first one last, i.e last one becomes first one
}
for ( linkedListIterator<int> itr = tmp1.begin(); itr != tmp1.end(); ++itr) {
       list.insertFirst(*itr); //insert the first one last, i.e last one becomes first one
}
   return;
}

Here is the error list:

error C2039: 'insertLast' : is not a member of 'linkedListIterator<Type>'
1>          with
1>          [
1>              Type=int
1>          ]

and that same error for begin and end

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.