堆栈(简称为栈)是一种先入后出(First In, Last Out)的数据结构。
环境要求
Windows XP
及以上。Windows 10
、Windows 11
在Windows 功能
中勾选.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