1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
#include <list>
#include <iostream>
#include <iterator>
#include <vector>
void hotPotatoGame( int N, int M );
std::list<int>* hotPotatoLoop( std::list<int> *myList, int M, int currentCount );
std::vector<int>* placeElementsInVector( int num, std::vector<int> *V );
int main()
{
//"amountOfItegers" represents initial number of integers to loop through
int amountOfIntegers = 10;
//"amountOfPasses" represents how many elements to pass before one is deleted
int amountOfPasses = 0;
hotPotatoGame( amountOfIntegers, amountOfPasses );
return 0;
}
void hotPotatoGame( int N, int M )
{
std::cout << "Hot potato game function invoked " << std::endl;
//list of integers to loop through in the hot potato game
std::list<int> *L;
std::vector<int> initialVector;
std::vector<int> *myVector = &initialVector;
//place integers from 1 to N in vector
std::vector<int> *newVector = placeElementsInVector( N, myVector );
//initialize list full of values from myVector
std::list<int> copy( newVector->begin(), newVector->end() );
L = ©
//while list isn't empty continue the hot potato game
//for( std::list<int>::iterator itL = L->begin(); itL != L->end(); )
std::list<int> *finalList;
/*the result of the hot potato game elimination is assigned to "finalList"
* which essentially means it should be assigned an empty list*/
finalList = hotPotatoLoop( L, M, 0 );
}
std::list<int>* hotPotatoLoop( std::list<int> *myList, int M, int currentCount )
{
std::cout << "Hot potato loop function invoked" << std::endl;
if( myList->empty() )
{
return myList;
}
else
{
/*keeps track of how many passes have occurred. One for-loop ends, the
* current value of count is passed so that the next function call will
* know how many passes have already occurred*/
std:: cout << "else condition entered " << std::endl;
int count = currentCount;
for( std::list<int>::iterator itL = myList->begin(); itL != myList->end(); ++itL )
{
std::cout << "for loop entered " << std::endl;
/*if M passes have occurred, then delete the current element from the list.
* Otherwise, iterate count*/
if( count == M )
{
std::cout << "if condition met" << std::endl;
myList->erase( itL );
count = 0;
}
else
{
std::cout << "else condition met" << std::endl;
std::cout << *itL << " ";
++count;
}
}
std::cout << '\n';
return hotPotatoLoop( myList, M, count );
}
}
std::vector<int>* placeElementsInVector( int num, std::vector<int> *V )
{
std::cout << "placeElementsInVector function invoked" << std::endl;
int currentValue = 1;
for( int i = 0; i < num; i++ )
{
V->push_back( currentValue );
std::cout<< (*V)[i] << std::endl;
currentValue++;
}
return V;
}
|