摘要:[Visual Basic 6.0] 資料結構自訂型態建立 鏈結串列(Linked List) 模擬佇列(Queue)
[Visual Basic 6.0] 資料結構自訂型態建立 鏈結串列(Linked List) 模擬佇列(Queue)
' 資料結構自訂型態建立 鏈結串列(Linked List) 模擬佇列
Private Type ListTrpe
Value As String
Up As Integer ' 前指標
Dn As Integer ' 後指標
End Type
' 定義目前指標位置
Dim AddData As Integer ' 新增資料
Dim RemData As Integer ' 移除資料
Dim A(9999) As ListTrpe ' 建立 9999 個串列
Private Sub Command1_Click()
Call Add(AddData, AddData) ' 新增資料
End Sub
Private Sub Command2_Click()
If AddData = RemData Then Print "佇列已空": Exit Sub
Call Clas(RemData) ' 移除資料
End Sub
Private Sub Form_Activate()
For I = 0 To UBound(A)
A(I).Value = -1
If I = 0 Then
A(I).Up = I + 1
A(I).Dn = UBound(A)
ElseIf I = UBound(A) Then
A(I).Up = 0
A(I).Dn = UBound(A) - 1
Else
A(I).Up = I + 1
A(I).Dn = I - 1
End If
Next I
End Sub
Function Add(ByRef AddData, Data) ' 新增資料
A(AddData).Value = Data
AddData = AddData + 1
Call Shows
End Function
Function Clas(ByRef RemData) ' 移除資料
A(A(RemData).Dn).Up = A(RemData).Up
RemData = RemData + 1
Call Shows
End Function
Sub Shows() ' 顯示
Cls
S = RemData
Do Until A(S).Value = -1
Print A(S).Value
S = A(S).Up
Loop
End Sub
#0xDe 從分享中學習
#Facebook:ProgrammerDe (https://www.facebook.com/MicrosoftDes) 有問題歡迎提問