Caché 实操《Caché 算法与数据结构-循环列表》学习笔记

参考书 第六章 Caché 算法与数据结构 循环队列_yaoxin521123的博客-CSDN博客

用数组实现循环列表,基于第二章创建的数组类定义。

第二章 数组原理参考书

第二章 Caché 算法与数据结构 数组原理_yaoxin521123的博客-CSDN博客

Caché 实操《Caché 算法与数据结构-数组原理》学习笔记_唐颖萱的博客-CSDN博客

在实操循环列队前,务必知晓其含义  循环队列_百度百科

本人Cache'小白,欢迎指教和交流。本文为学习原作者的代码进行代码重现后的总结精炼。使用数据库:InterSystems IRIS。 

目录

循环队列关键知识点

示例实操

首先写数组类

然后写循环队列类

 最后写调用类

运行结果


循环队列关键知识点

        循环队列:为充分利用向量空间,克服"假溢出"现象的方法是:将向量空间想象为一个首尾相接的圆环,并称这种向量为循环向量。存储在其中的队列称为循环队列(Circular Queue)。循环队列是把顺序队列首尾相连,把存储队列元素的表从逻辑上看成一个环,成为循环队列。

        循环队列就是将队列存储空间的最后一个位置绕到第一个位置,形成逻辑上的环状空间,供队列循环使用。在循环队列结构中,当存储空间的最后一个位置已被使用而再要进入队运算时,只需要存储空间的第一个位置空闲,便可将元素加入到第一个位置,即将存储空间的第一个位置作为队尾。 循环队列可以更简单防止伪溢出的发生,但队列大小是固定的。

     在循环队列中,当队列为空时,有front=rear,而当所有队列空间全占满时,也有front=rear。

     为了区别这两种情况,规定循环队列最多只能有Capacity -1个队列元素当循环队列中只剩下一个空存储单元时,队列就已经满了。

        因此,队列判空的条件是 front = rear

q:(..front=..rear"循环队列空了"

        队列判满的条件是 front = (rear+1)#MaxSize

q:( (..rear+1) #..array.length() = ..front )   "当前循环队列满了!"

示例实操

首先写数组类

基于第二章数组类的定义。

Class PHA.TYX.Arithmetic.Array Extends %RegisteredObject
 
{
 
Property array [ InitialExpression = {[]} ];
 
Property size [ InitialExpression = 0 ];
 
Property length [ InitialExpression = 0 ];
 
 
Method init (capacity As %Integer)                      //初始化数组
{
	d ..array.%Set(capacity - 1 ,"")                    /*注意 capacity-1 */
	s ..length = capacity
	s ..size = 0
}
 
Method insert(index As %Integer,element As %Integer) {}  //插入数据
 
Method get(capacity As %Integer) {}                      //获取数组下标的值
 
Method set(index As %Integer,element As %Integer) {}     //给数组下标赋值
 
Method length() {}
 
Method output() {}
 
Method delete(index As %Integer) As %Integer {}     
 
Method resize()  {}                                       //给数组扩容
 
}

然后写循环队列类

( ( 这里要多看。。

/// Function:循环列表类,基于数组原理实现
/// CreateDate:2022-8-25
/// Creator:tangyingxuan
Class PHA.TYX.Arithmetic.CircularQueue Extends %RegisteredObject
{

Property array As PHA.TYX.Arithmetic.Array;

Property front As %Integer [ InitialExpression = ];

Property rear As %Integer [ InitialExpression = ];

Method %OnNew(capacity As %Integer) As %Status [ Private, ServerOnly = 1 ]
{
        s $this.array = ##class(PHA.TYX.Arithmetic.Array).%New()
        d $this.array.init(capacity)
        q $$$OK
}

/* 往循环列表增加数据 */
Method enQueue(element As %Integer)
{
//前面这一长串怎么会等于front呢?为什么等于front后要quit出去??

//解答:因为是循环列表呀!去百度百科看一下循环列表含义。

//这里原作者写错了!害我理解了半天,应该是循环列表满了,直接quit出去。
        q:((..rear+1)#..array.length()=..front"当前循环队列满了!"                                    
        ..array.set(..rear,element)                             //从尾部增加元素
        ..rear = (..rear+1)#..array.length()               
}

/*删除数据*/
Method deQueue()
{
        q:(..front=..rear) "循环队列空了"
        s deQueueElement=..array.get(..front)
        s ..front=(..front+1)#..array.length()
        q deQueueElement
}

Method output()
{
        s = ..front
        while '= ..rear{
        w ..array.get(i),!
        s i=(i+1)#..array.length()
}
}

 最后写调用类

/// Function:调用循环队列,第四章:循环队列
/// CreateDate:2022-8-25
/// Creator:tangyingxuan
/// Resource:https://blog.csdn.net/yaoxin521123/article/details/106679761
/// w ##class(PHA.TYX.Arithmetic).CircularQueue()
ClassMethod CircularQueue()
{
        //capacity=6 实际只能存5个数字
        #dim circularQueue as PHA.TYX.Arithmetic.CircularQueue =         ##class(PHA.TYX.Arithmetic.CircularQueue).%New(6)

        d circularQueue.enQueue(1)
        d circularQueue.enQueue(2)
        d circularQueue.enQueue(3)
        d circularQueue.enQueue(4)
        d circularQueue.enQueue(5)
        //b ;1
        //d circularQueue.enQueue(6)            //   “当前循环队列满了!”
        //b ;2
        //w circularQueue.deQueue(),!
        w circularQueue.deQueue(),!
        w circularQueue.deQueue(),!
        w circularQueue.deQueue(),!

        d circularQueue.enQueue(77)
        d circularQueue.enQueue(88)
        d circularQueue.enQueue(99)


        d circularQueue.output()
        q ""
}

运行结果

USER>w ##class(PHA.TYX.Arithmetic).CircularQueue()
1
2
3
4
5
77
88
99

 本人Cache'小白,如有错误,欢迎指正。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值