-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjosephus.c
76 lines (70 loc) · 1.27 KB
/
josephus.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
75
76
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*head=NULL;
void create(int a[],int n)
{
int i=0;
struct node*last;
head=(struct node*)malloc(sizeof(struct node));
head->data=a[0];
head->next=head;
last=head;
for(i=1;i<n;i++)
{
struct node* t=(struct node*)malloc(sizeof(struct node));
t->data=a[i];
t->next=last->next;
last->next=t;
last=t;
}
}
void display()
{
struct node *p=head;
do
{
printf("%d ",p->data);
p=p->next;
}while(p!=head);
}
void survivor(struct node **head,int k)
{
int i;
struct node *p,*q;
p=q=*head;
while(p->next!=p)
{
for(i=1;i<k;i++)
{
q=p;
p=p->next;
}
q->next=p->next;
free(p);
p=q->next;
}
*head=p;
printf("\nThe survivor is %d ",p->data);
}
void main()
{
int n,i;
int a[20];
printf("Enter the elements\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter elements\n");
scanf("%d",&a[i]);
}
int k;
printf("How many number will be skipped\n");
scanf("%d",&k);
create(a,n);
display();
survivor(&head,k);
}