大头锻炼日记4 -- vbscript中的栈Stack
2014-11-22 21:53
155 查看
vbscript能实现链式链表和静态链表就可以很方便的实现栈。栈一样分为链式栈和静态栈。
以下为静态栈的类主体代码:
Option Explicit
'--------------------------------------
' 数组结构的栈 by.Shine
'--------------------------------------
Class Stack '创建类
Private Data(10) '以数组定长方式初始化栈容量为10
Private Top '栈顶
Public Sub Class_Initialize '构造方法初始化
Top=-1
End Sub
Public Sub Push(elem) '压栈
If Top=(10-1) Then '检查栈满
MsgBox "Stack is full"
Exit Sub
Else
Top=Top+1
Data(Top)=elem
End If
End Sub
Public Function Pop '弹栈
If IsRaw=True Then '检查栈空
MsgBox "Stack is empty"
Exit Function
Else
Pop=Data(Top)
Top=Top-1
End If
End Function
Public Function GetTop '取得栈顶元素
If IsRaw=True Then
MsgBox "Stack is empty"
Exit Function
Else
GetTop=Data(Top)
End If
End Function
Public Function IsRaw '判断栈是否空栈
If Top > -1 Then
IsRaw=False
Else
IsRaw=True
End If
End Function
Public Function StackLength '取得栈长
StackLength=Top+1
End Function
Public Sub Clear '清空栈
Top=-1
End Sub
End Class
以下为类实例测试代码:
'-------------------------------
' 脚本空间调用类实例
'-------------------------------
Dim TestST,i,str '定义变量
Set TestST=New Stack '实例对象
MsgBox TestST.IsRaw '测试是否为空栈
MsgBox TestST.StackLength '取得栈表长
TestST.Push "TESTTING" '测试压栈
TestST.Clear '测试清空栈表
TestST.Push "U" '测试连续压栈
TestST.Push "O"
TestST.Push "Y"
TestST.Push "E"
TestST.Push "V"
TestST.Push "O"
TestST.Push "L"
TestST.Push "I"
MsgBox TestST.GetTop '取得当前栈顶元素
MsgBox TestST.IsRaw '测试是否为空栈
MsgBox TestST.StackLength '取得栈表长
str=""
For i=1 To 8 Step 1
str=str&TestST.Pop&" " '测试连续弹栈并暂存
Next
MsgBox str '打印弹栈内容,反序显示
MsgBox TestST.IsRaw '测试是否为空栈
MsgBox TestST.StackLength '取得栈表长
vbscript能实现链式链表和静态链表就可以很方便的实现栈。栈一样分为链式栈和静态栈。
以下为静态栈的类主体代码:
Option Explicit
'--------------------------------------
' 数组结构的栈 by.Shine
'--------------------------------------
Class Stack '创建类
Private Data(10) '以数组定长方式初始化栈容量为10
Private Top '栈顶
Public Sub Class_Initialize '构造方法初始化
Top=-1
End Sub
Public Sub Push(elem) '压栈
If Top=(10-1) Then '检查栈满
MsgBox "Stack is full"
Exit Sub
Else
Top=Top+1
Data(Top)=elem
End If
End Sub
Public Function Pop '弹栈
If IsRaw=True Then '检查栈空
MsgBox "Stack is empty"
Exit Function
Else
Pop=Data(Top)
Top=Top-1
End If
End Function
Public Function GetTop '取得栈顶元素
If IsRaw=True Then
MsgBox "Stack is empty"
Exit Function
Else
GetTop=Data(Top)
End If
End Function
Public Function IsRaw '判断栈是否空栈
If Top > -1 Then
IsRaw=False
Else
IsRaw=True
End If
End Function
Public Function StackLength '取得栈长
StackLength=Top+1
End Function
Public Sub Clear '清空栈
Top=-1
End Sub
End Class
以下为类实例测试代码:
'-------------------------------
' 脚本空间调用类实例
'-------------------------------
Dim TestST,i,str '定义变量
Set TestST=New Stack '实例对象
MsgBox TestST.IsRaw '测试是否为空栈
MsgBox TestST.StackLength '取得栈表长
TestST.Push "TESTTING" '测试压栈
TestST.Clear '测试清空栈表
TestST.Push "U" '测试连续压栈
TestST.Push "O"
TestST.Push "Y"
TestST.Push "E"
TestST.Push "V"
TestST.Push "O"
TestST.Push "L"
TestST.Push "I"
MsgBox TestST.GetTop '取得当前栈顶元素
MsgBox TestST.IsRaw '测试是否为空栈
MsgBox TestST.StackLength '取得栈表长
str=""
For i=1 To 8 Step 1
str=str&TestST.Pop&" " '测试连续弹栈并暂存
Next
MsgBox str '打印弹栈内容,反序显示
MsgBox TestST.IsRaw '测试是否为空栈
MsgBox TestST.StackLength '取得栈表长
相关文章推荐
- 大头锻炼日记5 -- vbscript中的栈Stack(链栈)
- 大头锻炼日记6 -- vbscript中循环队列Queue
- 大头锻炼日记7 -- vbscript的链表队列Queue
- 大头锻炼日记1 -- vbscript实现典型线性表
- 大头锻炼日记2 -- 无指针和结构体的vbscript中实现单向链表
- 大头锻炼日记3 -- vbscript静态链表实现法
- 骑行日记,回暖天晴,开始恢复锻炼 推荐
- VBScript:关闭进程与模拟键盘输入
- 微信开发学习日记(五):weiphp开源框架的bug,公众号权限编辑问题
- 2017.08.24工作日记
- 日记:2010-08-18 星期三
- 黑马程序员_日记48_泛型基本使用
- 日记:一个大坑,关于python的sort和sorted
- Sql server 日记 (删除administrator 组造成无法登陆SQL SERVER)
- 微信开发学习日记(七):开源微商城wemall
- 【父母如何给孩子合理安排锻炼时间…
- 日记0827
- houmee实习日记4.20-22
- Arduino学习日记四
- 过程改进日记之学习Scrum2010-8-18 backlog or Feature list?(二)