大头锻炼日记2 -- 无指针和结构体的vbscript中实现单向链表
2014-11-22 21:49
501 查看
大头锻炼日记2 -- 无指针和结构体的vbscript中实现单向链表
继续数据结构复习,脚本实现单链。vbscript一种弱类型(无具体引用类型)无结构体和无指针的脚本中不借助宿主对象也可以实现链表。
以下为单链表类主体: 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
大头锻炼日记2 -- 无指针和结构体的vbscript中实现单向链表
继续数据结构复习,脚本实现单链。vbscript一种弱类型(无具体引用类型)无结构体和无指针的脚本中不借助宿主对象也可以实现链表。
以下为单链表类主体: 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
相关文章推荐
- 大头锻炼日记1 -- vbscript实现典型线性表
- 大头锻炼日记7 -- vbscript的链表队列Queue
- 大头锻炼日记3 -- vbscript静态链表实现法
- 编码实现环状单向链表(尾指针直接指向头指针,中间没有空节点),去除连续的重复元素的操作。
- 大头锻炼日记5 -- vbscript中的栈Stack(链栈)
- 关于指针的调用实现单向链表及指针函数的几个小问题
- 编码实现环状单向链表(尾指针直接指向头指针,中间没有空节点),去除连续的重复元素的操作
- 大头锻炼日记6 -- vbscript中循环队列Queue
- 利用双重指针来实现单向链表的节点交换
- Python 实现带头尾指针的单向链表
- 大头锻炼日记4 -- vbscript中的栈Stack
- 编码实现环状单向链表(尾指针直接指向头指针,中间没有空节点),去除连续的重复元素的操作
- 通过结构体指针实现顺序链表的基础操作
- list.c - A linked list by C --- C语言实现的单向链表
- 带表头的单向链表实现(C语言)
- C#单向链表的实现
- 用C#实现单向链表
- 线性表的链式存储结构_单向链表[带头结点]_C#实现
- 单向链表的原地归并排序实现
- 单向链表实现倒排序