vb6.0过时了吗?是的,应该是。不过,到现在,它仍然有它的不可替代的用途。起码,.net开发的程序还不是每个电脑都能够运行,起码,现在的vb.net已经不支持ole控件。新的有新的理由,用旧的也有旧的用途。从这个角度看,所有的技术都谈不上过时与不过时。
我现在在做的题库,比较起来就用vb6.0开发要方便得多。只不过原来习惯于C++的STL,突然发现原来VB6.0连内建的链表都没有。没办法,只能自己建造轮子了。现在把我自己写的这个链表与大家分享,或许你也需要,或许,你觉得我的这个设计笨拙得有些要命,那就请你帮我提出修改意见,不胜感谢。
先定义数据节点(定义一个叫ListNode的类),从性能角度考虑,定义出具体的数据类型,此处为String,可改为其他类型:
Code
Option Explicit
Public strValue As String
Public preNode As ListNode
Public nextNode As ListNode
Option Explicit
Public strValue As String
Public preNode As ListNode
Public nextNode As ListNode
再 定义一个双向链表类(StrList)
Code
Option Explicit
Private mvarHead As ListNode
Private curNode As ListNode
Private endNode As ListNode
Dim mvarSize As Integer
Public Function Size() As Integer
Size = mvarSize
End Function
Public Function IsEnd() As Boolean
IsEnd = curNode Is Nothing
End Function
Public Function IsHead() As Boolean
IsHead = curNode Is mvarHead
End Function
Public Sub Unique()
Dim bDelete As Boolean
Dim temNode As ListNode
Dim strValue As String
GoHead
Do While Not IsEnd
strValue = GetValue
Set temNode = curNode
GoNext
Do While Not IsEnd
bDelete = True
If GetValue = strValue Then
If temNode Is endNode Then
Exit Sub
ElseIf curNode Is endNode Then
Set endNode = curNode.preNode
Set endNode.nextNode = Nothing
Set curNode = endNode
Else
Set curNode.preNode.nextNode = curNode.nextNode
Set curNode.nextNode.preNode = curNode.preNode
Set curNode = curNode.nextNode
End If
mvarSize = mvarSize - 1
bDelete = False
End If
If (Not IsEnd) And bDelete Then GoNext
Loop
Set curNode = temNode.nextNode
Loop
End Sub
Public Sub ReMove(strValue As String)
Dim bDelete As Boolean
GoHead
Do While Not IsEnd
bDelete = True
If GetValue = strValue Then
If mvarHead Is endNode Then
Set mvarHead = Nothing
Set endNode = Nothing
Set curNode = Nothing
ElseIf curNode Is mvarHead Then
Set mvarHead = curNode.nextNode
Set mvarHead.preNode = Nothing
Set curNode = mvarHead
ElseIf curNode Is endNode Then
Set endNode = curNode.preNode
Set endNode.nextNode = Nothing
Set curNode = endNode
Else
Set curNode.preNode.nextNode = curNode.nextNode
Set curNode.nextNode.preNode = curNode.preNode
Set curNode = curNode.nextNode
End If
mvarSize = mvarSize - 1
bDelete = False
End If
If (Not IsEnd) And bDelete Then GoNext
Loop
End Sub
Public Sub Delete()
If mvarHead Is endNode Then
Set mvarHead = Nothing
Set endNode = Nothing
Set curNode = Nothing
ElseIf curNode Is mvarHead Then
Set mvarHead = curNode.nextNode
Set mvarHead.preNode = Nothing
Set curNode = mvarHead
ElseIf curNode Is endNode Then
Set endNode = curNode.preNode
Set endNode.nextNode = Nothing
Set curNode = endNode
Else
Set curNode.preNode.nextNode = curNode.nextNode
Set curNode.nextNode.preNode = curNode.preNode
Set curNode = curNode.nextNode
End If
mvarSize = mvarSize - 1
End Sub
Public Sub Clear()
Set mvarHead = Nothing
Set endNode = Nothing
Set curNode = Nothing
mvarSize = 0
End Sub
Public Sub Add(strValue As String)
Dim node As New ListNode
node.strValue = strValue
Set node.preNode = endNode
If mvarHead Is Nothing Then
Set mvarHead = node
Set curNode = node
Else
Set endNode.nextNode = node
End If
Set endNode = node
mvarSize = mvarSize + 1
End Sub
Public Sub GoNext()
Set curNode = curNode.nextNode
End Sub
Public Sub GoAhead()
Set curNode = curNode.preNode
End Sub
Public Function GetValue() As String
GetValue = curNode.strValue
End Function
Public Sub GoHead()
Set curNode = mvarHead
End Sub
Public Sub GoLast()
Set curNode = endNode
End Sub
Option Explicit
Private mvarHead As ListNode
Private curNode As ListNode
Private endNode As ListNode
Dim mvarSize As Integer
Public Function Size() As Integer
Size = mvarSize
End Function
Public Function IsEnd() As Boolean
IsEnd = curNode Is Nothing
End Function
Public Function IsHead() As Boolean
IsHead = curNode Is mvarHead
End Function
Public Sub Unique()
Dim bDelete As Boolean
Dim temNode As ListNode
Dim strValue As String
GoHead
Do While Not IsEnd
strValue = GetValue
Set temNode = curNode
GoNext
Do While Not IsEnd
bDelete = True
If GetValue = strValue Then
If temNode Is endNode Then
Exit Sub
ElseIf curNode Is endNode Then
Set endNode = curNode.preNode
Set endNode.nextNode = Nothing
Set curNode = endNode
Else
Set curNode.preNode.nextNode = curNode.nextNode
Set curNode.nextNode.preNode = curNode.preNode
Set curNode = curNode.nextNode
End If
mvarSize = mvarSize - 1
bDelete = False
End If
If (Not IsEnd) And bDelete Then GoNext
Loop
Set curNode = temNode.nextNode
Loop
End Sub
Public Sub ReMove(strValue As String)
Dim bDelete As Boolean
GoHead
Do While Not IsEnd
bDelete = True
If GetValue = strValue Then
If mvarHead Is endNode Then
Set mvarHead = Nothing
Set endNode = Nothing
Set curNode = Nothing
ElseIf curNode Is mvarHead Then
Set mvarHead = curNode.nextNode
Set mvarHead.preNode = Nothing
Set curNode = mvarHead
ElseIf curNode Is endNode Then
Set endNode = curNode.preNode
Set endNode.nextNode = Nothing
Set curNode = endNode
Else
Set curNode.preNode.nextNode = curNode.nextNode
Set curNode.nextNode.preNode = curNode.preNode
Set curNode = curNode.nextNode
End If
mvarSize = mvarSize - 1
bDelete = False
End If
If (Not IsEnd) And bDelete Then GoNext
Loop
End Sub
Public Sub Delete()
If mvarHead Is endNode Then
Set mvarHead = Nothing
Set endNode = Nothing
Set curNode = Nothing
ElseIf curNode Is mvarHead Then
Set mvarHead = curNode.nextNode
Set mvarHead.preNode = Nothing
Set curNode = mvarHead
ElseIf curNode Is endNode Then
Set endNode = curNode.preNode
Set endNode.nextNode = Nothing
Set curNode = endNode
Else
Set curNode.preNode.nextNode = curNode.nextNode
Set curNode.nextNode.preNode = curNode.preNode
Set curNode = curNode.nextNode
End If
mvarSize = mvarSize - 1
End Sub
Public Sub Clear()
Set mvarHead = Nothing
Set endNode = Nothing
Set curNode = Nothing
mvarSize = 0
End Sub
Public Sub Add(strValue As String)
Dim node As New ListNode
node.strValue = strValue
Set node.preNode = endNode
If mvarHead Is Nothing Then
Set mvarHead = node
Set curNode = node
Else
Set endNode.nextNode = node
End If
Set endNode = node
mvarSize = mvarSize + 1
End Sub
Public Sub GoNext()
Set curNode = curNode.nextNode
End Sub
Public Sub GoAhead()
Set curNode = curNode.preNode
End Sub
Public Function GetValue() As String
GetValue = curNode.strValue
End Function
Public Sub GoHead()
Set curNode = mvarHead
End Sub
Public Sub GoLast()
Set curNode = endNode
End Sub
利用这个双向链表构造一个循环链表(CycList):
Code
Option Explicit
Dim list As New StrList
Public Function Size() As Integer
Size = list.Size
End Function
Public Sub ReMove(strValue As String)
list.ReMove strValue
End Sub
Public Sub Delete()
list.Delete
End Sub
Public Sub Clear()
list.Clear
End Sub
Public Sub Add(strValue As String)
list.Add strValue
End Sub
Public Sub GoNext()
If list.IsEnd Then list.GoHead
list.GoNext
If list.IsEnd Then list.GoHead
End Sub
Public Sub GoAhead()
If list.IsEnd Then list.GoLast
list.GoAhead
If list.IsEnd Then list.GoLast
End Sub
Public Function GetValue() As String
GetValue = list.GetValue
End Function
Public Sub GoHead()
list.GoHead
End Sub
Public Function IsHead() As Boolean
IsHead = list.IsHead
End Function
Option Explicit
Dim list As New StrList
Public Function Size() As Integer
Size = list.Size
End Function
Public Sub ReMove(strValue As String)
list.ReMove strValue
End Sub
Public Sub Delete()
list.Delete
End Sub
Public Sub Clear()
list.Clear
End Sub
Public Sub Add(strValue As String)
list.Add strValue
End Sub
Public Sub GoNext()
If list.IsEnd Then list.GoHead
list.GoNext
If list.IsEnd Then list.GoHead
End Sub
Public Sub GoAhead()
If list.IsEnd Then list.GoLast
list.GoAhead
If list.IsEnd Then list.GoLast
End Sub
Public Function GetValue() As String
GetValue = list.GetValue
End Function
Public Sub GoHead()
list.GoHead
End Sub
Public Function IsHead() As Boolean
IsHead = list.IsHead
End Function
使用实例代码:
Code
Dim list As New StrList
list.Clear
list.Add "a"
list.Add "b"
list.GoHead
Do While Not list.IsEnd
'do something
'list.GetValue
list.GoNext
Dim list As New StrList
list.Clear
list.Add "a"
list.Add "b"
list.GoHead
Do While Not list.IsEnd
'do something
'list.GetValue
list.GoNext