Solving the Josephus problem with circular lists Ioan Vlad Luca March 29, 2019 1 Introduction • The goal of the project is to implement the Josephus problem using circular lists. Josephus problem with circular lists consist in: given some numbers put them in a circular list. After that given a starting point delete every 3rd element from the list until only one element remains. 2 Content of the project • The project contains 3 important files: • main.cpp: containing the Josephus problem; • Circular list.h: containing the functions necessary for the list • Heap.h: containing the functions for a node 3 Functions choices • The node class contains trivial functions for accessing and changing the value or the connection of the node like: • getInfo() • getNextNode() • setValue() • etc... • The list class contains functions for viewing,deleting, changing and searching an element: • searchNodeWithInfo(int value) • addBeforeHead(int value) 1 • delete node(node *n) deletes node with address n • SkipNodesAferNode(node* start = NULL, int i = 0) jumps i nodes after node start and returns the address • printList() prints the list • getLength() • The main.cpp contains the algorithm for Josephus problem and does the reading for the array. 4 Design choices My choices of design (the functions used) come from the requires of the Josephus problem. We need first to search for the starting point in the list, for that the function searchNodeWithInfo is used. After that we need to jump 2 nodes, so i used: SkipNodesAferNode and delete it: delete node. The getLength() function is used for knowing when to stop. The pseudocode for tho Josephus problem using my choices of functions will look like: Josephus problem: begin start = searchNodeWithInfo(value) sword = SkipNodesAferNode(start,2) while (getLength() > 1) : deletenode(sword) sword = SkipNodesAferNode(sword,3) end 5 Tests and encountered issues • The program works on any given input of integers numbers, with any length smaller than 231 (int limit in Codeblocks). • One important issue is the time it take to run. Because I printed the list at every state, the algorithm takes a significant time to run. To avoid this issue the cout statements can be deleted or commented and only the final element can be printed. Another possible problem encountered is when 2 or more nodes have the same value. If this value is given as the starting point the first value in the list will be selected, so it’s best to have different content for the nodes. 2