Singly-linked list/Element removal: Difference between revisions

From Rosetta Code
Content added Content deleted
m (→‎{{header|Visual Basic .NET}}: renamed functions.)
m (→‎{{header|Visual Basic .NET}}: Clarified contract.)
Line 3: Line 3:
The contract requirement for these functions is:
The contract requirement for these functions is:


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


The contract ensures:
The contract ensures:

Revision as of 10:04, 27 October 2016

Visual Basic .NET

The contract requirement for these functions is:

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

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 RemoveListEntryTasteless(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 RemoveListEntryTastefull(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>