< %
' 链表节点类
Class Node
Public pNext
Public pPrev
Public data
Private Sub Class_Initialize()
Set pNext = Nothing
Set pPrev = Nothing
Set data = Nothing
End Sub
Private Sub Class_Terminate()
Set pNext = Nothing
Set pPrev = Nothing
Set data = Nothing
End Sub
End Class
' 双向链表类
Class BidList
Private pHead
Private pTail
Private iCount
Private Sub Class_Initialize()
Set pHead = Nothing
Set pTail = Nothing
iCount = 0
End Sub
Private Sub Class_Terminate()
Set pHead = Nothing
Set pTail = Nothing
End Sub
Public Function GetHead()
Set GetHead = pHead
End Function
Public Function GetTail()
Set GetTail = pTail
End Function
' -------------------------------------------
' 功能:向链表尾部添加节点
' 参数:pNode,添加的节点
' 返回: 无
' -------------------------------------------
Public Sub AddNodeToTail(ByRef pNode)
If pTail Is Nothing Then
Set pTail = pNode
Set pHead = pNode
Else
Set pTail.pNext = pNode
Set pNode.pPrev = pTail
Set pTail = pNode
End If
iCount = iCount + 1
End Sub
' -------------------------------------------
' 功能:向链表头部添加节点
' 参数:pNode,添加的节点
' 返回: 无
' -------------------------------------------
Public Sub AddNodeToHead(ByRef pNode)
If pHead Is Nothing Then
Set pTail = pNode
Set pHead = pNode
Else
Set pHead.pPrev = pNode
Set pNode.pNext = pHead
Set pHead = pNode
End If
iCount = iCount + 1
End Sub
' -------------------------------------------
' 功能:向链表尾部添加节点,节点本身由该函数负责创建
' 参数:pData,添加的节点数据域
' 返回: 无
' -------------------------------------------
Public Sub AddDataToTail(ByRef pData)
Set pNode = New Node
Set pNode.data = pData
AddNodeToTail(pNode)
End Sub
' -------------------------------------------
' 功能:向链表头部添加节点,节点本身由该函数负责创建
' 参数:pNode,添加的节点
' 返回: 无
' -------------------------------------------
Public Sub AddDataToHead(ByRef pData)
Set pNode = New Node
Set pNode.data = pData
AddNodeToHead(pNode)
End Sub
' -------------------------------------------
' 功能:删除指定的节点,资源由本函数释放
' 参数:pNode,要删除的节点
' 返回: 无
' -------------------------------------------
Public Sub RemoveNode(ByRef pNode)
If pNode Is Nothing Then Exit Sub End If
If pHead Is Nothing Then Exit Sub End If
' 如果删除的节点是头节点
If pNode.pPrev Is Nothing Then
Set pHead = pNode.pNext
Else
Set pNode.pPrev.pNext = pNode.pNext
End If
' 如果删除的节点是尾节点
If pNode.pNext Is Nothing Then
Set pTail = pNode.pNext
Else
Set pNode.pNext.pPrev = pNode.pPrev
End If
iCount = iCount - 1 ' 链表计数减一
Set pNode.data = Nothing ' 释放节点数据域
Set pNode = Nothing ' 释放节点本身
End Sub
' -------------------------------------------
' 功能:删除头节点
' 参数:无
' 返回: 无
' -------------------------------------------
Public Sub RemoveHead()
RemoveNode(pHead)
End Sub
' -------------------------------------------
' 功能:删除尾节点
' 参数:无
' 返回: 无
' -------------------------------------------
Public Sub RemoveTail()
RemoveNode(pTail)
End Sub
' -------------------------------------------
' 功能:获取链表节点个数
' 参数:无
' 返回: 链表中节点的个数
' -------------------------------------------
Public Function GetCount()
GetCount = iCount
End Function
' -------------------------------------------
' 功能:删除链表中所有节点,但不释放其节点资源
' 参数:无
' 返回: 链表中节点的个数
' -------------------------------------------
Public Sub RemoveAll()
Set pHead = Nothing
Set pTail = Nothing
iCount = 0
End Sub
' -------------------------------------------
' 功能:将另一个链表合并到当前链表尾
' 参数:无
' 返回: 链表中节点的个数
' -------------------------------------------
Public Sub AppendToTail(ByRef pList)
If pList Is Nothing Then Exit Sub End If
If pList.GetCount() = 0 Then Exit Sub End If
If pTail Is Nothing Then
Set pHead = pList.GetHead()
Set pTail = pList.GetTail()
Else
Set pTail.pNext = pList.GetHead()
Set pList.GetHead().pPrev = pTail
Set pTail = pList.GetTail()
End If
iCount = iCount + pList.GetCount()
End Sub
' -------------------------------------------
' 功能:根据索引取链表的数据对象
' 参数:无
' 返回: 符合要求的节点数据域
' -------------------------------------------
Public Function GetDataAt(iIndex)
Set curNode = pHead
Set GetDataAt = Nothing
index = 0
For index = 0 To iCount - 1
If index = iIndex Then
Set GetDataAt = curNode.data
Exit For
End If
Set curNode = curNode.pNext
Next
End Function
' -------------------------------------------
' 功能:根据索引取链表的节点对象
' 参数:无
' 返回: 符合要求的节点
' -------------------------------------------
Public Function GetNodeAt(iIndex)
Set curNode = pHead
Set GetNodeAt = Nothing
index = 0
For index = 0 To iCount - 1
If index = iIndex Then
Set GetNodeAt = curNode
Exit Function
End If
Set curNode = curNode.pNext
Next
End Function
' -------------------------------------------
' 功能:设置链表的指定位置节点的数据域
' 自动添加节点
' 参数:无
' 返回:
' -------------------------------------------
Public Sub SetDataAt(iIndex, ByRef objData)
If iCount > iIndex Then
Set GetNodeAt(iIndex).data = objData
Exit Sub
End If
For i = iCount To iIndex
Set pNode = New Node
AddNodeToTail(pNode)
Next
Set pTail.data = objData
End Sub
End Class
% >