Excel VBA 的 LIFO(堆栈)算法/类 [英] LIFO (Stack) Algorithm/Class for Excel VBA
本文介绍了Excel VBA 的 LIFO(堆栈)算法/类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望在 VBA for Excel 中实现一个堆栈"类.我想使用后进先出结构.有没有人遇到过这个问题?你知道外部库处理结构,如 Stack、Hastable、Vector...(除了原始的 Excel Collection 等...)
I'm looking to implement a "Stack" Class in VBA for Excel. I want to use a Last In First Out structure. Does anyone came across this problem before ? Do you know external libraries handling structure such as Stack, Hastable, Vector... (apart the original Excel Collection etc...)
谢谢
推荐答案
这是一个非常简单的堆栈类.
Here is a very simple stack class.
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
测试一下
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
布鲁斯
这篇关于Excel VBA 的 LIFO(堆栈)算法/类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文