您的位置:首页 > 产品设计 > UI/UE

大头锻炼日记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    '取得队列长度
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息