1. 程式人生 > >大頭鍛鍊日記1 -- vbscript實現典型線性表

大頭鍛鍊日記1 -- vbscript實現典型線性表

      網上有人說新時代的男性要兩個頭都要抓,兩個頭都要硬。好吧,不擼了。堅持去游泳,順便記錄每天鍛鍊大頭的點滴。
複習一下資料結構,OO思想。兩年沒有寫過指令碼了,拋開集合和字典物件,用vbscript偽面向物件實現一下線性表。

線性表類主體:

Option Explicit '-------------------------------------- '        by.Shine '-------------------------------------- Class ListMode  '     Private Data(10)  '
10
    Private Length  '     Public Sub Class_Initialize     Length=0     End Sub     Public Sub AddElem(elem)   '         If Length = 10 Then     MsgBox "list full,cannot add "&elem     Exit Sub     Else     Data(Length)=elem     Length=Length+1     End If     End Sub          Public
 Function IndexGetElem(index)   '     If Length = 0 Or index < 1 Or index > Length Then     MsgBox "List is empty or index is not correct"     Exit Function     Else     IndexGetElem=Data(index-1)     End If     End Function     Public Function ElemGetIndex(elem)    '
    If Length = 0 Then     MsgBox "List is empty"     Exit Function     End If     Dim k     For k=1 To Length Step 1     If Data(k-1) = elem Then     ElemGetIndex=k     Exit For     End If     Next                                     End Function     Public Sub Insert(elem,index)  '     If Length = 10 Then     MsgBox "list full,cannot insert "&elem     Exit Sub     End If     If index < 1 Or index > Length+1 Then     MsgBox "Index is not correct"     Exit Sub     End If           If index <= Length Then     Dim k     For k=Length-1 To index-1 Step 1      Data(k+1)=Data(k)     Next     End If     Data(index-1)=elem     Length=Length+1     End Sub     Public Function IndexDel(index)    '()     If Length = 0 Or index < 1 Or index > Length Then     MsgBox "List is empty or index is not correct"     Exit Function     End If     IndexDel=Data(index-1)     If index < Length Then     Dim k     For k=index To Length-1 Step 1     Data(k-1)=Data(k)     Next     End If     Length=Length-1     End Function     Public Sub ElemDel(elem)    '()     If Length = 0 Then     MsgBox "List is empty"     Exit Sub     End If       Dim k     For k=1 To Length Step 1     If Data(k-1) = elem Then     IndexDel k     Exit For     End If     Next     End Sub     Public Function getLength   '         getLength=Length     End Function     Public Sub Clear   '         Length=0     End Sub End Class

以下為例項物件測試程式碼:

'-------------------------------  '       調 '-------------------------------  Dim list,i,temp                             ' Set list=New ListMode                       ' MsgBox list.getLength                   ' list.IndexGetElem(0)                    ' list.IndexGetElem(1) list.IndexGetElem(4) Call list.Clear                         ' For i=1 To 10 Step 1 list.AddElem "elem"&i               '滿 Next list.AddElem "a"                        ' MsgBox list.getLength                   ' temp="" For i=1 To list.getLength Step 1 temp=temp&list.IndexGetElem(i)&" "          ' Next MsgBox temp list.ElemDel("elem2")                   ' temp="" For i=1 To list.getLength Step 1 temp=temp&list.IndexGetElem(i)&" "          ' Next     MsgBox temp list.IndexDel(5)                        '5 temp="" For i=1 To list.getLength Step 1 temp=temp&list.IndexGetElem(i)&" "          ' Next     MsgBox temp MsgBox list.ElemGetIndex("elem4")       ' MsgBox list.IndexGetElem(3)             ' Call list.Clear     ' MsgBox list.getLength temp="" For i=1 To list.getLength Step 1 temp=temp&list.IndexGetElem(i)&" "          ' Next