网上有人说新时代的男性要两个头都要抓,两个头都要硬。好吧,不撸了。坚持去游泳,顺便记录每天锻炼大头的点滴。
复习一下数据结构,OO思想。两年没有写过脚本了,抛开集合和字典对象,用vbscript伪面向对象实现一下线性表。
线性表类主体:
Option
Explicit
'--------------------------------------
' 顺序存储结构的线性表 by.Shine
'--------------------------------------
Class
ListMode
'创建类
Private
Data
(
10
)
'以数组定长方式初始化表容量为10
Private
Length
'表长
Public
Sub
Class_Initialize
Length
=
0
End
Sub
Public
Sub
AddElem
(
elem
)
'线性表加元素
If
Length
=
10
Then
MsgBox
"list full,cannot add "
&
elem
Exit
Sub
Else
Data
(
Length
)
=
elem
Length
=
Length
+1
End
If
End
Sub
Public
Function
IndexGetElem
(
index
)
'线性表读指定下标内容
If
Length
=
0
Or
index
<
1
Or
index
>
Length
Then
MsgBox
"List is empty or index is not correct"
Exit
Function
Else
IndexGetElem
=
Data
(
index
-1
)
End
If
End
Function
Public
Function
ElemGetIndex
(
elem
)
'线性表中查找第一次出现指定元素的下标
If
Length
=
0
Then
MsgBox
"List is empty"
Exit
Function
End
If
Dim
k
For
k
=
1
To
Length
Step
1
If
Data
(
k
-1
)
=
elem
Then
ElemGetIndex
=
k
Exit
For
End
If
Next
End
Function
Public
Sub
Insert
(
elem
,
index
)
'线性表插元素
If
Length
=
10
Then
MsgBox
"list full,cannot insert "
&
elem
Exit
Sub
End
If
If
index
<
1
Or
index
>
Length
+1
Then
MsgBox
"Index is not correct"
Exit
Sub
End
If
If
index
<=
Length
Then
Dim
k
For
k
=
Length
-1
To
index
-1
Step
1
Data
(
k
+1
)
=
Data
(
k
)
Next
End
If
Data
(
index
-1
)
=
elem
Length
=
Length
+1
End
Sub
Public
Function
IndexDel
(
index
)
'线性表删除指定下标(线性表下标)元素
If
Length
=
0
Or
index
<
1
Or
index
>
Length
Then
MsgBox
"List is empty or index is not correct"
Exit
Function
End
If
IndexDel
=
Data
(
index
-1
)
If
index
<
Length
Then
Dim
k
For
k
=
index
To
Length
-1
Step
1
Data
(
k
-1
)
=
Data
(
k
)
Next
End
If
Length
=
Length
-1
End
Function
Public
Sub
ElemDel
(
elem
)
'线性表删除指定元素(单步无递归)
If
Length
=
0
Then
MsgBox
"List is empty"
Exit
Sub
End
If
Dim
k
For
k
=
1
To
Length
Step
1
If
Data
(
k
-1
)
=
elem
Then
IndexDel
k
Exit
For
End
If
Next
End
Sub
Public
Function
getLength
'返回线性表长度
getLength
=
Length
End
Function
Public
Sub
Clear
'清空线性表
Length
=
0
End
Sub
End
Class
以下为实例对象测试代码:
'
-------------------------------
'
脚本空间调用类实例
'
-------------------------------
Dim list,i,temp
'
定义变量
Set list
=
New ListMode
'
实例对象
MsgBox list.getLength
'
取得表长
list.
IndexGetElem(
0)
'
测试非正常查询
list.
IndexGetElem(
1)
list.
IndexGetElem(
4)
Call list.
Clear
'
测试清除
For i
=
1
To
10 Step
1
list.AddElem
"
elem"
&i
'
填满
Next
list.AddElem
"
a"
'
测试溢出
MsgBox list.getLength
'
取得表长
temp
=
"
"
For i
=
1
To list.getLength Step
1
temp
=temp
&list.
IndexGetElem(i)
&
"
"
'
遍历全表
Next
MsgBox temp
list.
ElemDel(
"
elem2")
'
测试删除指定内容
temp
=
"
"
For i
=
1
To list.getLength Step
1
temp
=temp
&list.
IndexGetElem(i)
&
"
"
'
遍历全表
Next
MsgBox temp
list.
IndexDel(
5)
'
测试删除下标5的内容
temp
=
"
"
For i
=
1
To list.getLength Step
1
temp
=temp
&list.
IndexGetElem(i)
&
"
"
'
遍历全表
Next
MsgBox temp
MsgBox list.
ElemGetIndex(
"
elem4")
'
取得指定内容存放下标
MsgBox list.
IndexGetElem(
3)
'
取得指定下标存放内容
Call list.
Clear
'
测试清除
MsgBox list.getLength
temp
=
"
"
For i
=
1
To list.getLength Step
1
temp
=temp
&list.
IndexGetElem(i)
&
"
"
'
遍历全表
Next