'一个链表问题,只是VBA中无法短接而已,所以效率挺难控制的,,,
Option Explicit
Const NUM As Long = 10, STP As Long = 3, POS As Long = 1
Sub test()
Dim i As Long, p As Long, n As Long, cnt As Long
ReDim arr(1 To NUM, 1 To 2) As Long
If NUM = 1 Then Debug.Print 1: Exit Sub
For i = 2 To NUM - 1
arr(i, 1) = i + 1
arr(i, 2) = i - 1
Next
arr(1, 1) = 2: arr(NUM, 1) = 1
arr(1, 2) = NUM: arr(NUM, 2) = NUM - 1
p = POS
Do
n = n + 1
If n Mod STP = 0 Then
cnt = cnt + 1
arr(arr(p, 2), 1) = arr(p, 1)
arr(arr(p, 1), 2) = arr(p, 2)
End If
p = arr(p, 1)
Loop Until NUM - cnt = 1
Debug.Print p
End Sub |