大头锻炼日记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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值