Singly-linked list/Element removal

From Rosetta Code
Revision as of 06:29, 27 October 2016 by rosettacode>Kwhitefoot (Example created for HN thread https://news.ycombinator.com/item?id=12793624#12800091)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Visual Basic .NET

The contract requirement for these functions is:

- that the entry to be removed is not Nothing and that it is present in the list.

The contract ensures:

- The entry has been removed.

<lang vbnet>

   Module Module1
     Public Class ListEntry
       Public value As String
       Public [next] As ListEntry
     End Class
     Public Head As ListEntry
      <summary>
      Straight translation of Torvalds' tasteless version.
      </summary>
      <param name="entry"></param>
     Sub RemoveListEntry(entry As ListEntry)
       Dim prev As ListEntry = Nothing
       Dim walk = Head
       ' Walk the list
       While walk IsNot entry
         prev = walk
         walk = walk.next
       End While
       ' Remove the entry by updating the head or the previous entry.
       If prev Is Nothing Then
         Head = entry.next
       Else
         prev.next = entry.next
       End If
     End Sub
      <summary>
      Straight translation of Torvalds' tasteful version.
      </summary>
      <param name="entry"></param>
     Sub RemoveListEntry1(entry As ListEntry)
       Dim indirect = New ListEntry
       indirect.next = Head
       ' Walk the list looking for the thing that points at the thing that we
       ' want to remove.
       While indirect.next IsNot entry
         indirect = indirect.next
       End While
       ' ... and just remove it.
       indirect.next = entry.next
     End Sub

End Module

</lang>