-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path81llCopyPrimeAdd.c
504 lines (471 loc) · 8.92 KB
/
81llCopyPrimeAdd.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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
#include<stdio.h>
#include<stdlib.h>
struct node{
struct node* prev;
int data;
struct node* next;
};
int insertFirst(struct node**,int);
struct node* newNode(int);
void display(struct node**);
int insertLast(struct node**,int);
int insertAtPos(struct node**,int,int);
int deleteFirst(struct node **);
int deleteLast(struct node **);
int deleteAtPos(struct node**,int);
int searchFirstOcc(struct node** Head,int no);
int searchLastOcc(struct node**,int);
int searchSecondLastOcc(struct node**,int no);
int searchAllOcc(struct node**,int no);
int concatList(struct node**,struct node**);
int concatFirstN(struct node**,struct node**,int);
int concatLastN(struct node**,struct node**,int);
int concatListRange(struct node**,struct node**,int,int);
int llCopy(struct node**,struct node**);
int llNCopy(struct node**,struct node**,int);
int llLastNCopy(struct node**,struct node**,int);
int llCopyRange(struct node**,struct node**,int,int);
int llCopyAlt(struct node**,struct node**);
int llCopyEvenAdd(struct node**,struct node**);
int llCopyPrimeAdd(struct node**,struct node**);
int evenCheck(int);
int primeCheck(int);
int main(){
struct node* head1=NULL,*head2=NULL;
int itr1=0,num,no;
printf("Linked list 1\n");
printf("How many nodes to be created.\n");
scanf("%d",&num);
for(itr1=0;itr1<num;itr1++){
printf("Element no. %d :",itr1+1);
scanf("%d",&no);
insertLast(&head1,no);
}
display(&head1);
printf("\nLinked list 2(copy of list-1)\n");
llCopyPrimeAdd(&head1,&head2);
display(&head2);
return 0;
}
int evenCheck(int num){
int sum=0,num2;
while(num>0){
num2=num%10;
sum=sum+num2;
num=num/10;
}
if(primeCheck(sum)){
return 1;
}else{
return 0;
}
}
int primeCheck(int num){
int itr1=1,cnt=0;
while(itr1<=num/2){
if(num%itr1==0){
cnt++;
}
itr1++;
}
if(cnt==1 || num==1 || num==2){
return 1;
}else{
return 0;
}
}
int llCopyPrimeAdd(struct node** src,struct node** dest){
struct node *temp=*src;
if(*src==NULL){
printf("Src list is empty.\n");
return -1;
}
while(temp!=NULL){
if(evenCheck(temp->data)){
insertLast(dest,temp->data);
}
temp=temp->next;
}
return 0;
}
int llCopyEvenAdd(struct node** src,struct node** dest){
struct node *temp=*src;
int num,sum=0;
if(*src==NULL){
printf("Src list is empty.\n");
return -1;
}
while(temp!=NULL){
if(evenCheck(temp->data)){
insertLast(dest,temp->data);
}
temp=temp->next;
}
return 0;
}
int llCopyAlt(struct node** src,struct node** dest){
struct node *temp=*src;
int cnt=1;
if(*src==NULL){
printf("No elements present to copy.\n");
return -1;
}
while(temp!=NULL){
if(cnt%2==1){
insertLast(dest,temp->data);
}
temp=temp->next;
cnt++;
}
return 0;
}
int llCopyRange(struct node** src,struct node** dest,int start,int end){
struct node *temp=*src;
int cnt=1;
if(*src==NULL){
printf("No elements present to copy.\n");
return -1;
}
while(cnt<=end){
if(cnt>=start && cnt<=end){
insertLast(dest,temp->data);
}
temp=temp->next;
cnt++;
}
return 0;
}
int llLastNCopy(struct node** src,struct node** dest,int N){
struct node *temp=*src;
if(*src==NULL){
printf("No elements present to copy.\n");
return -1;
}
while(temp->next!=NULL){
temp=temp->next;
}
while(N>0){
insertFirst(dest,temp->data);
temp=temp->prev;
N--;
}
return 0;
}
int llNCopy(struct node** src,struct node** dest,int N){
struct node *temp=*src;
if(*src==NULL){
printf("No elements present to copy.\n");
return -1;
}
while(N>0){
insertLast(dest,temp->data);
temp=temp->next;
N--;
}
return 0;
}
int llCopy(struct node** src,struct node** dest){
struct node *temp=*src;
if(*src==NULL){
printf("Src list is empty.\n");
return -1;
}
while(temp!=NULL){
insertLast(dest,temp->data);
temp=temp->next;
}
return 0;
}
int concatListRange(struct node ** src,struct node **dest,int start,int end){
struct node* temp=*dest;
struct node* tSrc=*src;
int cnt=1;
if(start>end || *src==NULL || *dest==NULL){
printf("Start is greater than end. Enter correct values\n OR src/dest list is empty.\n");
return -1;
}
while(temp->next!=NULL){
temp=temp->next;
}
while(cnt<end){
if(cnt==start){
temp->next=tSrc;
tSrc->prev=temp;
}
tSrc=tSrc->next;
cnt++;
if(cnt==end){
tSrc->next=NULL;
}
}
}
int concatLastN(struct node** src,struct node **dest,int N){
struct node *temp=*dest;
struct node *temp2=*src;
if(*src==NULL || *dest==NULL){
printf("src/dest list is empty.\n");
return -1;
}
while(temp2->next!=NULL || temp->next!=NULL){
if(temp2->next!=NULL){
temp2=temp2->next;
}
if(temp->next!=NULL){
temp=temp->next;
}
}
while(N-1>0){
temp2=temp2->prev;
N--;
}
temp->next=temp2;
temp2->prev=temp;
}
int concatFirstN(struct node** src,struct node **dest,int N){
struct node *temp=*dest;
if(*src==NULL){
printf("src list is empty.\n");
return -1;
}
if(*dest==NULL){
temp=*src;
*dest=temp;
N=N-1;
}else{
while(temp->next!=NULL){
temp=temp->next;
}
temp->next=*src;
(*src)->prev=temp;
}
while(N>0){
temp=temp->next;
N--;
}
temp->next=NULL;
return 0;
}
int concatList(struct node** src,struct node **dest){
struct node *temp=*dest;
if(*dest==NULL){
*dest=*src;
return 0;
}
if(*src==NULL){
return 0;
}
while(temp->next!=NULL){
temp=temp->next;
}
temp->next=*src;
(*src)->prev=temp;
return 0;
}
int searchAllOcc(struct node** Head,int no){
int cnt=0;
struct node *temp=*Head;
if(*Head==NULL){
printf("List does not exist.\n");
return -1;
}
while(temp!=NULL){
if(temp->data==no){
cnt++;
}
temp=temp->next;
}
if(cnt==0){
printf("Element does not exists.\n");
return -1;
}else{
printf("Output: %d\n",cnt);
return 0;
}
}
int searchSecondLastOcc(struct node** Head,int no){
int pos2=0,pos3=0;
int pos=1;
struct node *temp=*Head;
if(*Head==NULL){
printf("List does not exist.\n");
return -1;
}
while(temp!=NULL){
if(temp->data==no){
pos3=pos2;
pos2=pos;
}
temp=temp->next;
pos++;
}
if(pos3==0){
printf("Element %d does not exists at second last pos.\n",no);
return -1;
}else{
printf("Output: %d\n",pos3);
return 0;
}
}
int searchLastOcc(struct node** Head,int no){
int pos2=0;
int pos=1;
struct node *temp=*Head;
if(*Head==NULL){
printf("List does not exist.\n");
return -1;
}
while(temp!=NULL){
if(temp->data==no){
pos2=pos;
}
temp=temp->next;
pos++;
}
if(pos2==0){
printf("Element %d does not exists.\n",no);
return -1;
}else{
printf("Output: %d\n",pos2);
return 0;
}
}
int searchFirstOcc(struct node** Head,int no){
struct node * temp=*Head;
int pos=1;
if(*Head==NULL){
printf("List does not exist.\n");
return -1;
}
printf("Output: ");
while(temp!=NULL){
if(temp->data==no){
printf("%d\n",pos);
return 0;
}
temp=temp->next;
pos++;
}
printf("Element does not exists.\n");
}
int deleteAtPos(struct node ** Head,int pos){
struct node * temp=*Head;
if(*Head==NULL){
printf("List does not exist.\n");
return -1;
}
if(pos==1){
deleteFirst(Head);
return 0;
}else{
while(pos>1){
temp=temp->next;
pos--;
}
temp->prev->next=temp->next;
if(temp->next!=NULL){
temp->next->prev=temp->prev;
}
free(temp);
temp=NULL;
}
//printf("%d",(*Head)->data);
return 0;
}
int deleteLast(struct node** Head){
struct node* temp=*Head;
if(*Head==NULL){
printf("List does not exist.\n");
return -1;
}
while(temp->next!=NULL){
temp=temp->next;
}
temp->prev->next=NULL;
temp->prev=NULL;
free(temp);
temp=NULL;
return 0;
}
int deleteFirst(struct node** Head){
struct node* temp=*Head;
if(*Head==NULL){
printf("Nothing to delete.\n");
return -1;
}
*Head=(*Head)->next;
(*Head)->prev=NULL;
free(temp);
temp=NULL;
return 0;
}
int insertAtPos(struct node** Head,int no,int pos){
struct node * temp=*Head;
struct node * tNew=NULL;
if(*Head==NULL){
printf("List is empty.\n");
return -1;
}
if(pos==1){
insertFirst(Head,no);
}else{
while(pos-1>1){
temp=temp->next;
pos--;
}
tNew=newNode(no);
tNew->next=temp->next;
tNew->prev=temp;
tNew->next->prev=tNew;
temp->next=tNew;
}
}
int insertLast(struct node** Head,int no){
struct node *temp=*Head;
if(*Head==NULL){
temp=newNode(no);
*Head=temp;
}else{
while(temp->next!=NULL){
temp=temp->next;
}
temp->next=newNode(no);
temp->next->prev=temp;
}
}
int insertFirst(struct node** Head,int no){
struct node* temp=NULL;
if(*Head==NULL){
temp=newNode(no);
*Head=temp;
}else{
temp=newNode(no);
temp->prev=NULL;
temp->next=*Head;
temp->next->prev=temp;
*Head=temp;
}
}
struct node* newNode(int no){
struct node *temp=(struct node*)malloc(sizeof(struct node));
if(temp==NULL){
printf("Was not able to allocate memory.\n");
return 0;
}
temp->prev=NULL;
temp->next=NULL;
temp->data=no;
return temp;
}
void display(struct node** Head){
struct node *temp=*Head;
if(*Head==NULL){
printf("Nothing to display.\n");
return;
}else{
while(temp->next!=NULL){
printf("|%d|<=>",temp->data);
temp=temp->next;
}
printf("|%d|\n",temp->data);
}
}