630 likes | 795 Views
This Java class implements the Josephus problem using a circular linked list structure. It defines a private static nested class `Node`, which contains an integer value and a reference to the next node. The `main` method generates `N` nodes for the circular linked list, where each node's value starts from 1 and goes up to `N`. The head and tail pointers keep track of the start and end of the list. The program takes two parameters, `M` and `N`, where `M` determines the step count for elimination in the Josephus problem.
E N D
Josephus Problem: Build the Circular Linked List publicclass Josephus { privatestaticclass Node { int val; Node next; } publicstaticvoidmain(String[] args){ int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head =newNode(); head.val =1; head.next = head; Node tail = head; for(int i =2; i <= N; i++){ Node x =newNode(); x.val = i; x.next = head; tail.next = x; tail = x; } null 0 head
Josephus Problem: Build the Circular Linked List publicclass Josephus { privatestaticclass Node { int val; Node next; } publicstaticvoidmain(String[] args){ int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head =newNode(); head.val =1; head.next = head; Node tail = head; for(int i =2; i <= N; i++){ Node x =newNode(); x.val = i; x.next = head; tail.next = x; tail = x; } null 1 head
Josephus Problem: Build the Circular Linked List publicclass Josephus { privatestaticclass Node { int val; Node next; } publicstaticvoidmain(String[] args){ int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head =newNode(); head.val =1; head.next = head; Node tail = head; for(int i =2; i <= N; i++){ Node x =newNode(); x.val = i; x.next = head; tail.next = x; tail = x; } 1 head
Josephus Problem: Build the Circular Linked List publicclass Josephus { privatestaticclass Node { int val; Node next; } publicstaticvoidmain(String[] args){ int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head =newNode(); head.val =1; head.next = head; Node tail = head; for(int i =2; i <= N; i++){ Node x =newNode(); x.val = i; x.next = head; tail.next = x; tail = x; } 1 tail head
Josephus Problem: Build the Circular Linked List publicclass Josephus { privatestaticclass Node { int val; Node next; } publicstaticvoidmain(String[] args){ int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head =newNode(); head.val =1; head.next = head; Node tail = head; for(int i =2; i <= N; i++){ Node x =newNode(); x.val = i; x.next = head; tail.next = x; tail = x; } 1 tail head
Josephus Problem: Build the Circular Linked List publicclass Josephus { privatestaticclass Node { int val; Node next; } publicstaticvoidmain(String[] args){ int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head =newNode(); head.val =1; head.next = head; Node tail = head; for(int i =2; i <= N; i++){ Node x =newNode(); x.val = i; x.next = head; tail.next = x; tail = x; } x 0 null 1 tail head
Josephus Problem: Build the Circular Linked List publicclass Josephus { privatestaticclass Node { int val; Node next; } publicstaticvoidmain(String[] args){ int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head =newNode(); head.val =1; head.next = head; Node tail = head; for(int i =2; i <= N; i++){ Node x =newNode(); x.val = i; x.next = head; tail.next = x; tail = x; } x 2 null 1 tail head
Josephus Problem: Build the Circular Linked List publicclass Josephus { privatestaticclass Node { int val; Node next; } publicstaticvoidmain(String[] args){ int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head =newNode(); head.val =1; head.next = head; Node tail = head; for(int i =2; i <= N; i++){ Node x =newNode(); x.val = i; x.next = head; tail.next = x; tail = x; } x 2 1 tail head
Josephus Problem: Build the Circular Linked List publicclass Josephus { privatestaticclass Node { int val; Node next; } publicstaticvoidmain(String[] args){ int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head =newNode(); head.val =1; head.next = head; Node tail = head; for(int i =2; i <= N; i++){ Node x =newNode(); x.val = i; x.next = head; tail.next = x; tail = x; } x 2 1 tail head
Josephus Problem: Build the Circular Linked List publicclass Josephus { privatestaticclass Node { int val; Node next; } publicstaticvoidmain(String[] args){ int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head =newNode(); head.val =1; head.next = head; Node tail = head; for(int i =2; i <= N; i++){ Node x =newNode(); x.val = i; x.next = head; tail.next = x; tail = x; } x 2 1 tail head
Josephus Problem: Build the Circular Linked List publicclass Josephus { privatestaticclass Node { int val; Node next; } publicstaticvoidmain(String[] args){ int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head =newNode(); head.val =1; head.next = head; Node tail = head; for(int i =2; i <= N; i++){ Node x =newNode(); x.val = i; x.next = head; tail.next = x; tail = x; } x 2 1 tail head
Josephus Problem: Build the Circular Linked List publicclass Josephus { privatestaticclass Node { int val; Node next; } publicstaticvoidmain(String[] args){ int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head =newNode(); head.val =1; head.next = head; Node tail = head; for(int i =2; i <= N; i++){ Node x =newNode(); x.val = i; x.next = head; tail.next = x; tail = x; } x 2 0 1 tail null head
Josephus Problem: Build the Circular Linked List publicclass Josephus { privatestaticclass Node { int val; Node next; } publicstaticvoidmain(String[] args){ int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head =newNode(); head.val =1; head.next = head; Node tail = head; for(int i =2; i <= N; i++){ Node x =newNode(); 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 publicclass Josephus { privatestaticclass Node { int val; Node next; } publicstaticvoidmain(String[] args){ int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head =newNode(); head.val =1; head.next = head; Node tail = head; for(int i =2; i <= N; i++){ Node x =newNode(); x.val = i; x.next = head; tail.next = x; tail = x; } x 2 3 1 tail head
Josephus Problem: Build the Circular Linked List publicclass Josephus { privatestaticclass Node { int val; Node next; } publicstaticvoidmain(String[] args){ int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head =newNode(); head.val =1; head.next = head; Node tail = head; for(int i =2; i <= N; i++){ Node x =newNode(); x.val = i; x.next = head; tail.next = x; tail = x; } x 2 3 1 tail head
Josephus Problem: Build the Circular Linked List publicclass Josephus { privatestaticclass Node { int val; Node next; } publicstaticvoidmain(String[] args){ int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head =newNode(); head.val =1; head.next = head; Node tail = head; for(int i =2; i <= N; i++){ Node x =newNode(); x.val = i; x.next = head; tail.next = x; tail = x; } x 2 3 1 tail head
Josephus Problem: Build the Circular Linked List publicclass Josephus { privatestaticclass Node { int val; Node next; } publicstaticvoidmain(String[] args){ int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head =newNode(); head.val =1; head.next = head; Node tail = head; for(int i =2; i <= N; i++){ Node x =newNode(); x.val = i; x.next = head; tail.next = x; tail = x; } x 2 3 1 tail head
Josephus Problem: Build the Circular Linked List publicclass Josephus { privatestaticclass Node { int val; Node next; } publicstaticvoidmain(String[] args){ int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head =newNode(); head.val =1; head.next = head; Node tail = head; for(int i =2; i <= N; i++){ Node x =newNode(); x.val = i; x.next = head; tail.next = x; tail = x; } 2 x 3 1 tail 0 head null
Josephus Problem: Build the Circular Linked List publicclass Josephus { privatestaticclass Node { int val; Node next; } publicstaticvoidmain(String[] args){ int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head =newNode(); head.val =1; head.next = head; Node tail = head; for(int i =2; i <= N; i++){ Node x =newNode(); 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 publicclass Josephus { privatestaticclass Node { int val; Node next; } publicstaticvoidmain(String[] args){ int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head =newNode(); head.val =1; head.next = head; Node tail = head; for(int i =2; i <= N; i++){ Node x =newNode(); 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 publicclass Josephus { privatestaticclass Node { int val; Node next; } publicstaticvoidmain(String[] args){ int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head =newNode(); head.val =1; head.next = head; Node tail = head; for(int i =2; i <= N; i++){ Node x =newNode(); 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 publicclass Josephus { privatestaticclass Node { int val; Node next; } publicstaticvoidmain(String[] args){ int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head =newNode(); head.val =1; head.next = head; Node tail = head; for(int i =2; i <= N; i++){ Node x =newNode(); 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 publicclass Josephus { privatestaticclass Node { int val; Node next; } publicstaticvoidmain(String[] args){ int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head =newNode(); head.val =1; head.next = head; Node tail = head; for(int i =2; i <= N; i++){ Node x =newNode(); 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 publicclass Josephus { privatestaticclass Node { int val; Node next; } publicstaticvoidmain(String[] args){ int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head =newNode(); head.val =1; head.next = head; Node tail = head; for(int i =2; i <= N; i++){ Node x =newNode(); 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 publicclass Josephus { privatestaticclass Node { int val; Node next; } publicstaticvoidmain(String[] args){ int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head =newNode(); head.val =1; head.next = head; Node tail = head; for(int i =2; i <= N; i++){ Node x =newNode(); 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 publicclass Josephus { privatestaticclass Node { int val; Node next; } publicstaticvoidmain(String[] args){ int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head =newNode(); head.val =1; head.next = head; Node tail = head; for(int i =2; i <= N; i++){ Node x =newNode(); 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 publicclass Josephus { privatestaticclass Node { int val; Node next; } publicstaticvoidmain(String[] args){ int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head =newNode(); head.val =1; head.next = head; Node tail = head; for(int i =2; i <= N; i++){ Node x =newNode(); 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 publicclass Josephus { privatestaticclass Node { int val; Node next; } publicstaticvoidmain(String[] args){ int M = Integer.parseInt(args[0]); int N = Integer.parseInt(args[1]); Node head =newNode(); head.val =1; head.next = head; Node tail = head; for(int i =2; i <= N; i++){ Node x =newNode(); 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 % 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 % 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 % 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 % 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 % 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 % 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 % 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 % 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 % 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 % 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 % 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 % 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 % 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 % 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 % 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 % 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 % 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 % 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 % 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 % 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 % 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 % java Josephus 5 9 5