Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Class HashTable
- Public table() As Integer?
- Private size As Integer
- Sub New(maxIndex As Integer)
- size = maxIndex + 1
- ReDim table(maxIndex)
- End Sub
- Sub print()
- For i = 0 To size - 1
- Console.WriteLine(i & " : " & table(i))
- Next
- End Sub
- Function insert(key As Integer) As Integer
- 'returns 1 for success, 0 for failure
- Dim address As Integer = hash(key)
- Dim initialAddress As Integer = address
- While Not table(address) Is Nothing
- address += 1
- If address >= size Then
- address = 0
- End If
- If address = initialAddress Then
- Console.WriteLine("ERROR - Hash table is full!")
- Return 0
- End If
- End While
- table(address) = key
- Return 1
- End Function
- Function searchIndex(key As Integer) As Integer
- 'returns -1 if not found
- Dim address As Integer = hash(key)
- Dim initialAddress As Integer = address
- While table(address) Is Nothing Or table(address) <> key
- address += 1
- If address >= size Then
- address = 0
- End If
- If address = initialAddress Then
- Return -1
- End If
- End While
- Return address
- Return 1
- End Function
- Function hash(key) As Integer
- Return key Mod size
- End Function
- End Class
- Sub Main()
- 'demo
- Dim myHashTable As New HashTable(9)
- myHashTable.insert(8)
- myHashTable.insert(14)
- myHashTable.insert(65)
- myHashTable.insert(5)
- myHashTable.insert(53)
- myHashTable.insert(23)
- myHashTable.insert(9)
- myHashTable.insert(42)
- myHashTable.insert(91)
- myHashTable.insert(43)
- myHashTable.print()
- Console.WriteLine()
- Console.WriteLine(myHashTable.searchIndex(53))
- Console.ReadLine()
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement