forked from Vatsalparsaniya/Data-Structure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjosephus_problem.c
243 lines (216 loc) · 4.51 KB
/
josephus_problem.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include <stdio.h>
#include <stdlib.h>
// Node of the circular linked list.
typedef struct Node
{
int data;
struct Node *next;
} Node;
// Handle of the list.
// Head points to the first node in the list.
// Tail points to the last node in the list.
typedef struct List
{
Node *head;
Node *tail;
int length;
} List;
// Initializes a cirucular linked list.
List* initialize_list();
// Creates a node and stores the data in it.
Node* create_node(int data);
// Inserts data at the head of the list.
void insert_head(List* cll, int data);
// Deletes the node at the head position. No operation if list is empty.
void delete_head(List* cll);
// Swaps the first(Head) and last(Tail) element.
void swap_first_and_last(List* cll);
// Prints the data present in the safe node according to the josephus problem.
int josephus(List* cll, int k);
// Prints the entire list. Prints "EMPTY" if the list is empty.
void display(List* cll);
// Deallocates resources held by the list.
void destroy_list(List* cll);
int main()
{
List* cll = initialize_list();
int ele, choice, pos, k;
do
{
scanf("%d",&choice);
switch(choice)
{
// Insert at Head.
case 1:
scanf("%d",&ele);
insert_head(cll,ele);
break;
// Delete at Head.
case 2:
delete_head(cll);
break;
// Josephus problem.
case 3:
scanf("%d",&k);
ele = josephus(cll,k);
printf("%d\n",ele);
break;
// Swap first and last element.
case 4:
swap_first_and_last(cll);
break;
// Print entire list.
case 5:
display(cll);
break;
}
}
while (choice != 0);
destroy_list(cll);
return 0;
}
List* initialize_list()
{
List *list =(List*)malloc(sizeof(List));
list->head=NULL;
list->tail=NULL;
list->length=0;
return list;
}
Node* create_node(int data)
{
Node *temp=(Node*)malloc(sizeof(Node));
temp->data=data;
temp->next=NULL;
return temp;
}
void insert_head(List* cll, int data)
{
Node *temp=create_node(data);
if(cll->head==NULL) // if(length==0)
{
temp->next=temp;
cll->head=temp;
cll->tail=temp;
}
else
{
temp->next=cll->head;
cll->tail->next=temp;
cll->head=temp;
}// TODO
cll->length+=1;
return;
}
void delete_head(List* cll)
{
if(cll->head==NULL)
{
return;
}
else if(cll->head->next==cll->head)
{
free(cll->head);
cll->head=NULL;
cll->tail=NULL;
cll->length-=1;
return;
}
else
{
Node *temp=cll->head;
cll->head=temp->next;
free(temp);
cll->tail->next=cll->head;
cll->length-=1;
return;
}
}
void swap_first_and_last(List *cll)
{
if(cll->length==0)
{
return;
}
else if(cll->length==1)
{
return;
}
else
{
int temp1;
temp1=cll->head->data;
cll->head->data=cll->tail->data;
cll->tail->data=temp1;
}
}
int josephus(List *cll, int k)
{
if(cll->length==0 || cll->length==1)
{
return;
}
else if(cll->length==2)
{
if(k==0)
{
free(cll->tail);
cll->tail=cll->head;
}
else
{
free(cll->head);
cll->head=cll->tail;
}
cll->length-=1;
return cll->head->data;
}
else
{
Node *temp=cll->head;
for(int i=0;i<k;i++)
{
temp=temp->next;
}
Node *kill;
while(cll->length>1)
{
kill=temp->next;
temp->next=kill->next;
temp=kill->next;
free(kill);
cll->length-=1;
}
cll->head=temp;
cll->tail=temp;
return temp->data;
}
}
void display(List* cll)
{
Node *temp=cll->head;
if(cll->head==NULL)
{
printf("EMPTY");
}
else
{
while(temp->next!=cll->head)
{
printf("%d ",temp->data);
temp=temp->next;
}
printf("%d ",temp->data);
}printf("\n");
}
void destroy_list(List* cll)
{
Node *temp;
while(cll->length!=0)
{
temp=cll->head;
cll->head=temp->next;
free(temp);
cll->length-=1;
}
}