Question

In: Computer Science

• Implement the codes must use the LinkedList implementation • Add an additional empty node (“dummy...

• Implement the codes must use the LinkedList implementation

• Add an additional empty node (“dummy node”) that connects the end of the list with the beginning, transforming the list to a circular list

Code in c++

The Josephus problem is named after the historian Flavius Josephus, who lived between the years 37 and 100 CE. Josephus was a reluctant leader of the Jewish revolt against the Roman Empire. When it appeared that Josephus and his band were to be captured, they resolved to kill themselves. Josephus persuaded the group by saying, “Let us commit our mutual deaths to determination by lot. He to whom the first lot falls, let him be killed by him that hath the second lot, and thus fortune shall make its progress through us all; nor shall any of us erish by his own right hand, for it would be unfair if, when the rest are gone, somebody should repent and save himself” (Flavius Josephus, The Wars of the Jews, Book III, Chapter 8, Verse 7, tr. William Whiston, 1737). Yet that is exactly what happened; Josephus was left for last, and he and the person he was to kill surrendered to the Romans. Although Josephus does not describe how the lots were assigned, the following approach is generally believed to be the way it was done. People form a circle and count around the circle some predetermined number. When this number is reached, that person receives a lot and leaves the circle. The count starts over with the next person. Using the circular linked list developed in Exercise 6, simulate this problem.

Your program should take two parameters: n, the number of people that start, and

m, the number of counts. For example, try n = 20 and m = 12. Where does Josephus need to be in the original list so that he is the last one chosen?

Solutions

Expert Solution

#include<bits/stdc++.h>
using namespace std;
  

struct Node
{
int position;
struct Node *next;
};
  

Node *newNode(int pos)
{
Node *temp = new Node;
temp->next = temp;
temp->position = pos;
}
  
/* Function to find the only person left
after one in every m-th node is killed
in a circle of n nodes */
void josephusPosition(int m, int n)
{
// Create a circular linked list of
// size N.
Node *head = newNode(1);
Node *prev = head;
for (int i = 2; i <= n; i++)
{
prev->next = newNode(i);
prev = prev->next;
}
prev->next = head; // Connect last
// node to first
  
/* while only one node is left in the
linked list*/
Node *ptr1 = head, *ptr2 = head;
while (ptr1->next != ptr1)
{
// Find m-th node
int count = 1;
while (count != m)
{
ptr2 = ptr1;
ptr1 = ptr1->next;
count++;
}
  
/* Remove the m-th node */
ptr2->next = ptr1->next;
free(ptr1);
ptr1 = ptr2->next;
}
  
cout<<"Position of Josephus in orignal list is: "<<ptr1->position<<endl;
}
  

int main()
{
int n, m;
cout<<"Enter number of persons(n)"<<endl;
cin>>n;
cout<<"Enter the count(m)"<<endl;
cin>>m;
josephusPosition(m, n);
return 0;
}


Related Solutions

Python Add your own implementation of the function rearrange_list rearrange_list(linkedlist, number) returns the result of rearranging...
Python Add your own implementation of the function rearrange_list rearrange_list(linkedlist, number) returns the result of rearranging the nodes in linkedlist so that: all the nodes whose values are less than number are at the front all the nodes whose values are greater than number are at the rear any nodes whose values are equal to number are in the middle For example, given the linked list represented by linkedlist = -2 -> -3 -> -1 -> 2 -> 3 ->...
Tasks 2 Write algorithms in pseudocode to implement a LinkedList The following operations must be performed...
Tasks 2 Write algorithms in pseudocode to implement a LinkedList The following operations must be performed on the LinkedList: *Add an element *Insert an element at a given position x. *Retrieve/Read the value of an element at position y. *Delete an element from position z. (x,y and z represent any value in the valid range of indices of the LinkedList).
Use Excel to calculate the values to fill in the empty boxes. Feel free to add...
Use Excel to calculate the values to fill in the empty boxes. Feel free to add additional tables and calculations as needed. Please use the assignment 1 discussion board to ask questions. Once completed, save this file and upload it in Canvas. Historical Demand Data 2012 to 2016: The table reproduced below is the demand data for a company (aggregated) for the previous five years. 2012 2013 2014 2015 2016 Q1 11632 15034 16117 15565 16470 Q2 22509 26824 24169...
Use Excel to calculate the values to fill in the empty boxes. Feel free to add...
Use Excel to calculate the values to fill in the empty boxes. Feel free to add additional tables and calculations as needed. Historical Demand Data 2012 to 2016: The table reproduced below is the demand data for a company (aggregated) for the previous five years. 2012 2013 2014 2015 2016 Q1 11632 15034 16117 15565 16470 Q2 22509 26824 24169 20151 42858 Q3 21646 13314 14505 13392 19278 Q4 11355 10698 11176 10613 13934 Annual Demand 67,142 65,870 65,967 59,721...
C++ tree program (please do NOT use struct Node, use classes) Program 1 Implement a Binary...
C++ tree program (please do NOT use struct Node, use classes) Program 1 Implement a Binary tree using an array Program 2 Implement a tree using linked list - pointer Binary Tree Program 3 - Convert program 1 to a template (include screenshots please of output)
C++ tree program (please do NOT use struct Node, use classes) Program 1 Implement a Binary...
C++ tree program (please do NOT use struct Node, use classes) Program 1 Implement a Binary tree using an array Program 2 Implement a tree using linked list - pointer Binary Tree Program 3 - Convert program 1 to a template
Use this implementation of Integer node, public class IntegerNode { public int item; public IntegerNode next;...
Use this implementation of Integer node, public class IntegerNode { public int item; public IntegerNode next; public IntegerNode(int newItem) { item = newItem; next = null; } // end constructor public IntegerNode(int newItem, IntegerNode nextNode) { item = newItem; next = nextNode; } // end constructor } // end class IntegerNode You need to implement add( ), delete( ), traverse( ) methods for an ordered linked list. And after insertion and deletion, your linked list will remain ordered. Your code...
Initialize an empty hashtable, programmatically add 5 items (colors, names, fruits, etc.), use Write-Host to print...
Initialize an empty hashtable, programmatically add 5 items (colors, names, fruits, etc.), use Write-Host to print the keys and values separately, then remove one of the items and print the entire hashtable POWERSHELL PLEASE!
C Program only - MUST USE MALLOC IN CODE Research and implement the Sieve of Eratosthenes....
C Program only - MUST USE MALLOC IN CODE Research and implement the Sieve of Eratosthenes. Example Program Session (implement some linefeed ‘\n’ formatting): Enter the limit: 1000 Primes up to 1000    2    3    5    7   11   13   17   19   23   29   31   37   41   43   47   53 59   61   67   71   73   79   83   89   97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211...
Directions: Use a CPT manual to obtain codes. Do not leave any blank spaces and all letters must be capitalized.
Directions: Use a CPT manual to obtain codes. Do not leave any blank spaces and all letters must be capitalized.a. Circumcision using a clamp with regional dorsal penile block      b. Office visit for an established patient requiring a detailed history, expanded problem-focused examination, and low-complexity medical decision making      c. Anesthesia for a healthy, 10-month-old male patient having blepharoplasty (code only the anesthesia service—two codes are required)      d. X-ray of the shoulder, 1 view      e. Replacement during follow-up care of cast application of the left...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT