-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03_josephus.c
More file actions
50 lines (45 loc) · 1.03 KB
/
Copy path03_josephus.c
File metadata and controls
50 lines (45 loc) · 1.03 KB
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
// circular linked list no need for h & z
#include <stdio.h>
#include <stdlib.h>
struct node {
int key;
struct node* next;
};
typedef struct node Node;
void circular_list_printer(Node* x) {
Node* run = x;
while(run->next != x) {
printf("%d ", run->key);
run = run->next;
}
printf("%d\n", run->key);
}
int main() {
int N, M; // N-people, M-th suicide
scanf("%d %d", &N, &M);
Node* t;
t = (Node*) malloc(sizeof *t);
Node* x = t; // x stores the starting point of the list
t->key = 1;
for (int i=2; i!=N+1; ++i) {
t->next = (Node*) malloc(sizeof *t);
t = t->next;
t->key = i;
}
t->next = x;
circular_list_printer(x);
//suicide:
t = x;
while(t->next != t) {
for (int i=1; i!=M-1; ++i) {
t = t->next;
}
Node* delete = t->next;
printf("%d ", delete->key);
t->next = delete->next;
free(delete);
t = t->next;
}
printf("%d\n", t->key);
return 0;
}