應用資料結構(一)線性表節點的插入和刪除演算法 (轉)

worldblog發表於2007-08-16
應用資料結構(一)線性表節點的插入和刪除演算法 (轉)[@more@]

線性表節點的插入和刪除演算法

'作者:Snowcloud2002
'插入節點
'名稱: NodeInsert(PAs 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

===============================================
歡迎大家發表看法,有問題的地方大家請指正,謝謝!
===============================================

 


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-962143/,如需轉載,請註明出處,否則將追究法律責任。

相關文章