大头锻炼日记5 -- vbscript中的栈Stack(链栈)


用vbscript的链栈实现LIFO的方法。

节点和栈类主体如下:
Option Explicit
' ----------------------------------------- 
'                by.Shine
' -----------------------------------------
Class StackNode          '
     Private Data         '
     Private NextP        '
     Public Property  Let  letData(da)      ' set
         Data =da
     End Property 
     Public Property  Get getData          ' get
         getData =Data
     End Property 
     Public Property  Set  setNextP(obj)    ' set
          Set NextP =obj
     End Property    
     Public Property  Get getNextP         ' get
          Set getNextP =NextP
     End Property    
End  Class
Class Stack              '
     Private Top          '
     Private count        '
    
     Private Sub  Class_Initialize         '
         count = 0
          Set Top = New StackNode
          Set Top.setNextP = Nothing
     End  Sub
    
     Public Sub  Push(elem)            '
          If  IsEmpty(Top.getData) = True  Then
              Top.letData =elem
              count =count + 1
          Else
               Dim ndobj
               Set ndobj = New StackNode
               Set ndobj.setNextP =Top.getNextP
              ndobj.letData =Top.getData
              Top.letData =elem
               Set Top.setNextP =ndobj
              count =count + 1
          End If
     End  Sub
     Public  Function Pop          '
          If IsRaw = True  Then       '
               MsgBox  " Stack is empty" 
               Exit Function
          Else
               If count  > 1  Then
                Pop =Top.getData
                Top.letData =Top.getNextP.getData
                 Set Top.setNextP =Top.getNextP.getNextP
             Else
                Pop =Top.getData
                Top.letData = Empty
             End If
              count =count -1
          End If
     End Function
     Public  Function  GetTop       '
          If IsRaw = True  Then
               MsgBox  " Stack is empty" 
               Exit Function
          Else            
               GetTop =Top.getData
          End If
     End Function
     Public  Function IsRaw        '
          If count  >  0  Then 
              IsRaw = False
          Else
              IsRaw = True
          End If
     End Function
     Public  Function StackLength  '
         StackLength =count
     End Function
     Public Sub  Clear             '
         count = 0
         Top.letData = Empty
     End  Sub
End  Class
 
 
实例对像的测试代码:
' ------------------------------- 
'        
' -------------------------------    
Dim TestST,i,str                         '    
Set TestST = New Stack                     '
MsgBox TestST.IsRaw                      '
MsgBox TestST.StackLength                '
TestST.Push  " TESTTING"                   '
TestST. Clear                             '
TestST.Push  " U"                          '
TestST.Push  " O"
TestST.Push  " Y"
TestST.Push  " E"
TestST.Push  " V"
TestST.Push  " O"
TestST.Push  " L"
TestST.Push  " I"
MsgBox TestST. GetTop                     '
MsgBox TestST.IsRaw                      '
MsgBox TestST.StackLength                '
str = " "
For i = 1  To TestST.StackLength Step  1
    str =str &TestST.Pop & "  "               '
Next
MsgBox str                               '
MsgBox TestST.IsRaw                      '
MsgBox TestST.StackLength                '
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值