-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack.cls
58 lines (51 loc) · 1.48 KB
/
Stack.cls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "Stack"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
' Copyright William Schwartz 2013.
' Stack is a singly linked list implementing a stack interface. Insert
' values with Push. Remove values from with Pop. Count the size of the stack
' with Count. Call Init immediately after making a new stack.
Option Explicit
Private head As node
Private length As Long
Const EmptyStackErrorCode As Long = 513
Public Sub Init()
' Initialize a new stack.
Set head = Nothing
length = 0
End Sub
Public Function Count() As Long
' Return the number of elements in the stack
Count = length
End Function
Public Sub Push(v As Long)
' Place v on the top of the stack.
Dim newNode As New node
newNode.Value = v
If head Is Nothing Then
Set newNode.nextNode = Nothing
Else
Set newNode.nextNode = head
End If
Set head = newNode
length = length + 1
End Sub
Public Function Pop() As Long
' Return the number stored at the top of the stack
If head Is Nothing Then
Err.Raise Number:=vbObjectError + EmptyStackErrorCode, source:="Stack", Description:="Pop from empty stack."
Else
Dim newHead As node
Pop = head.Value
Set newHead = head.nextNode
Set head.nextNode = Nothing
Set head = newHead
length = length - 1
End If
End Function