Skip to content

Commit

Permalink
Add subroutine to pop a node when given it's pointer.
Browse files Browse the repository at this point in the history
  • Loading branch information
pedro-ricardo committed May 16, 2019
1 parent 12f39b7 commit 11e1741
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 27 deletions.
68 changes: 42 additions & 26 deletions src/linked_list.f08
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ module Linked_List
procedure:: append => list_append_item
procedure:: destroy => list_finalizer
procedure:: foreach => list_foreach
procedure:: pop => list_pop_node
procedure:: pop_node => list_pop_node_n
procedure:: pop_this => list_pop_node
end type list
!-------------------------------------

Expand Down Expand Up @@ -109,8 +110,8 @@ end subroutine node_finalizer_snowball
! ##############################################################################

! ##############################################################################
! Pop out a node from the list.
pure subroutine list_pop_node( this_list, node_num )
! Pop out a node from the list, by a given number.
pure subroutine list_pop_node_n( this_list, node_num )
implicit none
!Entrada:
class(list), intent(inout) :: this_list
Expand All @@ -125,30 +126,8 @@ pure subroutine list_pop_node( this_list, node_num )
do while ( associated(curr) )
if (cont==node_num) then

if (associated(curr%prev).and.associated(curr%next)) then
!In List middle
curr%next%prev => curr%prev
curr%prev%next => curr%next

else if (associated(curr%prev)) then
!In List tail
nullify(curr%prev%next)
this_list%tail => curr%prev

else if (associated(curr%next)) then
!In List head
nullify(curr%next%prev)
this_list%head => curr%next
end if

!Destroy node content
call curr%destroy()
!Free it's memmory
deallocate(curr)
call this_list%pop_this(curr)

!Remove node from count
this_list%num_nodes = this_list%num_nodes - 1

!Exit when found
return
end if
Expand All @@ -157,6 +136,43 @@ pure subroutine list_pop_node( this_list, node_num )
end do


end subroutine list_pop_node_n
! ##############################################################################

! ##############################################################################
! Pop out a node from the list, by the given node.
pure subroutine list_pop_node(this_list, this_node)
implicit none
!Entrada:
class(list), intent(inout) :: this_list
type(node), pointer :: this_node
!Local:

if (associated(this_node%prev).and.associated(this_node%next)) then
!In List middle
this_node%next%prev => this_node%prev
this_node%prev%next => this_node%next

else if (associated(this_node%prev)) then
!In List tail
nullify(this_node%prev%next)
this_list%tail => this_node%prev

else if (associated(this_node%next)) then
!In List head
nullify(this_node%next%prev)
this_list%head => this_node%next
end if

!Destroy node content
call this_node%destroy()
!Free it's memmory
deallocate(this_node)

!Remove node from count
this_list%num_nodes = this_list%num_nodes - 1


end subroutine list_pop_node
! ##############################################################################

Expand Down
2 changes: 1 addition & 1 deletion src/test_link.f08
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ program test_link
!-------------
! Remove item 4 from list
!-------------
call L%pop(4)
call L%pop_node(4)

write(*,*)'----'

Expand Down

0 comments on commit 11e1741

Please sign in to comment.