-
Notifications
You must be signed in to change notification settings - Fork 0
/
Josephus Circle.c
74 lines (74 loc) · 1.08 KB
/
Josephus Circle.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct node
{
int info;
struct node* next;
};
void setup();
void delNode();
struct node* head = NULL;
int n=0;
int m=0;
void setup()
{
struct node* prev;
int i;
for(i=1;i<=n;i++)
{
struct node* temp=(struct node*)malloc(sizeof(struct node));
temp->info=i;
if(i==1)
{
head=temp;
prev=temp;
}
else
{
prev->next=temp;
prev=temp;
}
}
prev->next=head;
}
void execute()
{
int e=0;
int c=1;
int f=e;
struct node *temp=head;
while(e<n-1)
{
while(c!=m-1)
{
temp=temp->next;
c++;
}
c=1;
temp->next=temp->next->next;
temp=temp->next;
struct node* temp1=head;
while(f!=n-1)
{
temp1=temp1->next;
f++;
}
e++;
f=e;
}
head=temp;
}
void display()
{
printf("The safe position in the circle is %d",head->info);
}
int main()
{
printf("Please enter the number of people in the circle (n) and the number of people to be skipped (m)\n");
scanf("%d\n%d",&n,&m);
setup();
execute();
display();
return 1;
}