1 / 63

Josephus Problem: Build the Circular Linked List

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 ]);

donny
Download Presentation

Josephus Problem: Build the Circular Linked List

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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

  3. 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

  4. 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

  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; } 1 tail head

  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; } x 0 null 1 tail head

  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; } x 2 null 1 tail head

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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

  20. 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

  21. 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

  22. 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

  23. 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

  24. 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

  25. 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

  26. 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

  27. 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

  28. 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

  29. 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

  30. 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

  31. 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

  32. 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

  33. 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

  34. 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

  35. 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

  36. 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

  37. 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

  38. 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

  39. 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

  40. 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

  41. 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

  42. 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

  43. 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

  44. 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

  45. 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

  46. 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

  47. 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

  48. 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

  49. 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

  50. 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

More Related