静态链表法 一种使用数组和游标方式实现单向链表的方法。
甚至可以用并行基本类型数组来模拟对象类型数组。用vbscript实现的效果。
以下为静态链类主体:
Option Explicit
'
-----------------------------------------------------------
'
静态链表(数组+游标)方式的链式线性表 by.Shine
'
-----------------------------------------------------------
Class StructNode
'
创建Node类来实现结构体数组
Private data
'
数据域
Private cur
'
游标域(指针)
Public Property
Let
letData(da)
'
data set方法
data
=da
End Property
Public Property
Get getData
'
data get方法
getData
=data
End Property
Public Property
Let
letCur(c)
'
游标 set方法
If c
<
0
Then
MsgBox
"
Cur is not correct"
Else
cur
=c
End If
End Property
Public Property
Get getCur
'
游标 get方法
getCur
=cur
End Property
End
Class
Class StaticLinked
'
创建静态链表类
Public
StructArr(
10)
'
定义结构体数组容量,假设为10
Public Sub
Class_Initialize
'
初始化链表
Dim i
For i
=
0
To
9 Step
1
Set
StructArr(i)
=
New StructNode
'
元素构造为Node类型
If i
=
9
Then
StructArr(i).letCur
=
0
'
数组头元素游标指向空闲位置的起始下标
Else
StructArr(i).letCur
=i
+
1
'
数组尾元素游标指向已被占据位置的起始下标
End If
Next
End
Sub
Private
Function Malloc
'
申请新空间
Dim i
i
=
StructArr(
0).getCur
StructArr(
0).letCur
=
StructArr(i).getCur
Malloc
=i
End Function
Public Sub
Free(index)
'
释放占用空间
StructArr(index).letCur
=
StructArr(
0).getCur
StructArr(
0).letCur
=index
End
Sub
Public
Function Length
'
获取链表长
Dim count,i
count
=
0
i
=
StructArr(
9).getCur
Do
While i
<>
0
i
=
StructArr(i).getCur
count
=count
+
1
Loop
Length
=count
End Function
Public Sub
Insert(index,elem)
'
链表中插入元素
Dim ln,i,j,temp
temp
=
9
ln
=Length
If index
<
1
Or index
> ln
+
1
Then
MsgBox
"
Index is not correct"
Exit Sub
End If
i
=Malloc
If i
>
0
Then
StructArr(i).letData
=elem
For j
=
1
To index
-1 Step
1
temp
=
StructArr(temp).getCur
Next
StructArr(i).letCur
=
StructArr(temp).getCur
StructArr(temp).letCur
=i
Else
MsgBox
"
Can not get free space"
Exit Sub
End If
End
Sub
Public Sub
Delete(index)
'
链表中删除元素
Dim j,ln,temp
ln
=Length
If index
<
1
Or index
> ln
Then
MsgBox
"
Index is not correct"
Exit Sub
End If
temp
=
9
For j
=
1
To index
-1 Step
1
temp
=
StructArr(temp).getCur
Next
j
=
StructArr(temp).getCur
StructArr(temp).letCur
=
StructArr(j).getCur
Free(j)
End
Sub
Public
Function IsRaw
'
检查是否为空
If Length
>
0
Then
IsRaw
=
False
Else
IsRaw
=
True
End If
End Function
Public
Function Print
'
打印静态链表
Dim j,var
j
=TestSL.
StructArr(
9).getCur
var
=
"
"
For i
=
1
To Length Step
1
var
=var
&
StructArr(j).getData
&
"
"
'
遍历链表中元素并按顺序打印
j
=
StructArr(j).getCur
Next
MsgBox var
End Function
End
Class
以下为类实例对象测试代码:
'
-------------------------------
'
脚本空间调用类实例
'
-------------------------------
Dim TestSL,i,j,data
Set TestSL
=
New StaticLinked
'
类实例化
MsgBox
"
list length is "
&TestSL.Length
'
测试获取链表长度
MsgBox TestSL.IsRaw
'
测试判断空表
TestSL.Insert
1,
"
A"
'
链表插入数据
TestSL.Insert
2,
"
C"
TestSL.Insert
3,
"
D"
TestSL.Insert
4,
"
E"
TestSL.Insert
2,
"
B"
MsgBox
"
list length is "
&TestSL.Length
'
测试获取链表长度
MsgBox TestSL.IsRaw
TestSL.Print
'
测试链表打印
data
=
"
"
For i
=
0
To
9 Step
1
data
=data
&
"
Index"
&i
&
"
{Data:"
&TestSL.
StructArr(i).getData
&
"
,Cur:index"
&_
TestSL.
StructArr(i).getCur
&
"
} "
'
取得存储结构中实际存放逻辑
Next
MsgBox data
TestSL.Delete
4
'
测试删除链表位置的元素
TestSL.Delete
3
TestSL.Print
data
=
"
"
For i
=
0
To
9 Step
1
data
=data
&
"
Index"
&i
&
"
{Data:"
&TestSL.
StructArr(i).getData
&
"
,Cur:index"
&_
TestSL.
StructArr(i).getCur
&
"
} "
'
取得存储结构中实际存放逻辑
Next
MsgBox data