Question: Josephus Election A. Implement the code below, document, test and submit a full report. B. Describe how you could implement this using an array instead
Josephus Election
A. Implement the code below, document, test and submit a full report.
B. Describe how you could implement this using an array instead of the node class.
C .Implement your design in problem B (explain)
//Josephus Problem
#include
#include
using namespace std;
class node
{
public:
int item; node* next;
node(int x, node* t)
{ item = x; next = t; }
};
typedef node *link;
int main(int argc, char *argv[])
{ int i, N = atoi(argv[1]), M = atoi(argv[2]);
link t = new node(1, 0); t->next = t;
link x = t;
for (i = 2; i <= N; i++)
x = (x->next = new node(i, t));
while (x != x->next)
{
for (i = 1; i < M; i++) x = x->next;
x->next = x->next->next;
}
cout << x->item << endl;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
