# C Program to Solve Josephus Problem using Linked List

This C Program Solves the Josephus Problem using Linked List. Josephus Problem talks about a problem where there are people standing in a circle waiting to be executed. The counting out begins at some point in the circle and proceeds around the circle in a fixed direction. In each step, a certain number of people are skipped and the next person is executed. The elimination proceeds around the circle (which is becoming smaller and smaller as the executed people are removed), until only the last person remains, who is given freedom.

Here is source code of the C Program to Solve Josephus Problem using Linked List. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

1. `/*`
2. ` * C Program to Solve Josephus Problem using Linked List `
3. ` */`
4. `#include <stdio.h>`
5. `#include <stdlib.h>`
6. ` `
7. `struct node`
8. `{`
9. `    int num;`
10. `    struct node *next;`
11. `};`
12. ` `
13. `void create(struct node **);`
14. `void display(struct node *);`
15. `int survivor(struct node **, int);`
16. ` `
17. `int main()`
18. `{`
19. `    struct node *head = NULL;`
20. `    int survive, skip;`
21. ` `
22. `    create(&head);`
23. `    printf("The persons in circular list are:\n");`
24. `    display(head);`
25. `    printf("Enter the number of persons to be skipped: ");`
26. `    scanf("%d", &skip);`
27. `    survive = survivor(&head, skip);`
28. `    printf("The person to survive is : %d\n", survive);`
29. `    free(head);`
30. ` `
31. `    return 0;`
32. `}`
33. ` `
34. `int survivor(struct node **head, int k)`
35. `{`
36. `    struct node *p, *q;`
37. `    int i;`
38. ` `
39. `    q = p = *head;`
40. `    while (p->next != p)`
41. `    {`
42. `        for (i = 0; i < k - 1; i++)`
43. `        {`
44. `            q = p;`
45. `            p = p->next;`
46. `        }`
47. `        q->next = p->next;`
48. `        printf("%d has been killed.\n", p->num);`
49. `        free(p);`
50. `        p = q->next;`
51. `    }`
52. `    *head = p;`
53. ` `
54. `    return (p->num);`
55. `}`
56. ` `
57. `void create (struct node **head)`
58. `{`
59. `    struct node *temp, *rear;`
60. `    int a, ch;`
61. ` `
62. `    do`
63. `    {`
64. `        printf("Enter a number: ");`
65. `        scanf("%d", &a);`
66. `        temp = (struct node *)malloc(sizeof(struct node));`
67. `        temp->num = a;`
68. `        temp->next = NULL;`
69. `        if (*head == NULL)`
70. `        {`
71. `            *head = temp;`
72. `        }`
73. `        else`
74. `        {`
75. `            rear->next = temp;`
76. `        }`
77. `        rear = temp;`
78. `        printf("Do you want to add a number [1/0]? ");`
79. `        scanf("%d", &ch);`
80. `    } while (ch != 0);`
81. `    rear->next = *head;`
82. `}`
83. ` `
84. `void display(struct node *head)`
85. `{`
86. `    struct node *temp;`
87. ` `
88. `    temp = head;`
89. `    printf("%d   ", temp->num);`
90. `    temp = temp->next;`
91. `    while (head != temp)`
92. `    {`
93. `        printf("%d   ", temp->num);`
94. `        temp = temp->next;`
95. `    }`
96. `    printf("\n");`
97. `}`

```\$ gcc josephus.c
\$ ./a.out
Enter a number: 1
Do you want to add a number [1/0]? 1
Enter a number: 2
Do you want to add a number [1/0]? 1
Enter a number: 3
Do you want to add a number [1/0]? 1
Enter a number: 4
Do you want to add a number [1/0]? 1
Enter a number: 5
Do you want to add a number [1/0]? 1
Enter a number: 6
Do you want to add a number [1/0]? 1
Enter a number: 7
Do you want to add a number [1/0]? 0
The persons in circular list are:
1   2   3   4   5   6   7
Enter the number of persons to be skipped: 3
3 has been killed.
6 has been killed.
2 has been killed.
7 has been killed.
5 has been killed.
1 has been killed.
The person to survive is : 4```

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]