Josephus Problem: Build the Circular Linked List

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

Double linked list Lai Ah Fur. The structure of node class IntDLLNode { int info; IntDLLNode next = null, prev = null; IntDLLNode() { } IntDLLNode(int.
Data Structures ADT List
Data Structure Lecture-5
Linked Lists. 2 Merge Sorted Lists Write an algorithm that merges two sorted linked lists The function should return a pointer to a single combined list.
Ics202 Data Structures. hh tail head (b) LinkedList head tail Element datum next 3 Integer Element datum next 1 Integer Element datum next 4 Integer.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Linked List Improvements & Memory. BigO's What is BigO for our basic linked list operations? InsertStart Insert at middle InsertEnd Retrieve First Value.
List class.head NULL _class Cell { void *item; Cell *next; public:... } _class List { Cell *head; public:... }
1 Recitation 9. Practice with linked lists Introduction. This recitation will go over the linked list implementation given on the back of this sheet and.
Queue using an array. .head.tail Pointers head and tail always point to the first empty slot before or after elements in the list. Thus, initially they.
List class.head NULL _class Cell { void *object; Cell *next; public:... } _class List { Cell *head; public:... }
Circular List Next field in the last node contains a pointer back to the first node rather than null pointer From any point in such a list it is possible.
Queue, Deque, and Priority Queue Implementations Chapter 11 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Chapter 101 Dynamic Data Structures and Generics Chapter 10.
Week 3 - Wednesday.  What did we talk about last time?  Started linked lists.
Garbage Collection CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
13 X 11 Java Lecture 6 CS 1311X Self-Referential Structures Building a Queue 13 X 11.
Programming Progamz pls. Importance VERY IMPORTANT.
Linked Structures See Section 3.2 of the text.. First, notice that Java allows classes to be recursive, in the sense that a class can have an element.
Created by Terri Street Copyright, 2000  1,000,0001,000,000  500,000500,000  250,000250,000  125,000125,000  64,00064,000  32,00032,000  16,00016,000.
JAVA MEMORY MODEL AND ITS IMPLICATIONS Srikanth Seshadri
12/18/2015ITK 2751 CollectionList A bstractSequentialList Vector Stack LinkedList {interface} AbstractList {abstract} ArrayList Java Provides a List interface.
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
1 Today’s Material List ADT –Definition List ADT Implementation: LinkedList.
1. Last Lecture Stacks and Queues implemented with a Linked List Doubly Linked Lists 2 Today.
CS212: DATASTRUCTURES Lecture 3: Searching 1. SinglyLinked list 2 A linked list is a series of connected nodes. Each node contains at least: – A piece.
Linked lists. Data structures to store a collection of items Data structures to store a collection of items are commonly used Typical operations on such.
 Head pointer  Last node  Build a complete linked list  Node deletion  Node insertion  Helpful hints.
CSC 205 Programming II Lecture 15 Linked List – Other Variations.
資料結構(計財系).
DATA STRUCTURE Presented By: Mahmoud Rafeek Alfarra Using C# MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
JAVA METHODS (FUNCTIONS). Why are they called methods? Java is a strictly object-oriented programming language Methods are functions inside of objects.
There are three possible lifetime patterns for Java data. Static Data The data is created when the program begins execution and is retained until the program.
3/21/2016IT 2751 Tow kinds of Lists Array What can be done? What can be easily done? student 1 student 2 student 3 student 4 Linked List student 2 student.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Queues Dale Roberts, Lecturer
Unit 3 Linked Lists King Fahd University of Petroleum & Minerals
using System; namespace Demo01 { class Program
UNIT – I Linked Lists.
© Ronaldo Menezes, Florida Tech
Abstract Data Types Polynomials CSCI 240
Algorithm for deleting a node from a singly linked list
Java collections library
Function Call Trace public class Newton {
Functions Used to write code only once Can use parameters.
Computing Adjusted Quiz Total Score
Queues.
Data Structures ADT List
Data Structures ADT List
Need for Iterators O(n2) this is a common application
Propositional Equivalences Rosen 5th and 6th Editions section 1.2
AP Java Warm-up Boolean Array.
ADT list.
Doubly Linked Lists Lecture 21 Tue, Mar 21, 2006.
Code Animation Examples
References, Objects, and Parameter Passing
Recursive GCD Demo public class Euclid {
References and Objects
class PrintOnetoTen { public static void main(String args[]) {
Java Programming with Multiple Classes
Queues CSC212.
Data Structures ADT List
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
Developing Java Applications with NetBeans
Building Java Programs
List Iterator Implementation
Developing Java Applications with NetBeans
Data Structures and Algorithms 2/2561
Presentation transcript:

Josephus Problem: Build the Circular Linked List public class Josephus { private static class Node { int val; Node next; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head = new Node(); head.val = 1; head.next = head; Node tail = head; for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; null head

Josephus Problem: Build the Circular Linked List public class Josephus { private static class Node { int val; Node next; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head = new Node(); head.val = 1; head.next = head; Node tail = head; for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; null 1 head

Josephus Problem: Build the Circular Linked List public class Josephus { private static class Node { int val; Node next; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head = new Node(); head.val = 1; head.next = head; Node tail = head; for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; 1 head

Josephus Problem: Build the Circular Linked List public class Josephus { private static class Node { int val; Node next; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head = new Node(); head.val = 1; head.next = head; Node tail = head; for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; 1 tail head

Josephus Problem: Build the Circular Linked List public class Josephus { private static class Node { int val; Node next; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head = new Node(); head.val = 1; head.next = head; Node tail = head; for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; 1 tail head

Josephus Problem: Build the Circular Linked List public class Josephus { private static class Node { int val; Node next; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head = new Node(); head.val = 1; head.next = head; Node tail = head; for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; x null 1 tail head

Josephus Problem: Build the Circular Linked List public class Josephus { private static class Node { int val; Node next; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head = new Node(); head.val = 1; head.next = head; Node tail = head; for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; x 2 null 1 tail head

Josephus Problem: Build the Circular Linked List public class Josephus { private static class Node { int val; Node next; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head = new Node(); head.val = 1; head.next = head; Node tail = head; for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; x 2 1 tail head

Josephus Problem: Build the Circular Linked List public class Josephus { private static class Node { int val; Node next; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head = new Node(); head.val = 1; head.next = head; Node tail = head; for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; x 2 1 tail head

Josephus Problem: Build the Circular Linked List public class Josephus { private static class Node { int val; Node next; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head = new Node(); head.val = 1; head.next = head; Node tail = head; for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; x 2 1 tail head

Josephus Problem: Build the Circular Linked List public class Josephus { private static class Node { int val; Node next; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head = new Node(); head.val = 1; head.next = head; Node tail = head; for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; x 2 1 tail head

Josephus Problem: Build the Circular Linked List public class Josephus { private static class Node { int val; Node next; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head = new Node(); head.val = 1; head.next = head; Node tail = head; for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; x 2 1 tail null head

Josephus Problem: Build the Circular Linked List public class Josephus { private static class Node { int val; Node next; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head = new Node(); head.val = 1; head.next = head; Node tail = head; for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; x 2 3 1 tail null head

Josephus Problem: Build the Circular Linked List public class Josephus { private static class Node { int val; Node next; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head = new Node(); head.val = 1; head.next = head; Node tail = head; for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; x 2 3 1 tail head

Josephus Problem: Build the Circular Linked List public class Josephus { private static class Node { int val; Node next; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head = new Node(); head.val = 1; head.next = head; Node tail = head; for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; x 2 3 1 tail head

Josephus Problem: Build the Circular Linked List public class Josephus { private static class Node { int val; Node next; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head = new Node(); head.val = 1; head.next = head; Node tail = head; for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; x 2 3 1 tail head

Josephus Problem: Build the Circular Linked List public class Josephus { private static class Node { int val; Node next; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head = new Node(); head.val = 1; head.next = head; Node tail = head; for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; x 2 3 1 tail head

Josephus Problem: Build the Circular Linked List public class Josephus { private static class Node { int val; Node next; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head = new Node(); head.val = 1; head.next = head; Node tail = head; for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; 2 x 3 1 tail head null

Josephus Problem: Build the Circular Linked List public class Josephus { private static class Node { int val; Node next; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head = new Node(); head.val = 1; head.next = head; Node tail = head; for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; 2 x 3 1 tail 4 head null

Josephus Problem: Build the Circular Linked List public class Josephus { private static class Node { int val; Node next; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head = new Node(); head.val = 1; head.next = head; Node tail = head; for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; 2 x 3 1 tail 4 head

Josephus Problem: Build the Circular Linked List public class Josephus { private static class Node { int val; Node next; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head = new Node(); head.val = 1; head.next = head; Node tail = head; for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; 2 x 3 1 tail 4 head

Josephus Problem: Build the Circular Linked List public class Josephus { private static class Node { int val; Node next; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head = new Node(); head.val = 1; head.next = head; Node tail = head; for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; 2 x 3 1 tail 4 head

Josephus Problem: Build the Circular Linked List public class Josephus { private static class Node { int val; Node next; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head = new Node(); head.val = 1; head.next = head; Node tail = head; for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; 2 3 1 4 head tail 5

Josephus Problem: Build the Circular Linked List public class Josephus { private static class Node { int val; Node next; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head = new Node(); head.val = 1; head.next = head; Node tail = head; for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; 2 3 1 4 head tail 5 6

Josephus Problem: Build the Circular Linked List public class Josephus { private static class Node { int val; Node next; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head = new Node(); head.val = 1; head.next = head; Node tail = head; for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; 2 3 1 4 head tail 5 6 7

Josephus Problem: Build the Circular Linked List public class Josephus { private static class Node { int val; Node next; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head = new Node(); head.val = 1; head.next = head; Node tail = head; for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; 2 3 1 4 head tail 5 8 6 7

Josephus Problem: Build the Circular Linked List public class Josephus { private static class Node { int val; Node next; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head = new Node(); head.val = 1; head.next = head; Node tail = head; for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; 2 3 1 4 head 9 tail 5 8 6 7

Josephus Problem: Build the Circular Linked List public class Josephus { private static class Node { int val; Node next; } public static void main(String[] args) { int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head = new Node(); head.val = 1; head.next = head; Node tail = head; for (int i = 2; i <= N; i++) { Node x = new Node(); x.val = i; x.next = head; tail.next = x; tail = x; 2 3 1 4 head 9 tail 5 8 6 7 Done Building

Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 1 4 9 5 8 6 7 M 5 N 9 % java Josephus 5 9

Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 1 4 x 9 5 8 6 7 M 5 N 9 % java Josephus 5 9

Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 1 4 x 9 5 8 6 7 M 5 N 9 % java Josephus 5 9

Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 1 4 x 9 5 8 6 7 M 5 N 9 i 1 % java Josephus 5 9

Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 1 4 x 9 5 8 6 7 M 5 N 9 i 1 % java Josephus 5 9

Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 1 4 x 9 5 8 6 7 M 5 N 9 i 2 % java Josephus 5 9

Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 1 4 x 9 5 8 6 7 M 5 N 9 i 2 % java Josephus 5 9

Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 1 4 x 9 5 8 6 7 M 5 N 9 i 3 % java Josephus 5 9

Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 1 4 x 9 5 8 6 7 M 5 N 9 i 3 % java Josephus 5 9

Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 1 4 x 9 5 8 6 7 M 5 N 9 i 4 % java Josephus 5 9

Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 1 4 x 9 5 8 6 7 M 5 N 9 i 4 % java Josephus 5 9

Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 1 4 x 9 5 8 6 7 M 5 N 9 i 5 % java Josephus 5 9

Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 1 4 x 9 5 8 6 7 M 5 N 9 % java Josephus 5 9 5

Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 1 4 x 9 5 is effectively deleted 5 8 6 7 M 5 N 9 % java Josephus 5 9 5

Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 1 4 x 9 8 6 7 M 5 N 9 % java Josephus 5 9 5

Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 1 4 x 9 8 6 7 M 5 N 9 i 1 % java Josephus 5 9 5

Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 1 4 x 9 8 6 7 M 5 N 9 i 1 % java Josephus 5 9 5

Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 1 4 x 9 8 6 7 M 5 N 9 i 2 % java Josephus 5 9 5

Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 1 4 x 9 8 6 7 M 5 N 9 i 2 % java Josephus 5 9 5

Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 1 4 x 9 8 6 7 M 5 N 9 i 3 % java Josephus 5 9 5

Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 1 4 x 9 8 6 7 M 5 N 9 i 3 % java Josephus 5 9 5

Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 1 4 x 9 8 6 7 M 5 N 9 i 4 % java Josephus 5 9 5

Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 1 4 x 9 8 6 7 M 5 N 9 i 4 % java Josephus 5 9 5

Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 1 4 x 9 8 6 7 M 5 N 9 i 5 % java Josephus 5 9 5

Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 1 4 x 9 8 6 7 M 5 N 9 % java Josephus 5 9 5 1

Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 1 4 x 9 8 6 7 M 5 N 9 % java Josephus 5 9 5 1

Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 4 x 9 8 6 7 M 5 N 9 % java Josephus 5 9 5 1

Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 4 x 9 8 6 7 M 5 N 9 % java Josephus 5 9 5 1 7

Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 3 x 9 8 6 M 5 N 9 % java Josephus 5 9 5 1 7 4

Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 x 9 8 6 M 5 N 9 % java Josephus 5 9 5 1 7 4 3

Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 x 9 8 M 5 N 9 % java Josephus 5 9 5 1 7 4 3 6

Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); 2 x 8 M 5 N 9 % java Josephus 5 9 5 1 7 4 3 6 9

Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); x 8 M 5 N 9 % java Josephus 5 9 5 1 7 4 3 6 9 2

Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); x 8 M 5 N 9 % java Josephus 5 9 5 1 7 4 3 6 9 2

Josephus Problem: Kill Off Every Mth Person Node x = tail; while (x != x.next) { for (int i = 1; i < M; i++) x = x.next; System.out.print(x.next.val + " "); x.next = x.next.next; } System.out.println(x.val); x Survivor 8 M 5 N 9 % java Josephus 5 9 5 1 7 4 3 6 9 2 8