c program for josefus problem


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

Comments

Popular Posts