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
- #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 << " List: ";
- printList(list);
- cout << endl;
- perfectShuffle(list);
- cout << " Perfect Shuffled: ";
- printList(list);
- cout << " ** Press any key to continue ** ";
- 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;
- }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.