大头锻炼日记2 -- 无指针和结构体的vbscript中实现单向链表
继续数据结构复习,脚本实现单链。vbscript一种弱类型(无具体引用类型)无结构体和无指针的脚本中不借助宿主对象也可以实现链表。
以下为单链表类主体: vbs默认传参类型为ByRef
以下为实例测试代码:
以下为单链表类主体: vbs默认传参类型为ByRef
Option
Explicit
'-----------------------------------------
' 单链表方式线性表 by.Shine
'-----------------------------------------
Class
Node
'定义节点类
Private
Data
'数据域
Private
NextP
'指针域
Public
Property
Let
letData
(
da
)
'数据域set方法
Data
=
da
End
Property
Public
Property
Get
getData
'数据域get方法
getData
=
Data
End
Property
Public
Property
Set
setNextP
(
obj
)
'指针域set方法
Set
NextP
=
obj
End
Property
Public
Property
Get
getNextP
'指针域get方法
Set
getNextP
=
NextP
End
Property
End
Class
Class
List
'定义链表类
Private
Head
'标头
Private
Tail
'表尾
Private
Length
'表长
Private
Sub
Class_Initialize
'构造方法初始化
Length
=
0
Set
Head
=
New
Node
'开辟表头
Set
Head
.
SetNextP
=
Nothing
'初始化指针
Set
Tail
=
Head
'开辟表尾
End
Sub
Public
Property
Get
getLength
'取得链表长度
getLength
=
Length
End
Property
Public
Sub
Clear
'清除链表
Length
=
0
Set
Head
=
Nothing
Set
Tail
=
Nothing
End
Sub
Public
Function
IsRaw
'查询是否为空链
If
Length
=
0
Then
IsRaw
=
True
Else
IsRaw
=
False
End
If
End
Function
Public
Function
GetElem
(
index
)
'取得链表指定位置的内容
If
index
<
1
Or
index
>
Length
Then
MsgBox
"Index is not correct"
Exit
Function
Else
Dim
temp
,
i
Set
temp
=
Head
For
i
=
0
To
index
-1
Step
1
Set
temp
=
temp
.
getNextP
Next
MsgBox
"List index"
&
index
&
" is "
&
temp
.
getData
End
If
End
Function
Public
Sub
AddHead
(
elem
)
'表头添加元素
If
IsEmpty
(
elem
)
Then
MsgBox
"elem is empty"
Exit
Sub
Else
Dim
ndobj
Set
ndobj
=
New
Node
Set
ndobj
.
setNextP
=
Nothing
ndobj
.
letData
=
elem
Set
ndobj
.
setNextP
=
Head
.
getNextP
Set
Tail
=
ndobj
.
getNextP
Set
Head
.
setNextP
=
ndobj
Length
=
Length
+1
End
If
End
Sub
Public
Sub
AddTail
(
elem
)
'表尾添加元素
If
IsEmpty
(
elem
)
Then
MsgBox
"elem is empty"
Exit
Sub
Else
Dim
ndobj
,
temp
Set
temp
=
Tail
Set
ndobj
=
New
Node
Set
ndobj
.
setNextP
=
Nothing
ndobj
.
letData
=
elem
Set
temp
.
setNextP
=
ndobj
Set
Tail
=
ndobj
Length
=
Length
+1
End
if
End
Sub
Public
Sub
Delete
(
index
)
'删除指定位置的元素
If
index
<
1
Or
index
>
Length
Then
MsgBox
"Index is not correct"
Exit
Sub
Else
Dim
temp
,
p
,
i
Set
temp
=
Head
For
i
=
0
To
index
-1
Step
1
Set
p
=
temp
Set
temp
=
temp
.
getNextP
Next
Set
p
.
setNextP
=
temp
.
getNextP
Length
=
Length
-1
End
If
End
Sub
Public
Sub
AddIndex
(
elem
,
index
)
'链表指定位置插入元素
If
index
<
1
Or
index
>
Length
Then
MsgBox
"Index is not correct"
Exit
Sub
Else
Dim
ndobj
,
temp
,
p
,
i
Set
ndobj
=
New
Node
Set
ndobj
.
setNextP
=
Nothing
ndobj
.
letData
=
elem
Set
temp
=
Head
For
i
=
0
To
index
-1
Step
1
Set
p
=
temp
Set
temp
=
temp
.
getNextP
Next
Set
ndobj
.
setNextP
=
temp
Set
p
.
setNextP
=
ndobj
Length
=
Length
+1
End
If
End
Sub
Public
Function
Print
'打印单向链表
Dim
temp
,
i
Set
temp
=
Head
For
i
=
0
To
Length
Step
1
Print
=
Print
&
temp
.
getData
&
" "
Set
temp
=
temp
.
getNextP
Next
End
Function
End
Class
'
-------------------------------
'
脚本空间调用类实例
'
-------------------------------
Dim TestLS
Set TestLS
=
New List
'
类实例化
MsgBox TestLS.IsRaw
'
测试空表判断
MsgBox TestLS.getLength
'
测试表长判断
TestLS.AddHead
"
W"
'
测试表头添加
TestLS.AddHead
"
S"
TestLS.AddTail
44
'
测试表尾添加
TestLS.AddTail
33
TestLS.AddTail
"
P"
TestLS.AddTail
22
TestLS.AddTail
77
TestLS.AddHead
"
O"
TestLS.AddHead
99
TestLS.AddHead
66
MsgBox TestLS.getLength
MsgBox TestLS.Print
TestLS.Delete
3
'
测试删除一个链表位置的元素
MsgBox TestLS.Print
MsgBox TestLS.getLength
TestLS.AddIndex
"
N",
3
'
测试指定位置插入元素
MsgBox TestLS.Print
MsgBox TestLS.getLength
TestLS.
Clear
'
测试清空链表
MsgBox TestLS.IsRaw
MsgBox TestLS.getLength