-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmylinkedlist_delNth.f90
115 lines (101 loc) · 2.43 KB
/
mylinkedlist_delNth.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
program ketoprak
use salak
implicit none
type(node), pointer::ll
nullify(head)
call insertn(9,1)
call insertn(15,2)
call insertn(3,3)
call insertn(17,2)
call printn()
print *,
call deln(5)
call printn()
!call sizen()
stop
contains
subroutine deln(nth)
implicit none
integer idat, nth, i
type(node), pointer::tmp,tmp2
if(nth < 0) then; print *, 'nth must > 0'; stop
endif
tmp => head
if(.not.associated(head)) then
print *, 'linkedList empty'
elseif(nth == 1) then
head => tmp%next
else
if(.not.associated(tmp%next)) then
print *, 'nth > sizeof-linkedList'; stop
end if
do i = 1,nth-2
tmp => tmp%next
if(.not.associated(tmp%next)) then
print *, 'nth > sizeof-linkedList'; stop
end if
end do
tmp2 => tmp; tmp => tmp%next
tmp2%next => tmp%next
end if
nullify(tmp%next)
deallocate(tmp)
return
end subroutine deln
subroutine insertn(idat,nth)
implicit none
integer idat, nth, i
type(node), pointer::tmp,tmp2
if(nth < 0) then; print *, 'nth must > 0'; stop; endif
allocate(tmp); tmp%data = idat; nullify(tmp%next)
if(.not.associated(head) .and. nth > 1) then
print *, 'wrong insertion number, nth', nth, '> sizeof-linkedList'
elseif(nth == 1) then
tmp%next => head; head => tmp
else
tmp2 => head
do i = 1,nth-2
! print *, 'here', .not.associated(tmp2%next)
tmp2 => tmp2%next
if(.not.associated(tmp2)) then
print *, 'nth > sizeof-linkedList'; stop
end if
end do
tmp%next => tmp2%next; tmp2%next => tmp
end if
return
end subroutine insertn
subroutine printn()
implicit none
type(node), pointer::tmp
tmp => head
if(.not.associated(tmp)) then
print *, 'no linked-list'
return
else
do while(associated(tmp))
print *, tmp%data
tmp => tmp%next
end do
end if
return
end subroutine printn
subroutine sizen()
implicit none
integer n
type(node), pointer::tmp
tmp => head
if(.not.associated(tmp)) then
print *, 'no linked-list'
return
else
n = 0
do while(associated(tmp))
n = n+1
tmp => tmp%next
end do
end if
print *, n
return
end subroutine sizen
end program ketoprak