I am trying to understand linked lists one step at a time. I can make forward or backward linked lists okay. I can delete the first link in a linked list okay. Now I am attempting to delete a user chosen link in the middle of a linked list.
I assume the strategy is to keep track of three links, the prior link, the link to be deleted and the link following the one to be deleted? And then to do some bookkeeping to tie what remains together? I have attempted to do this but have a dangling pointer error that I cannot detect. I have tried several permutations of pointers in the subroutine shown with no success, clearly there is a conceptual error present that I cannot find. I hope someone can reveal my errors.
SUBROUTINE remove() !Subroutine to remove a user selected link in a linked list
TYPE(link),POINTER::nextp,this,current
integer :: linkrem,k,j
IF(.NOT.ASSOCIATED(start)) RETURN !checks if start is disassociated and thus is empty
call display() !Another subroutine to print the linked list
print '(a,i2,\)','Enter the link to be removed ',
read *,linkrem !User chosen link to remove
nextp=>start !Begin at the start of the list
do k=1,linkrem !Count down to link linkrem, the link to be removed
this=>start !Begin at the start of the same linked list again
if(k>1)then
do j=1,k-1 !This counts down to the prior link
prev_nextp=>this !I am trying to point to the prior link
this=>this%next
end do
end if
current=>nextp !current should point to the link to remove when k=linkrem is reached
nextp=>nextp%next !nextp should point to the link beyond the one that is being removed
end do
DEALLOCATE(current) !Deallocate the present link current
print *,'associated(nextp))= ',associated(nextp) !check that nextp is associated
print *,'associated(prev_nextp))= ',associated(prev_nextp) !check that prev_nextp is associated
prev_nextp=>nextp !Trying to point the prior link to nextp
END SUBROUTINE remove