forked from aryansharma23124/Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcircular_linked_list.cpp
339 lines (291 loc) · 6.4 KB
/
circular_linked_list.cpp
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include<bits/stdc++.h>
using namespace std;
struct Listnode
{
int data;
struct Listnode *next;
};
void insert_data(Listnode **head,int data){
Listnode *temp=*head;
//cout<<temp->data;
Listnode *ins=new Listnode;
ins->data=data;
ins->next=*head;
if(*head==NULL){
//ins->data=data;
ins->next=ins;
}
else{
while(temp->next!=*head){
temp=temp->next;
}
temp->next=ins;
}
*head=ins;
}
//print from tail
void printlist(Listnode *head){
Listnode *temp1=head,*temp2=head;
if(temp1!=NULL and temp1->next!=NULL){
do{
cout<<temp1->next->data;
temp1=temp1->next;
}
while(temp1!=temp2);
}
else{
cout<<"NULL HERE";
}
}
//Last node insertion when the pointer points at last node
int insert_in_an_emptylist_lnode(Listnode **last,int data){
Listnode *temp1=new Listnode;
if(*last!=NULL)
return 0;
else{
temp1->data=data;
temp1->next=temp1;
*last=temp1;
//cout<<last->data;
//last=temp1;
return 1;
}
}
int inserting_a_node_begining_using_lnode(Listnode *last,int data){
if(last==NULL){
return (insert_in_an_emptylist_lnode(&last,data));
}
else{
Listnode *insert=new Listnode;
insert->data=data;
insert->next=last->next;
last->next=insert;
return 1;
}
}
int insert_node_end_using_lastnode(Listnode **last,int data){
if(last==NULL){
return (insert_in_an_emptylist_lnode(last,data));
}
else{
Listnode *temp=*last;
Listnode *insert=new Listnode;
insert->data=data;
insert->next=temp->next;
temp->next=insert;
*last=insert;
}
//cout<<last->data;
}
int insert_between_two_nodes(Listnode **last,int data,int item){
Listnode *pointer=*last;
if(last==NULL){
return 0;
}
else{
Listnode *new_node=new Listnode;
Listnode *temp=*last;
if(temp->data==item){
new_node->data=data;
new_node->next=temp->next;
temp->next=new_node;
*last=new_node;
}
else{
while(pointer->data!=item){
pointer=pointer->next;
}
new_node->data=data;;
new_node->next=pointer->next;
pointer->next=new_node;
pointer=new_node;
}
}
//return 1;
}
void printlist_head(Listnode *head){
Listnode *temp1=head,*temp2=head;
if(temp1!=NULL and temp1->next!=NULL){
do{
cout<<temp1->data;
temp1=temp1->next;
}
while(temp1!=temp2);
}
else{
cout<<"NULL HERE";
}
}
int split_circular_list_two_halves(Listnode *last,Listnode **ll1,Listnode **ll2){
Listnode *head=last->next;
Listnode *temp1=head,*temp2=head;
while(temp2->next!=head){
temp2=temp2->next;
if(temp2->next!=head){
temp2=temp2->next;
temp1=temp1->next;
}
}
temp2->next=temp1->next;
*ll2=temp2->next;
temp1->next=head;
*ll1=temp1->next;
printlist_head(*ll1);
cout<<endl;
printlist_head(*ll2);
}
int insert_node_in_sorted_circular_linkedlist(Listnode *last,int data){
Listnode *head=last->next,*pointer_head=last->next;
if(last->next->data>data){
Listnode *new_node=new Listnode;
new_node->data=data;
new_node->next=last->next;
head=new_node;
last->next=head;
printlist_head(head);
}
else if(last->data<data){
Listnode *new_node= new Listnode;
new_node->data=data;
new_node->next=last->next;
last->next=new_node;
last=new_node;
head=last->next;
printlist_head(head);
}
else{
while(head->next->data<data){
head=head->next;
}
Listnode *new_node=new Listnode;
new_node->data=data;
new_node->next=head->next;
head->next=new_node;
printlist_head(pointer_head);
}
return 0;
}
void delete_begin(Listnode **last){
Listnode *temp=*last;
Listnode *head=temp->next;
Listnode *head_next=head->next;
temp->next=head_next;
free(head);
printlist(*last);
}
void delete_last_node(Listnode **last){
Listnode *temp=*last;
Listnode *head=temp->next;
Listnode *last_node=temp;
Listnode *traversal_pointer=temp->next;
while(traversal_pointer->next!=temp){
traversal_pointer=traversal_pointer->next;
}
last_node=traversal_pointer->next;
free(last_node);
traversal_pointer->next=temp->next;
*last=traversal_pointer;
printlist(*last);
}
void swap_first_last_node(Listnode **last){
Listnode *temp=*last;Listnode *temp2=*last;
Listnode *head_pointer=temp->next;
Listnode *traversal_pointer =temp->next;
while(traversal_pointer->next!=temp){
traversal_pointer=traversal_pointer->next;
}
temp->next=head_pointer->next;
head_pointer->next=traversal_pointer->next;
traversal_pointer->next=head_pointer;
temp=traversal_pointer->next;
//traversal_pointer=temp->next;
//*last=traversal_pointer->next;
printlist(temp);
}
// void exchange_first_last()
// {
// if(last==NULL||last->next==last)
// return;
// node *temp=last->next;
// node *head=last->next;
// if(temp->next==last)
// last=last->next;
// else
// {
// node *pre=last;
// while(temp!=last)
// {
// pre=pre->next;
// temp=temp->next;
// }
// last->next=head->next;
// head->next=pre->next;
// pre->next=head;
// last=pre->next;
// }
// }
int josephus_circle(int m,int n){
//creating a circular Linked List with integers till n
Listnode *head=new Listnode;
head->data=1;
Listnode *temp=head;
for(int i=1;i<m;i++){
Listnode *new_node=new Listnode;
new_node->data=i+1;
new_node->next=new_node;
temp->next=new_node;
temp=temp->next;
}
temp->next=head;
temp=head;
//cout<<temp->next->data<<endl;
//printlist_head(head);
int counter=0;
int nodes_deleted=0;
Listnode *prev=temp;
while(1){
counter=0;
while(counter<n-1){
prev=temp;
temp=temp->next;
counter++;
}
//cout<<temp->data;
prev->next=temp->next;
free(temp);
nodes_deleted++;
if(nodes_deleted==m-1){
return prev->data;
}
temp=prev->next;
}
return 0;
}
int main(){
Listnode *last =new Listnode;
last=NULL;
Listnode *linked_list1 =new Listnode;
linked_list1=NULL;
Listnode *linked_list2 =new Listnode;
linked_list2=NULL;
//head = NULL;
// insert_data(&head,3);
// insert_data(&head,4);
// insert_data(&head,5);
// insert_in_an_emptylist_lnode(&last,4);
// inserting_a_node_begining_using_lnode(last,3);
// insert_node_end_using_lastnode(&last,6);
// //cout<<last->data;
// //insert_between_two_nodes(&last,7,3);
// insert_between_two_nodes(&last,9,6);
// //split_circular_list_two_halves(last,&linked_list1,&linked_list2);
// //insert_node_in_sorted_circular_linkedlist(last,11);
// //delete_begin(&last);
// //delete_last_node(&last);
// swap_first_last_node(&last);
cout<<josephus_circle(14,2);
//cout<<last->data;
//cout<<head->data;
//printlist(last);
//todo insert begin end betwee sorted insert delete begin end exchange first and last
}