-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjosephhead.c
100 lines (88 loc) · 1.63 KB
/
josephhead.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <stdio.h>
#include <stdlib.h>
struct node{
int num;
struct node *next;
};
struct node *head =NULL;
void display();
void create();
int survivor(int);
int main()
{
int ali,skip,n,survive;
printf("N: ");
scanf("%d",&n);
create(n);
display();
printf("\n");
printf("Skip: \n");
scanf("%d",&skip);
printf("\n");
survive=survivor(skip);
printf("\n");
printf("Srurvived : %d \n",survive);
free(head);
return 0;
}
void create(int n)
{
struct node *temp,*rear;
int a;
for(int i=1;i<=n;i++)
{
printf("Enter Head");
scanf("%d",&a);
temp=(struct node *)malloc(sizeof(struct node));
temp->num=a;
temp->next=NULL;
if(head==NULL)
{
head=temp;
temp->next=head;
}
else
{
rear=head;
while(rear->next!=head)
{
rear=rear->next;
}
rear->next=temp;
temp->next=head;
}
}
}
void display()
{
struct node *temp;
temp=head;
do
{
printf("%d ",temp->num);
temp=temp->next;
}
while(temp!=head);
printf("\n");
}
int survivor(int k)
{
struct node *p,*q;
p=head;
q=head;
while(p->next!=p)
{
for(int i=0;i<k-1;i++)
{
q=p;
p=p->next;
}
q->next=p->next;
p->num=p->num--;
if(p->num==0){
free(p);}
p=q->next;
}
head=p;
return (p->num);
}