클래스 모듈(StackItem)
' 스택(Stack) 데이터 구조
' 변수 선언
Public Value As Variant
Public NextItem As StackItem
Dim siTop As StackItem
Public Sub Push(ByVal varText As Variant)
' 스택의 맨위에 새로운 항목을 추가하고, 이 것을 맨위로 설정한다.
Dim siNewTop As New StackItem
siNewTop.Value = varText
Set siNewTop.NextItem = siTop
Set siTop = siNewTop
End Sub
Public Function Pop() As Variant
If Not StackEmpty Then
' 현재 맨위 항목의 값을 얻고(사라짐), 그 다음 항목을 맨위항목으로 설정한다.
Pop = siTop.Value
Set siTop = siTop.NextItem
End If
End Function
Property Get StackEmpty() As Boolean
' 스택이 비어있는가를 검사. Is the stack empty? It can
' only be empty if siTop is Nothing.
StackEmpty = (siTop Is Nothing)
End Property
Property Get StackTop() As Variant
'항목을 제거하지 않고 맨위 항목의 값을 얻는다.
If StackEmpty Then
StackTop = Null
Else
StackTop = siTop.Value
End If
End Property
' 변수 선언
Public Value As Variant
Public NextItem As StackItem
Dim siTop As StackItem
Public Sub Push(ByVal varText As Variant)
' 스택의 맨위에 새로운 항목을 추가하고, 이 것을 맨위로 설정한다.
Dim siNewTop As New StackItem
siNewTop.Value = varText
Set siNewTop.NextItem = siTop
Set siTop = siNewTop
End Sub
Public Function Pop() As Variant
If Not StackEmpty Then
' 현재 맨위 항목의 값을 얻고(사라짐), 그 다음 항목을 맨위항목으로 설정한다.
Pop = siTop.Value
Set siTop = siTop.NextItem
End If
End Function
Property Get StackEmpty() As Boolean
' 스택이 비어있는가를 검사. Is the stack empty? It can
' only be empty if siTop is Nothing.
StackEmpty = (siTop Is Nothing)
End Property
Property Get StackTop() As Variant
'항목을 제거하지 않고 맨위 항목의 값을 얻는다.
If StackEmpty Then
StackTop = Null
Else
StackTop = siTop.Value
End If
End Property
일반모듈에서 테스트
Dim stkTest As New StackItem
Sub TestStacks()
stkTest.Push "안녕!"
stkTest.Push "그동안"
stkTest.Push "잘"
stkTest.Push "지냈어요?"
stkTest.Push "^_^;;"
Do While Not stkTest.StackEmpty
Debug.Print stkTest.Pop()
Loop
Debug.Print
Debug.Print "테스트 종료"
End Sub
Source : http://msdn.microsoft.com/en-us/library/aa227567(VS.60).aspx
Sub TestStacks()
stkTest.Push "안녕!"
stkTest.Push "그동안"
stkTest.Push "잘"
stkTest.Push "지냈어요?"
stkTest.Push "^_^;;"
Do While Not stkTest.StackEmpty
Debug.Print stkTest.Pop()
Loop
Debug.Print
Debug.Print "테스트 종료"
End Sub
댓글 없음:
댓글 쓰기