-
Notifications
You must be signed in to change notification settings - Fork 1
/
improved_linked_list.f90
496 lines (388 loc) · 11.9 KB
/
improved_linked_list.f90
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
MODULE improved_linked_list
USE linked_list, ONLY: node
USE generic_swap
IMPLICIT NONE
PRIVATE
PUBLIC :: linked_list_t
PUBLIC :: print_list
PUBLIC :: create_head, append_tail, append_head, append_after_node
PUBLIC :: remove_node
PUBLIC :: selection_sort, selection_sort_2, radix_sort
PUBLIC :: quick_sort, merge_sort ! defect in those two algorithms
PUBLIC :: interchange_sort
TYPE :: linked_list_t
TYPE (node), POINTER :: head => null()
TYPE (node), POINTER :: tail => null()
END TYPE linked_list_t
INTERFACE create_head
MODULE PROCEDURE allocate_head_and_assign_value
END INTERFACE
INTERFACE append_tail
MODULE PROCEDURE append_node_at_tail
END INTERFACE
INTERFACE append_head
MODULE PROCEDURE append_node_at_head
END INTERFACE
INTERFACE append_after_node
MODULE PROCEDURE append_new_node_after_node
END INTERFACE
INTERFACE remove_node
MODULE PROCEDURE remove_node_has_key
END INTERFACE
INTERFACE selection_sort
MODULE PROCEDURE list_selection_sort
END INTERFACE
INTERFACE interchange_sort
MODULE PROCEDURE list_interchange_sort
END INTERFACE
INTERFACE selection_sort_2
MODULE PROCEDURE list_selection_sort_2
END INTERFACE
INTERFACE quick_sort
MODULE PROCEDURE list_quick_sort
END INTERFACE
INTERFACE radix_sort
MODULE PROCEDURE list_radix_sort
END INTERFACE
INTERFACE merge_sort
MODULE PROCEDURE list_merge_sort
END INTERFACE
INTERFACE print_list
MODULE PROCEDURE tranverse_and_print
END INTERFACE
CONTAINS
SUBROUTINE allocate_head_and_assign_value(list, num)
IMPLICIT NONE
INTEGER, INTENT(in) :: num
INTEGER :: status
TYPE (linked_list_t) :: list
ALLOCATE(list%head, STAT = status) ! create the head of the list
IF (status > 0) STOP 'Fail to allocate a new node'
list%head%value = num ! give the value
NULLIFY(list%head%next) ! point to null
list%tail => list%head ! update tail of list
END SUBROUTINE
SUBROUTINE append_node_at_tail(list, num)
IMPLICIT NONE
INTEGER, INTENT(in) :: num
INTEGER :: status
TYPE (node), POINTER :: current
TYPE (linked_list_t) :: list
ALLOCATE(current, STAT = status) ! create new node
IF (status > 0) STOP 'Fail to allocate a new node'
current%value = num ! giving the value
NULLIFY(current%next) ! point to null (end of list)
list%tail%next => current ! link to tail of list
list%tail => current ! update tail of list
END SUBROUTINE
SUBROUTINE append_node_at_head(list, num)
IMPLICIT NONE
INTEGER, INTENT(in) :: num
INTEGER :: status
TYPE (node), POINTER :: current
TYPE (linked_list_t) :: list
ALLOCATE(current, STAT = status) ! create new node
IF (status > 0) STOP 'Fail to allocate a new node'
current%value = num ! giving the value
NULLIFY(current%next) ! point to null (end of list)
current%next => list%head ! link to head of list
list%head => current ! update head of list
END SUBROUTINE
SUBROUTINE append_new_node_after_node(list, num, value)
IMPLICIT NONE
INTEGER, INTENT(in) :: num, value
INTEGER :: status
TYPE (node), POINTER :: p
TYPE (node), POINTER :: q
TYPE (node), POINTER :: current
TYPE (linked_list_t) :: list
p => list%head
DO WHILE (ASSOCIATED(p))
q => p
IF (q%value == num) THEN
ALLOCATE(current, STAT = status) ! create new node
IF (status > 0) STOP 'Fail to allocate a new node'
current%value = value ! giving the value
current%next => q%next
q%next => current
IF (ASSOCIATED(q, list%tail)) list%tail => current
ENDIF
p => p%next
ENDDO
END SUBROUTINE
SUBROUTINE remove_node_has_key(list, num)
IMPLICIT NONE
INTEGER, INTENT(in) :: num
TYPE (node), POINTER :: p, q
TYPE (linked_list_t) :: list
p => list%head
q => null()
DO WHILE (ASSOCIATED(p))
IF (p%value == num) EXIT
q => p
p => p%next
ENDDO
IF (.NOT. ASSOCIATED(p)) THEN
PRINT*, 'Cannot find node to remove'
RETURN
ENDIF
IF (ASSOCIATED(q)) THEN
IF (ASSOCIATED(p, list%tail)) THEN
list%tail => q
ENDIF
q%next => p%next
NULLIFY(p)
ELSE
list%head => p%next
IF (.NOT. ASSOCIATED(list%head)) NULLIFY(list%tail)
ENDIF
END SUBROUTINE
SUBROUTINE list_radix_sort(l)
IMPLICIT NONE
TYPE (linked_list_t) :: l, B(0:9)
TYPE (node), POINTER :: p
INTEGER :: i, j, k
IF (ASSOCIATED(l%head, l%tail)) RETURN
DO k = 0, 9
DO WHILE (ASSOCIATED(l%head))
p => l%head
l%head => p%next
NULLIFY(p%next)
i = get_digit(p%value, k)
CALL add_tail(B(i), p)
ENDDO
ENDDO
l= B(0)
DO j = 1, 9
CALL append_list(l, B(j))
ENDDO
END SUBROUTINE
SUBROUTINE append_list(l, l1)
IMPLICIT NONE
TYPE (linked_list_t) :: l, l1
IF (ASSOCIATED(l%head) .AND. ASSOCIATED(l1%head)) THEN
l%tail%next => l1%head
l%tail => l1%tail
ELSE IF (ASSOCIATED(l%head) .AND. .NOT. ASSOCIATED(l1%head)) THEN
RETURN
ELSE
l = l1
ENDIF
END SUBROUTINE
INTEGER FUNCTION get_digit(n, k)
IMPLICIT NONE
INTEGER :: n, k
IF (k == 0) get_digit = MOD(n, 10)
IF (k == 1) get_digit = MOD(n/10, 10)
IF (k == 2) get_digit = MOD(n/100, 10)
IF (k == 3) get_digit = MOD(n/1000, 10)
IF (k == 4) get_digit = MOD(n/10000, 10)
IF (k == 5) get_digit = MOD(n/100000, 10)
IF (k == 6) get_digit = MOD(n/1000000, 10)
IF (k == 7) get_digit = MOD(n/10000000, 10)
IF (k == 8) get_digit = MOD(n/100000000, 10)
IF (k == 9) get_digit = MOD(n/1000000000, 10)
END FUNCTION get_digit
RECURSIVE SUBROUTINE list_quick_sort(l)
IMPLICIT NONE
TYPE (linked_list_t) :: l, l1, l2
TYPE (node), POINTER :: p, q
IF (ASSOCIATED(l%head, l%tail)) RETURN
IF (.NOT. ASSOCIATED(l%head)) RETURN
NULLIFY(l1%head)
NULLIFY(l2%head)
NULLIFY(l1%tail)
NULLIFY(l2%tail)
q => l%head
l%head => q%next
DO WHILE (ASSOCIATED(l%head))
p => l%head
l%head => p%next
p%next => null()
IF (p%value <= q%value) THEN
CALL add_tail(l1, p)
ELSE
CALL add_tail(l2, p)
ENDIF
ENDDO
CALL list_quick_sort(l1)
CALL list_quick_sort(l2)
IF (ASSOCIATED(l1%head) .AND. .NOT. ASSOCIATED(l1%tail)) THEN
CALL restore_tail(l1)
ENDIF
IF (ASSOCIATED(l1%head) .AND. ASSOCIATED(l1%tail)) THEN
l%head => l1%head
l1%tail%next => q
ELSE
l%head => q
ENDIF
q%next => l2%head
l2%head => q
IF (ASSOCIATED(l2%head)) THEN
l%tail => l2%tail
ELSE
l%tail => q
ENDIF
END SUBROUTINE
RECURSIVE SUBROUTINE list_merge_sort(l)
IMPLICIT NONE
TYPE (linked_list_t) :: l, l1, l2
IF (.NOT. ASSOCIATED(l%head)) RETURN
IF (ASSOCIATED(l%head, l%tail)) RETURN
CALL distribute_list(l, l1, l2)
CALL list_merge_sort(l1)
CALL list_merge_sort(l2)
CALL merge_list(l, l1, l2)
END SUBROUTINE
SUBROUTINE merge_list(l,l1,l2)
IMPLICIT NONE
TYPE (linked_list_t) :: l, l1, l2
TYPE (node), POINTER :: p
DO WHILE (ASSOCIATED(l1%head) .AND. ASSOCIATED(l2%head))
IF (l1%head%value <= l2%head%value) THEN
p => l1%head
l1%head => p%next
ELSE
p => l2%head
l2%head => p%next
ENDIF
NULLIFY(p%next)
CALL add_tail(l, p)
ENDDO
IF (ASSOCIATED(l1%head)) THEN
l%tail%next => l1%head
l%tail => l1%tail
ELSEIF (ASSOCIATED(l2%head)) THEN
l%tail%next => l2%head
l%tail => l2%tail
ENDIF
END SUBROUTINE
RECURSIVE SUBROUTINE distribute_list(l, l1, l2)
IMPLICIT NONE
TYPE (linked_list_t) :: l, l1, l2
TYPE (node), POINTER :: p
p => l%head
DO WHILE (ASSOCIATED(l%head) .AND. p%value <= l%head%value)
p => l%head
l%head => p%next
p%next => null()
CALL add_tail(l1, p)
ENDDO
IF (ASSOCIATED(l%head)) THEN
CALL distribute_list(l, l2, l1)
ELSE
NULLIFY(l%tail)
ENDIF
END SUBROUTINE
SUBROUTINE list_interchange_sort(list)
! Interchange sort algorithm on linked list by manipulating data field
IMPLICIT NONE
TYPE (linked_list_t) :: list
TYPE (node), POINTER :: p, q
p => list%head
DO WHILE (ASSOCIATED(p))
q => p%next
DO WHILE (ASSOCIATED(q))
IF (q%value < p%value) THEN
CALL swap(q%value, p%value)
ENDIF
q => q%next
ENDDO
p => p%next
ENDDO
END SUBROUTINE
SUBROUTINE list_selection_sort(list)
! Selection sort algorithm on linked list by manipulating data field
IMPLICIT NONE
TYPE (linked_list_t) :: list
TYPE (node), POINTER :: p, q, min
p => list%head
DO WHILE (ASSOCIATED(p))
q => p%next
min => p
DO WHILE (ASSOCIATED(q))
IF (q%value < min%value) THEN
min => q
ENDIF
q => q%next
ENDDO
CALL swap(min%value, p%value)
p => p%next
ENDDO
END SUBROUTINE
SUBROUTINE list_selection_sort_2(list)
IMPLICIT NONE
! Selection sort algorithm on linked list by manipulating pointer 'next'
TYPE (linked_list_t) :: list
TYPE (linked_list_t) :: list_res
TYPE (node), POINTER :: min, min_prev, p, q
DO WHILE (ASSOCIATED(list%head))
p => list%head
q => p%next
min => p
min_prev => null()
DO WHILE (ASSOCIATED(q))
IF (q%value < min%value) THEN
min => q
min_prev => p
ENDIF
p => q
q => q%next
ENDDO
IF (ASSOCIATED(min_prev)) THEN
min_prev%next => min%next
ELSE
list%head => min%next
ENDIF
! Append min to tail of list_res
CALL add_tail(list_res, min)
ENDDO
! Assign list = list_res
list%head => list_res%head
list%tail => list_res%tail
END SUBROUTINE
SUBROUTINE add_tail(l, new_node)
IMPLICIT NONE
TYPE (linked_list_t) :: l
TYPE (node), POINTER :: new_node
IF (.NOT. ASSOCIATED(l%head)) THEN
l%head => new_node
l%tail => l%head
ELSE
l%tail%next => new_node
l%tail => new_node
ENDIF
END SUBROUTINE
SUBROUTINE restore_tail(list)
! To deal with some linked lists which loose their tail during compatation
IMPLICIT NONE
TYPE (linked_list_t) :: list
TYPE (node), POINTER :: current
IF (ASSOCIATED(list%tail)) RETURN
current => list%head ! make current as alias of list
DO
IF (.NOT. ASSOCIATED(current%next)) THEN
list%tail => current
EXIT ! exit if null pointer
ENDIF
current => current%next ! make current alias of next node
END DO
END SUBROUTINE
SUBROUTINE tranverse_and_print(list)
IMPLICIT NONE
TYPE (linked_list_t) :: list
TYPE (node), POINTER :: current
current => list%head ! make current as alias of list
WRITE(*, "(A)", ADVANCE = "NO") "["
DO
IF (.NOT. ASSOCIATED(current)) EXIT ! exit if null pointer
IF (.NOT. ASSOCIATED(current%next)) THEN
WRITE(*, "(1x, i0, a)", ADVANCE = "NO") current%value, "]"
ELSE
WRITE(*, "(1x, i0, a)", ADVANCE = "NO") current%value, ","
ENDIF
current => current%next ! make current alias of next node
END DO
WRITE(*, *)
END SUBROUTINE
END MODULE