-
Notifications
You must be signed in to change notification settings - Fork 1
/
list_pointer.f90
74 lines (53 loc) · 1.39 KB
/
list_pointer.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
module list_class
implicit none
type :: node
type(node), pointer :: next
real, pointer :: data
end type node
type :: list
type(node), pointer :: head
contains
procedure :: append
end type list
interface list
module procedure create_list
end interface list
interface node
module procedure create_node
end interface node
contains
type(list) function create_list(head) result(self)
type(node), target, intent(in) :: head
self % head => head
end function create_list
type(node) function create_node(item) result(self)
real, intent(in) :: item
allocate(self % data, source = item)
end function create_node
subroutine append(self, item)
class(list) , intent(inout) :: self
type(node) , intent(inout) :: item
end subroutine append
end module list_class
program test
use list_class
type(list) :: lst
type(node), target :: a, b, c, d
a = node(1.00)
b = node(2.00)
c = node(3.00)
d = node(4.00)
a % next => b
b % next => c
c % next => d
!class(list) , allocatable :: lst
!class(node) , allocatable :: n
!allocate(n, source = node(1))
!allocate(lst, source = list())
!deallocate(lst)
lst = list(a)
print *, lst % head % data
print *, lst % head % next % data
print *, lst % head % next % next % data
print *, lst % head % next % next % next % data
end program test