-
Notifications
You must be signed in to change notification settings - Fork 630
/
Copy pathJosephus_Problem_Circular_LL.c
54 lines (51 loc) · 1.2 KB
/
Josephus_Problem_Circular_LL.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int data;
struct node *next;
} node;
node *head=NULL, *p, *temp,*temp2;
int main()
{
int size, start, step,i;
printf("Enter the size of Circular Linked List");
scanf("%d", &size);
for(i=0;i<size;i++){
p=(node*)malloc(sizeof(node));
p->data=i+1;
if(head==NULL){
head=p;
temp=head;
head->next= NULL;
}
else{
temp->next=p;
p->next=NULL;
temp=p;
}
if(i==size-1){p->next=head;}
}
temp=head;
printf("Your line-up of men is this: \n");
while(temp->next!=head){printf("%d ", temp->data); temp=temp->next;}
printf("%d\n", temp->data);
printf("Enter the starting position: ");
scanf("%d", &start);
printf("Enter the step size: ");
scanf("%d", &step);
temp=head;
while(temp->data!=start){
temp=temp->next;
}
while(temp->next!=temp){
for(i=0;i<step-1;i++){
temp=temp->next;
temp2=temp->next;
}
temp->next=temp2->next;
temp2->next=NULL;
free(temp2);
}
printf("\n");
printf("%d survives! \n", temp->data);
}