我想在VBA for Excel中实现"Stack"类.我想使用Last In First Out结构.以前有人遇到过这个问题吗?你知道外部库处理结构,如Stack,Hastable,Vector ......(除了原始的Excel Collection等...)
谢谢
bru*_*uce 10
这是一个非常简单的堆栈类.
Option Explicit
Dim pStack As Collection
Public Function Pop() As Variant
With pStack
If .Count > 0 Then
Pop = .Item(.Count)
.Remove .Count
End If
End With
End Function
Public Function Push(newItem As Variant) As Variant
With pStack
.Add newItem
Push = .Item(.Count)
End With
End Function
Public Sub init()
Set pStack = New Collection
End Sub
Run Code Online (Sandbox Code Playgroud)
测试一下
Option Explicit
Sub test()
Dim cs As New cStack
Dim i As Long
Set cs = New cStack
With cs
.init
For i = 1 To 10
Debug.Print CStr(.Push(i))
Next i
For i = 1 To 10
Debug.Print CStr(.Pop)
Next i
End With
End Sub
Run Code Online (Sandbox Code Playgroud)
布鲁斯