您的位置:首页 > 编程语言 > VB

大头锻炼日记4 -- vbscript中的栈Stack

2014-11-22 21:53 155 查看

vbscript能实现链式链表和静态链表就可以很方便的实现栈。栈一样分为链式栈和静态栈。

以下为静态栈的类主体代码: 

Option Explicit
'--------------------------------------
'     数组结构的栈   by.Shine
'--------------------------------------
Class Stack  '创建类
    Private Data(10)    '以数组定长方式初始化栈容量为10
    Private Top         '栈顶
    
    Public Sub Class_Initialize     '构造方法初始化
        Top=-1
    End Sub
    
    Public Sub Push(elem)       '压栈
        If Top=(10-1) Then      '检查栈满
            MsgBox "Stack is full" 
            Exit Sub
        Else
            Top=Top+1
            Data(Top)=elem
        End If
    End Sub
    Public Function Pop         '弹栈
        If IsRaw=True Then      '检查栈空
            MsgBox "Stack is empty" 
            Exit Function
        Else
            Pop=Data(Top)
            Top=Top-1
        End If
    End Function
    Public Function GetTop      '取得栈顶元素
        If IsRaw=True Then
            MsgBox "Stack is empty" 
            Exit Function
        Else            
            GetTop=Data(Top)
        End If
    End Function
    Public Function IsRaw       '判断栈是否空栈
        If Top > -1 Then 
            IsRaw=False
        Else
            IsRaw=True
        End If
    End Function
    Public Function StackLength '取得栈长
        StackLength=Top+1
    End Function
    Public Sub Clear            '清空栈
        Top=-1
    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 8 Step 1
    str=str&TestST.Pop&" "              '测试连续弹栈并暂存
Next
MsgBox str                              '打印弹栈内容,反序显示

MsgBox TestST.IsRaw                     '测试是否为空栈
MsgBox TestST.StackLength               '取得栈表长
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息