The Josephus Problem involves n people standing in a circle where every k-th person is executed until only one remains. The document provides a C implementation for solving this problem using both a recursive function and a linked list structure. It includes code for creating a circular linked list, displaying the list, and determining the surviving position based on the number of skips.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
24 views12 pages
Josephus Problem 39
The Josephus Problem involves n people standing in a circle where every k-th person is executed until only one remains. The document provides a C implementation for solving this problem using both a recursive function and a linked list structure. It includes code for creating a circular linked list, displaying the list, and determining the surviving position based on the number of skips.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12
Josephus Problem
Application for Circular Queue
• There are n 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. • Given the total number of persons n and a number k which indicates that k-1 persons are skipped and kth person is killed in circle. • The task is to choose the place in the initial circle so that you are the last one remaining and so survive. #include <stdio.h> int josephus(int n, int k) { if (n == 1) return 1; else /* The position returned by josephus(n - 1, k) is adjusted because the recursive call josephus(n - 1, k) considers the original position k%n + 1 as position 1 */ return (josephus(n - 1, k) + k-1) % n + 1; } // Driver Program to test above function int main() { int n = 14; int k = 2; printf("The chosen place is %d", josephus(n, k)); return 0; } #include <stdio.h> #include <stdlib.h> LINKED LIST IMPLEMENTATION struct node { int number; struct node *next; }; void create(struct node **); void display(struct node *); int survivor(struct node **, int); int main() { struct node *head = NULL; int survive, skip; create(&head); printf("The persons in circular list are:\n"); display(head); printf("Enter the number of persons to be skipped: "); scanf("%d", &skip); survive = survivor(&head, skip); printf("The person to survive is : %d\n", survive); free(head); return 0; } int survivor(struct node **head, int k) { struct node *p, *q; int i; q = p = *head; while (p->next != p) { for (i = 0; i < k - 1; i++) { q = p; p = p->next; } q->next = p->next; printf("%d has been killed.\n", p->number); free(p); p = q->next; } *head = p; return (p->number); } void create (struct node **head) { struct node *temp, *rear; int a, ch; do { printf("Enter a number: "); scanf("%d", &a); temp = (struct node *)malloc(sizeof(struct node)); temp->number = a; temp->next = NULL; if (*head == NULL) *head = temp; else rear->next = temp; rear = temp; printf("Do you want to add a number [1/0]? "); scanf("%d", &ch); } while (ch != 0); rear->next = *head; } void display(struct node *head) { struct node *temp; temp = head; printf("%d ", temp->number); temp = temp->next; while (head != temp) { printf("%d ", temp->number); temp = temp->next; } printf("\n"); }