-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path55listSortAsc.c
441 lines (419 loc) · 8.56 KB
/
55listSortAsc.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
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node* next;
};
struct node* newNode(int no);
int insertLast(struct node **head,int);
int createNode(struct node **);
void display(struct node**);
int insertFirst(struct node **head,struct node **tail,int);
int insertAtPos(struct node**,struct node**,int no,int pos);
int deleteFirst(struct node**,struct node**);
int deleteLast(struct node**,struct node**);
int deleteAtPos(struct node**,struct node**,int);
int length(struct node**);
int searchFirstOcc(struct node**,struct node**,int);
int searchLastOcc(struct node**,struct node**,int);
int secondLastOcc(struct node**,struct node**,int);
int searchAllOcc(struct node**,struct node**,int);
int concatList(struct node**,struct node**,struct node**,struct node**);
struct node* findTail(struct node**);
int concatListN(struct node**,struct node**,struct node**,struct node**,int);
int listReverse(struct node** head,struct node**);
int listPalindrome(struct node** head,struct node** tail);
int listSortAsc(struct node**,struct node**);
int main(){
int data,num,res;
struct node *head1=NULL,*tail1=NULL;
printf("src linked list.\n");
createNode(&head1);
display(&head1);
tail1=findTail(&head1);
listSortAsc(&head1,&tail1);
display(&head1);
return 0;
}
int listSortAsc(struct node** head,struct node** tail){
struct node *temp,*temp2;
temp=*head;
temp2=*head;
int tempD;
while(temp2->next!=*head){
temp=*head;
while(temp->next!=*head){
if(temp->data>temp->next->data){
tempD=temp->data;
temp->data=temp->next->data;
temp->next->data=tempD;
}
temp=temp->next;
}
temp2=temp2->next;
}
}
int listPalindrome(struct node** head,struct node** tail){
struct node *temp=*head;
int len=length(head);
int itr1,itr2;
int arr[len];
for(itr1=0;itr1<len;itr1++){
arr[itr1]=temp->data;
temp=temp->next;
}
itr1=len-1;
itr2=0;
while(itr2!=itr1){
if(arr[itr2]!=arr[itr1]){
return -1;
}else{
return 0;
}
itr2++;
itr1--;
}
}
int listReverse(struct node** head,struct node** tail){
struct node *temp,*prev,*cur,*next;
if(*head==NULL){
printf("\nNo linked list present.\n");
return 0;
}else{
temp=*head;
prev=*tail;
cur=*head;
next=(*head)->next;
while(temp->next!=*head){
temp=temp->next;
cur->next=prev;
prev=cur;
cur=next;
next=temp->next;
}
cur->next=prev;
prev=cur;
*head=cur;
}
}
int concatListN(struct node** srcHead,struct node** srcTail,struct node** destHead,struct node** destTail,int N){
int len=0,cnt=1;
struct node *temp=NULL;
if(*srcHead==NULL){
printf("src list empty.\n");
return 0;
}else{
len=length(srcHead);
if(N>len){
printf("Number of elements to concat are greater than src-list's length\n");
exit(1);
}else{
if(*destHead==NULL && *destTail==NULL){
*destHead=*srcHead;
temp=*destHead;
}else{
(*destTail)->next=*srcHead;
temp=(*destTail)->next;
}
while(cnt!=N){
temp=temp->next;
cnt++;
}
temp->next=*destHead;
}
}
}
int concatList(struct node** srcHead,struct node** srcTail,struct node** destHead,struct node** destTail){
if(*srcHead==NULL || *destHead==NULL){
printf("One or both the Lists are empty.\n");
exit(1);
}else{
(*srcTail)->next=*destHead;
(*destTail)->next=*srcHead;
}
return 0;
}
struct node* findTail(struct node** head){
struct node* temp=*head;
if(*head==NULL){
printf("Linked List not present.\n");
return NULL;
}
while(temp->next!=*head){
temp=temp->next;
}
return temp;
}
int searchAllOcc(struct node** head,struct node** tail,int num){
int cnt=0;
struct node *temp=*head;
if(*head==NULL){
printf("No Nodes to display.\n");
exit(1);
}else{
while(temp->next!=*head){
if(temp->data==num){
cnt++;
}
temp=temp->next;
}
if(temp->data==num){
cnt++;
}
if(cnt==0){
printf("%d is not present in linked list.\n",num);
return -1;
}
return cnt;
}
}
int secondLastOcc(struct node** head,struct node** tail,int num){
struct node *temp=*head;
int pos=1,num1=0,secondL=0;
if(*head==NULL){
printf("No Nodes to display.\n");
exit(1);
}else{
while(temp->next!=*head){
if(temp->data==num){
secondL=num1;
num1=pos;
}
temp=temp->next;
pos++;
}
if(temp->data==num){
secondL=num1;
num1=pos;
}
if(*head==*tail || secondL==0){
printf("there is no second last element.\n");
exit(1);
}/*else if(secondL==0){
printf("%d is not present in linked list.\n",num);
return -1;
}*/
return secondL;
}
}
int searchLastOcc(struct node** head,struct node** tail,int num){
struct node *temp=*head;
int pos=1,num1=0;
if(*head==NULL){
printf("No Nodes to display.\n");
exit(1);
}else{
while(temp->next!=*head){
if(temp->data==num){
num1=pos;
}
temp=temp->next;
pos++;
}
if(temp->data==num){
num1=pos;
}
if(num1==0){
printf("%d is not present in linked list.\n",num);
return -1;
}
return num1;
}
}
int searchFirstOcc(struct node** head,struct node** tail,int num){
struct node *temp=*head;
int pos=1;
if(*head==NULL){
printf("No Nodes to display.\n");
exit(1);
}else{
while(temp->next!=*head){
if(temp->data==num){
return pos;
}
temp=temp->next;
pos++;
}
if(temp->data==num){
return pos;
}else{
printf("%d is not present in linked list.\n",num);
return -1;
}
}
}
int deleteAtPos(struct node** head,struct node** tail,int pos){
int len=0,cnt=1;
struct node *temp=*head;
struct node *prevDel=*head;
struct node* delPos=*head;
if(*head==NULL){
printf("Nothing to print.\n");
exit(1);
}else{
len=length(head);
if(pos>len){
printf("Position is greater than length\n");
exit(1);
}else if(pos==len){
deleteLast(head,tail);
}else if(pos==1){
deleteFirst(head,tail);
}else{
while(cnt<pos && temp->next!=*head){
prevDel=temp;
temp=temp->next;
cnt++;
delPos=temp;
}
prevDel->next=delPos->next;
delPos->next=NULL;
free(delPos);
delPos=NULL;
}
return 0;
}
}
int length(struct node **head){
int len=0;
struct node *temp=*head;
if(*head==NULL){
return 0;
}else{
len=1;
while(temp->next!=*head){
temp=temp->next;
len++;
}
return len;
}
}
int deleteLast(struct node** head,struct node** tail){
struct node* delL=NULL;
struct node* sLast=*head;
if(*head==NULL){
printf("Nothing to display.\n");
exit(1);
}else if(*tail==*head){
delL=*head;
*head=NULL;
*tail=NULL;
delL->next=NULL;
free(delL);
delL=NULL;
}else{
while(sLast->next!=*tail){
sLast=sLast->next;
}
delL=*tail;
sLast->next=*head;
*tail=sLast;
delL->next=NULL;
free(delL);
delL=NULL;
}
return 0;
}
int deleteFirst(struct node** head,struct node** tail){
struct node* tempH=*head;
struct node* delF=NULL;
if(*head==NULL){
printf("Nothing to delete.\n");
exit(1);
}else{
delF=*head;
*head=tempH->next;
delF->next=NULL;
(*tail)->next=*head;
free(delF);
delF=NULL;
return 0;
}
}
int insertAtPos(struct node **head,struct node **tail,int no,int pos){
int cnt=1;
struct node *temp=*head;
struct node *newN=NULL;
if(head==NULL){
printf("No Linked list present.\n");
}else{
if(pos==1){
insertFirst(head,tail,no);
return 0;
}
while(temp->next!=*head && cnt<pos-1){
temp=temp->next;
cnt++;
}
newN=newNode(no);
newN->next=temp->next;
temp->next=newN;
}
return 0;
}
int insertFirst(struct node **head,struct node **tail,int no){
struct node *temp=*head;
struct node *newN=NULL;
if(*head==NULL){
newN=newNode(no);
*head=newN;
newN->next=*head;
}else{
newN=newNode(no);
newN->next=*head;
*head=newN;
(*tail)->next= *head;
}
return 0;
}
int createNode(struct node **head){
int itr1,data,num;
printf("How many nodes to be created.\n");
scanf("%d",&num);
for(itr1=0;itr1<num;itr1++){
printf("Enter element no. %d : ",itr1+1);
scanf("%d",&data);
insertLast(head,data);
}
}
int insertLast(struct node **head,int no){
struct node *temp=*head;
struct node *newN=NULL;
if(*head==NULL){
newN=newNode(no);
*head=newN;
newN->next=*head;
}else{
while(temp->next!=*head){
temp=temp->next;
}
newN=newNode(no);
temp->next=newN;
newN->next=*head;
}
return 0;
}
struct node* newNode(int no){
struct node *new=(struct node*)malloc(sizeof(struct node));
if(new==NULL){
printf("Memory allocation failed.\n");
exit(1);
}
new->data=no;
new->next=NULL;
return new;
}
void display(struct node **head){
struct node *temp=*head;
printf("\n\nOUTPUT:\n");
if(*head==NULL){
printf("Nothing to display.\n");
}else{
//printf("\n\nOUTPUT:\n");
while(temp->next!=*head){
printf("|%d|->",temp->data);
temp=temp->next;
}
printf("|%d|\n",temp->data);
}
}