2011-02-27 158 views
2

即时尝试在vb.net中创建双散列hashtable,并且我收到了一些我不知道如何解决的错误。希望你们能帮助我。我遇到的问题有任何我有dbnull.value或mod =我在编辑器中得到错误,我不知道如何解决它们。把这段代码放在vb中,看看我的意思。 这里是代码:vb.net中的双散列表或双散列hashtable

Public Class DoubleHashing 

Class DataItem 
    Private data As Integer 

    Public Sub New(ByVal i As Integer) 
     data = i 
    End Sub 
    Public Function getKey() As Integer 
     Return data 
    End Function 

End Class 

Private hashArray() As DataItem 
Private arraySize As Integer 
Private bufItem As DataItem 

Public Sub New(ByVal size As Integer) 

    arraySize = size 
    hashArray(arraySize) = New DataItem(arraySize) 
    Dim bufItem(-1) As DataItem 
End Sub 

Public Function hashFunc1(ByVal key As Integer) As Integer 
    Return key Mod arraySize 
End Function 

Public Function hashFunc2(ByVal key As Integer) As Integer 
    Return 6 - key Mod 6 
End Function 


Public Sub insert(ByVal key As Integer, ByVal item As DataItem) 
    Dim hashVal As Integer = hashFunc1(key) ' hash the key 
    Dim stepSize As Integer = hashFunc2(key) ' get step size 

    ' until empty cell or -1 
    While hashArray(hashVal) <> DBNull.Value & hashArray(hashVal).getKey() <> -1 
     hashVal += stepSize ' add the step 
     hashVal mod= arraySize ' for wraparound 
    End While 
    hashArray(hashVal) = item ' insert item 

End Sub 

Public Function delete(ByVal key As Integer) As DataItem 
    Dim hashVal As Integer = hashFunc1(key) 
    Dim stepSize As Integer = hashFunc2(key) ' get step size 

    While hashArray(hashVal) <> DBNull.Value 
     If hashArray(hashVal).getKey() = key Then 
      Dim temp As DataItem = hashArray(hashVal) ' save item 
      hashArray(hashVal) = bufItem ' delete item 
      Return temp ' return item 
     End If 

     hashVal += stepSize ' add the step 
     hashVal mod= arraySize ' for wraparound 
    End While 

    Return DBNull.Value 
End Function 

Public Function find(ByVal key As Integer) As DataItem 
    Dim hashVal As Integer = hashFunc1(key) 
    Dim stepSize As Integer = hashFunc2(key) 

    While hashArray(hashVal) <> DBNull.Value 
     If hashArray(hashVal).getKey() = key Then 
      Return hashArray(hashVal) 
     End If 

     hashVal += stepSize 
     hashVal mod= arraySize 
    End While 

    Return DBNull.Value 
End Function 

末级

+0

那么你的问题是什么? – Gabe 2011-02-27 04:48:04

+0

我得到语法错误与dbnull.value和线这样的hashVal mod = arraySize和我不知道在mod =东西的vb – bob 2011-02-27 04:51:56

+0

nvm null的任何其他值。 – bob 2011-02-27 04:55:30

回答

0

通常第二哈希函数必须永远不会返回0。

我不认为DBNull.Value从DataItem的继承。另外,你的数组被初始化为一个包含Nothing引用的大小的数组。

+0

不能回答我的问题 – bob 2011-02-27 04:10:02

+0

您可以尝试详细说明您的问题。这足以导致此失效,但可能还有更多。 – Joshua 2011-02-27 04:15:27

+0

把它放到VB中,看到错误即时解答难以解释 – bob 2011-02-27 04:22:46