链式栈

新建节点类snode.cls

Option Explicit

 

Public data As Integer

Public nextnode As SNODE

新建链表类linkstack.cls

Public top As SNODE

 

‘栈的初始化

Public Sub initstack()

Set top = Nothing

End Sub

 

‘入栈

Public Sub push(e As Integer)

Dim n As SNODE

Set n = New SNODE

n.data = e

Set n.nextnode = top

Set top = n

End Sub

 

‘出栈

Public Function pop() As Integer

If top Is Nothing Then

MsgBox “栈已经空了”

Exit Function

Else

Dim n As SNODE

Set n = top

Set top = n.nextnode

pop = n.data

End If

End Function

应用

Option Explicit

Public a As linkstack

 

Private Sub CommandButton1_Click()

Set a = New linkstack

a.initstack

a.push 123

End Sub

 

Private Sub CommandButton2_Click()

MsgBox a.pop()

End Sub

2021年3月16日

 

 

 

 

 

 

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注