大头锻炼日记6 -- vbscript中循环队列Queue
2014-11-22 21:56
211 查看
坚持最重要,继续。
队列(Queue),FIFO。顺序存储结构的队列需要规避地址越界和假溢出,一般采用循环队列模式且头尾指针不重叠。
vbscript使用数组实现循环队列的方法。
创建队列类,主体代码如下:
Option Explicit
'--------------------------------------
' 数组结构的循环队列 by.Shine
' 1.判断循环队列装满的数学公式
' (rear+1)和QueueSize求模的值等于front的index
' 2.计算队列长度的数学公式
' (rear-front+QueueSize)和QueueSize求模的值
'--------------------------------------
Class SqQueue
Private Data(10) '以数组定长方式初始化队列容量为10
Private front '队列头指针
Private rear '队列尾指针
Public Sub Class_Initialize '构造方法初始化
letFront=0 '指针复位
letRear=0 '指针复位
End Sub
Private Property Let letFront(e) '头指针SET方法
front=e
End Property
Private Property Let letRear(e) '尾指针SET方法
rear=e
End Property
Public Property Get getFront '头指针GET方法
getFront=front
End Property
Public Property Get getRear '尾指针GET方法
getRear=rear
End Property
Public Function InitQueue '清空队列
letFront=0
letRear=0
InitQueue=True
End Function
Public Function QueueLength '取得队列长度
QueueLength=((getRear-getFront+10) Mod 10) '由2公式得到队列长度,10是数组长度
End Function
Public Sub InQueue(elem) '队尾插入新元素
If IsFull=True Then '判断是否已满
MsgBox "Queue is full"
Exit Sub
Else
Data(getRear)=elem '插入元素
letRear=((getRear+1) Mod 10) '尾指针循环后移
End If
End Sub
Public Function DeQueue
If IsRaw=True Then '判断是否为空
MsgBox "Queue is empty"
Exit Function
Else
DeQueue=Data(getFront) '取得头元素
letFront=((getFront+1) Mod 10) '头指针循环后移
End If
End Function
Public Function IsRaw '判断是否空队列
If getFront=getRear Then
IsRaw=True
Else
IsRaw=False
End If
End Function
Public Function IsFull '判断队列是否已满 '
If ((getRear+1) Mod 10)=getFront Then
IsFull=True
Else
IsFull=False
End If
End Function
End Class
以下为类实例测试代码:
'-------------------------------
' 脚本空间调用类实例
'-------------------------------
Dim TestSQ,i,str '定义变量
Set TestSQ=New SqQueue '实例对象
MsgBox TestSQ.IsRaw '测试空队列判断
MsgBox TestSQ.IsFull '测试队列溢出
MsgBox "Queue length is "&TestSQ.QueueLength '取得队列长度
TestSQ.InQueue "I" '测试进入队列
TestSQ.InQueue "L"
TestSQ.InQueue "O"
TestSQ.InQueue "V"
TestSQ.InQueue "E"
TestSQ.InQueue "Y"
TestSQ.InQueue "O"
TestSQ.InQueue "U"
MsgBox TestSQ.IsRaw '测试空队列判断
MsgBox "Queue length is "&TestSQ.QueueLength '取得队列长度
str=""
For i=1 To TestSQ.QueueLength Step 1
str=str&TestSQ.DeQueue&" " '取出队列元素并暂存
Next
MsgBox str '打印出对列内容,为FIFO
TestSQ.InitQueue '清空队列
MsgBox TestSQ.IsRaw '测试空队列判断
MsgBox TestSQ.IsFull '测试队列溢出
MsgBox "Queue length is "&TestSQ.QueueLength '取得队列长度
坚持最重要,继续。
队列(Queue),FIFO。顺序存储结构的队列需要规避地址越界和假溢出,一般采用循环队列模式且头尾指针不重叠。
vbscript使用数组实现循环队列的方法。
创建队列类,主体代码如下:
Option Explicit
'--------------------------------------
' 数组结构的循环队列 by.Shine
' 1.判断循环队列装满的数学公式
' (rear+1)和QueueSize求模的值等于front的index
' 2.计算队列长度的数学公式
' (rear-front+QueueSize)和QueueSize求模的值
'--------------------------------------
Class SqQueue
Private Data(10) '以数组定长方式初始化队列容量为10
Private front '队列头指针
Private rear '队列尾指针
Public Sub Class_Initialize '构造方法初始化
letFront=0 '指针复位
letRear=0 '指针复位
End Sub
Private Property Let letFront(e) '头指针SET方法
front=e
End Property
Private Property Let letRear(e) '尾指针SET方法
rear=e
End Property
Public Property Get getFront '头指针GET方法
getFront=front
End Property
Public Property Get getRear '尾指针GET方法
getRear=rear
End Property
Public Function InitQueue '清空队列
letFront=0
letRear=0
InitQueue=True
End Function
Public Function QueueLength '取得队列长度
QueueLength=((getRear-getFront+10) Mod 10) '由2公式得到队列长度,10是数组长度
End Function
Public Sub InQueue(elem) '队尾插入新元素
If IsFull=True Then '判断是否已满
MsgBox "Queue is full"
Exit Sub
Else
Data(getRear)=elem '插入元素
letRear=((getRear+1) Mod 10) '尾指针循环后移
End If
End Sub
Public Function DeQueue
If IsRaw=True Then '判断是否为空
MsgBox "Queue is empty"
Exit Function
Else
DeQueue=Data(getFront) '取得头元素
letFront=((getFront+1) Mod 10) '头指针循环后移
End If
End Function
Public Function IsRaw '判断是否空队列
If getFront=getRear Then
IsRaw=True
Else
IsRaw=False
End If
End Function
Public Function IsFull '判断队列是否已满 '
If ((getRear+1) Mod 10)=getFront Then
IsFull=True
Else
IsFull=False
End If
End Function
End Class
以下为类实例测试代码:
'-------------------------------
' 脚本空间调用类实例
'-------------------------------
Dim TestSQ,i,str '定义变量
Set TestSQ=New SqQueue '实例对象
MsgBox TestSQ.IsRaw '测试空队列判断
MsgBox TestSQ.IsFull '测试队列溢出
MsgBox "Queue length is "&TestSQ.QueueLength '取得队列长度
TestSQ.InQueue "I" '测试进入队列
TestSQ.InQueue "L"
TestSQ.InQueue "O"
TestSQ.InQueue "V"
TestSQ.InQueue "E"
TestSQ.InQueue "Y"
TestSQ.InQueue "O"
TestSQ.InQueue "U"
MsgBox TestSQ.IsRaw '测试空队列判断
MsgBox "Queue length is "&TestSQ.QueueLength '取得队列长度
str=""
For i=1 To TestSQ.QueueLength Step 1
str=str&TestSQ.DeQueue&" " '取出队列元素并暂存
Next
MsgBox str '打印出对列内容,为FIFO
TestSQ.InitQueue '清空队列
MsgBox TestSQ.IsRaw '测试空队列判断
MsgBox TestSQ.IsFull '测试队列溢出
MsgBox "Queue length is "&TestSQ.QueueLength '取得队列长度
相关文章推荐
- 大头锻炼日记7 -- vbscript的链表队列Queue
- 大头锻炼日记1 -- vbscript实现典型线性表
- 大头锻炼日记2 -- 无指针和结构体的vbscript中实现单向链表
- 大头锻炼日记3 -- vbscript静态链表实现法
- 大头锻炼日记4 -- vbscript中的栈Stack
- 大头锻炼日记5 -- vbscript中的栈Stack(链栈)
- 使用linux中标准的queue.h头文件插件链表,队列,循环队列
- 骑行日记,回暖天晴,开始恢复锻炼 推荐
- ---记录错误日记 log------------ 队列Queue
- 20150817---成长日记1---DelayQueue&&Delayed&&Other
- uva133 The Dole Queue 循环队列模拟
- 【数据结构_队列_Queue_0965】循环队列
- I Can Guess the Data Structure!(水题,不过锻炼了stack,queue,priority的用法)
- 第一遍日记
- 黑马程序员 Java自学日记七 异常
- Implement Queue using Stacks
- leetcode——Queue Reconstruction by Height
- 运维大数据日记:故障根源分析之关联规则挖掘
- A Stack or A Queue?
- HDU 1297 Children’s Queue(递推)