线性表节点的插入和删除算法

 

'原作者:Snowcloud2002
'插入节点
'函数名称: NodeInsert(Pos As Integer, Num As Integer, TableName As Variant) As Boolean
'参数:      Pos As Integer要插入节点的位置,Num As Integer要插入的数值,TableName As Variant线性表名称
'返回值:    Boolean

Function NodeInsert(Pos As Integer, Num As Integer, TableName As Variant

    '改变源线性表长度:插入结点后线性表长度加一
    ReDim Preserve TableName(UBound(TableName) + 1)
    '对线性表中第i个元素到最后一个元素的位置后移一位
    For i = (UBound(TableName)) To (Pos + 1) Step -1
        If i < 1 Or i > (UBound(TableName)) Then
        Insert = False
        Exit Function
        Else
        TableName(i) = TableName(i - 1)
        End If
    Next
    '将要插入的元素放到第i个位置
    TableName(i) = Num
    Insert = True
End Function

'删除节点
'函数名称:  NodeDelete(Pos As Integer, TableName As Variant) As Boolean
'参数:      Pos As Integer要删除节点的位置,TableName as Variant线性表名称

'返回值:    Boolean
Function NodeDelete(Pos As Integer, TableName As Variant) As Boolean
    '对线性表中第i个元素到最后一个元素的位置前移一位
    For i = Pos To (UBound(TableName))
        If i < 1 Or i > (UBound(TableName)) Then
        Delete = False
        Exit Function
        Else
        TableName(i - 1) = TableName(i)
        End If
    Next
    '改变源线性表长度:删除结点后线性表长度减一
    ReDim Preserve TableName(UBound(TableName) - 1)
    Delete = True
End Function

原文地址:https://www.cnblogs.com/topboy168/p/460388.html