



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




***** head = (* head )-> next ; free ( temp ); }
prevPerson = newPerson ; } newPerson -> next = head ; head -> prev = newPerson ; return head ; } // Print the names of the people in the list void printList ( struct Person ***** head ) { struct Person ***** current = head ; do { cout << current -> name << " " ; current = current -> next ; } while ( current != head ); cout << endl ; } // Eliminate every mth person in the list until only one person remains void josephus ( int n , int m ) { struct Person ***** head = createList ( n ); struct Person ***** current = head ; int count = 1 ; while ( head -> next != head ) { if ( count == m ) { current -> prev -> next = current -> next ; current -> next -> prev = current -> prev ; struct Person ***** temp = current ; current = current -> next ; delete temp ; count = 1 ; printList ( head ); } else { current = current -> next ; count ++; } } cout << "The last person standing is " << head -> name << endl ; } int main () { int n , m ; cout << "Enter the number of people standing in the circle: " ; cin >> n ; cout << "Enter the number of people to be eliminated in each iteration: " ; cin >> m ; josephus ( n , m ); return 0 ; }
Critical Analysis and Conclusion