链式队列

新建节点node.cls

Option Explicit

 

Public data As Integer

Public nextdata As node

建队列类queue.cls

Option Explicit

 

 

Public head As node

Public length As Integer

 

‘初始化类

Private Sub Class_Initialize()

Set head = New node

Me.length = 0

End Sub

 

‘进队

Public Sub enQueue(e As Integer)

Dim s As node

Set s = New node

s.data = e

Set s.nextdata = Nothing

If Me.length = 0 Then

Set head.nextdata = s

Me.length = Me.length + 1

Exit Sub

End If

Dim i As Integer

Dim p As node

Set p = head

For i = 1 To Me.length

Set p = p.nextdata

Next i

Me.length = Me.length + 1

End Sub

 

 

‘出队

Public Function deQueue() As Integer

If Me.length = 0 Then

Exit Function

End If

Dim i As Integer

Dim p As node

Set p = head

For i = 1 To Me.length

Set p = p.nextdata

Next i

Me.length = Me.length – 1

deQueue = p.data

End Function

演示

Option Explicit

 

Dim a As New queue

 

Private Sub CommandButton1_Click()

a.enQueue (123)

End Sub

 

Private Sub CommandButton2_Click()

MsgBox a.deQueue

End Sub

2021年3月18日

 

 

 

 

发表评论

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