-
Notifications
You must be signed in to change notification settings - Fork 11
/
josephus.c
80 lines (72 loc) · 1.45 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
77
78
79
80
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;struct node *next;
};
void insert(struct node **Node,struct node **rear,int item)
{
struct node *ptr=(struct node*)malloc(sizeof(struct node));
ptr->data=item;
if(*Node==NULL)
{
*Node=ptr;
}
else{
(*rear)->next=ptr;
}
ptr->next=NULL;
*rear=ptr;
}
void traversal(struct node *head)
{
struct node *temp=head;
printf("%d ",temp->data);
temp=temp->next;
while(temp!=head)
{
printf("%d ",temp->data);
temp=temp->next;
}
}
int calculate(struct node *head,int k)
{
struct node *temp=head;
struct node *prev=head;
int i;
while(temp->next!=temp)
{
for(i=0;i<k-1;i++){
prev=temp;
temp=temp->next;
}
prev->next=temp->next;
temp=prev->next;
}
return temp->data;
}
int main()
{
struct node *Node=NULL;
struct node *rear=NULL;
int option;
while(1)
{
printf("Enter 0.Stop 1.Entry\n");
scanf("%d",&option);
if(option==1)
{
int item;
printf("Enter ");
scanf("%d",&item);
insert(&Node,&rear,item);
}
else
{
rear->next=Node;
traversal(Node);
int show=calculate(Node,3);
printf("%d",show);
break;
}
}
}