【原创】在 VBScript 中使用堆栈(Stack)

堆栈(简称为栈)是一种先入后出(First In, Last Out)的数据结构。

环境要求

  • Windows XP 及以上。
  • Windows 10Windows 11Windows 功能 中勾选 .NET Framework 3.5 (包括 .NET 2.0 和 3.0)

前置知识

WSH.Echo Empty = Empty
-1
WSH.Echo Null = Null
null
WSH.Echo New RegExp Is New RegExp
0
Set oRE = New RegExp
WSH.Echo oRE Is oRE
-1
WSH.Echo CreateObject("Scripting.FileSystemObject") Is CreateObject("Scripting.FileSystemObject")
0
Set oFS = CreateObject("Scripting.FileSystemObject")
WSH.Echo oFS Is oFS
-1

下面两个返回值出现的原因是浮点误差:

WSH.Echo 0.1 + 0.2 = 0.3
0
WSH.Echo 100000000000000000000000 = 100000000000000000000001
-1

使用

创建一个 Stack 对象:

Set oS = CreateObject("System.Collections.Stack")

Push 方法:将元素推入栈中

Set oS = CreateObject("System.Collections.Stack")
oS.Push Empty
oS.Push Null
oS.Push "String"
oS.Push 0
oS.Push 3.14
oS.Push CreateObject("Scripting.FileSystemObject")
oS.Push New RegExp
oS.Push True
oS.Push False

Count 属性:表示当前栈内元素个数

Set oS = CreateObject("System.Collections.Stack")
WSH.Echo oS.Count()
0
oS.Push 666
WSH.Echo oS.Count()
1

Clear 方法:清空堆栈

Set oS = CreateObject("System.Collections.Stack")
oS.Push 888
WSH.Echo oS.Count()
1
oS.Clear
WSH.Echo oS.Count()
0

Clone 方法:返回该堆栈的拷贝

Set oS = CreateObject("System.Collections.Stack")
oS.Push 666
Set oS2 = oS.Clone()
oS2.Push 888
WSH.Echo oS Is oS2, oS.Count(), oS2.Count()
0 1 2
Set oS = CreateObject("System.Collections.Stack")
oS.Push 666
Set oS2 = oS
oS2.Push 888
WSH.Echo oS Is oS2, oS.Count(), oS2.Count()
-1 2 2
Set oS = CreateObject("System.Collections.Stack")
oS.Push CreateObject("Scripting.FileSystemObject")
Set oS2 = oS.Clone
WSH.Echo oS Is oS2, oS.Peek() Is oS2.Peek()
0 -1
Set oS = CreateObject("System.Collections.Stack")
oS.Push New RegExp
Set oS2 = oS.Clone
WSH.Echo oS Is oS2, oS.Peek() Is oS2.Peek()
0 -1

ToArray 方法:将堆栈转为普通 VBScript 数组

Set oS = CreateObject("System.Collections.Stack")
oS.Push 1
oS.Push 3.1415926
oS.Push True
WSH.Echo Join(oS.ToArray(), " ")
True 3.1415926 1
Set oS = CreateObject("System.Collections.Stack")
oS.Push New RegExp
WSH.Echo oS.ToArray()(0) Is oS.Peek()
-1
Set oS = CreateObject("System.Collections.Stack")
oS.Push CreateObject("Scripting.FileSystemObject")
WSH.Echo oS.ToArray()(0) Is oS.Peek()
-1

Contains 方法:检查堆栈内是否包含某元素

Set oS = CreateObject("System.Collections.Stack")
oS.Push 1
oS.Push 2
WSH.Echo oS.Contains(0), oS.Contains(1)
0 -1
Set oS = CreateObject("System.Collections.Stack")
oS.Push Null
oS.Push oS
WSH.Echo oS.Contains(Null), oS.Contains(oS), oS.Contains(CreateObject("System.Collections.Stack"))
-1 -1 0

Peek 方法:返回栈顶的元素(但不从堆栈中移除)

Set oS = CreateObject("System.Collections.Stack")
oS.Push 1
oS.Push 2
WSH.Echo oS.Peek(), oS.Peek()
2 2
WSH.Echo Join(oS.ToArray(), " ")
2 1

Pop 方法:移除栈顶元素并将其返回

Set oS = CreateObject("System.Collections.Stack")
oS.Push 1
oS.Push 2
WSH.Echo oS.Pop(), oS.Pop(), UBound(oS.ToArray())
2 1 -1

GetHashCode 方法:返回堆栈的哈希码

Set oS = CreateObject("System.Collections.Stack")
Set oS2 = oS
Set oS3 = oS.Clone()
Set oS4 = CreateObject("System.Collections.Stack")
WSH.Echo oS.GetHashCode(), oS2.GetHashCode(), oS3.GetHashCode(), oS4.GetHashCode()
58225482 58225482 54267293 18643596

Equals 方法:确定是否为同一个堆栈

Set oS = CreateObject("System.Collections.Stack")
Set oS2 = oS
Set oS3 = oS.Clone()
Set oS4 = CreateObject("System.Collections.Stack")
WSH.Echo oS.Equals(oS), oS.Equals(oS2), oS.Equals(oS3), oS.Equals(oS4)
-1 -1 0 0

ToString 方法:返回类名

Set oS = CreateObject("System.Collections.Stack")
WSH.Echo oS.ToString(), TypeName(oS)
System.Collections.Stack Stack

参考

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老刘1号

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值