第六章 Caché 算法与数据结构 循环队列

文章目录

第六章 Caché 算法与数据结构 循环队列

数组循环队列

队列是一种只能在表的一端进行插入运算,在表的另一端进行删除运算的线性表(头删尾插)

数组类

基于第二章数组原理类

Class PHA.YX.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 ,"")
	s ..length = capacity
	s ..size = 0
}

Method insert(index As %Integer, element As %Integer)
{
	/* 超出数组实际元素范围 */
	i (index < 0) || (index > ..size) d
	.throw ##class(PHA.COM.MOB.Exception).%New("超出数组实际元素范围!")
	
	/* 超出数组实际元素范围或扩容 */
	i ..size >= ..array.%Size() d
	.throw ##class(PHA.COM.MOB.Exception).%New("超出数组实际元素范围!")
	.;d ..resize()
	
	/* 从右向左,将元素逐个向右挪一位 */
	f i = (..array.%Size() - 1) : -1 : index d
	.d ..array.%Set(i + 1, ..array.%Get(i))
	
	/* 腾出位置放新元素 */
	d ..array.%Set(index, element)
	
	/* 保持数组长度 */
	d ..array.%Remove(..length)
	
	/* 长度增一 */
	s ..size = ..size + 1
}

/// 获取数组下标的值
Method get(index As %Integer)
{
	q ..array.%Get(index)
}

/// 给数组下标赋值
Method set(index As %Integer, element As %Integer)
{
	q ..array.%Set(index, element)
}

/// 获取数组长度
Method length()
{
	q ..length
}

Method output()
{
	/* 遍历输出结果 */
	f i = 0 : 1 : ..size d
	.w ..array.%Get(i),!
}

Method delete(index As %Integer) As %Integer
{
	/* 超出数组实际元素范围 */
	if (index < 0 )||(index >= ..size) d
	.throw ##class(PHA.COM.MOB.Exception).%New("超出数组实际元素范围!")
	
	/* 从左向右,将元素逐个向左挪一位 */
	s deletedElement = ..array.%Get(index)
	f i = index : 1 : ..size - 1 d
	.d ..array.%Set(i, ..array.%Get(i + 1))
	
	/* 长度减一 */
	s ..size = ..size -1
	
	/* 返回删除的元素 */
	q deletedElement
}

Method resize()
{
	/* 数组扩容为原来二倍 */
	s arrayNew = []
	d arrayNew.%Set(..array.%Size() * 2 - 1 ,"")
	
	/* 给扩容数组重新赋值 */
	f i = 0 : 1 : (..array.%Size() * 2 - 1)  d
	.d arrayNew.%Set(i, ..array.%Get(i))
	
	/* 把扩容数组赋值给原数组 */
	s ..array = arrayNew
	
	/* 重新赋值长度 */
	s ..length = ..array.%Size()
}

}

循环链表类

Class PHA.YX.Arithmetic.CircularQueue Extends %RegisteredObject
{

Property array As PHA.YX.Arithmetic.Array;

Property front As %Integer [ InitialExpression = 0 ];

Property rear As %Integer [ InitialExpression = 0 ];

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

Method enQueue(element As %Integer)
{
	i ((..rear + 1) # ..array.length()) = ..front d
	.throw ##class(PHA.COM.MOB.Exception).%New("当前队列为空!")
	d ..array.set(..rear,element)
	s ..rear = (..rear + 1) # ..array.length()
}

Method deQueue()
{
	i (..rear = ..front)
	.throw ##class(PHA.COM.MOB.Exception).%New("队列已空!")
	s deQueueElement = ..array.get(..front)
	s ..front = (..front + 1) # ..array.length()
	q deQueueElement
}

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

	}
}

}

调用

/// w ##class(PHA.YX.Arithmetic).CircularQueue()
ClassMethod CircularQueue()
{
	s $zt = "ErrCircularQueue"
	#dim circularQueue as PHA.YX.Arithmetic.CircularQueue = ##class(PHA.YX.Arithmetic.CircularQueue).%New(6)
	d circularQueue.enQueue(3)
	d circularQueue.enQueue(5)
	d circularQueue.enQueue(6)
	d circularQueue.enQueue(8)
	d circularQueue.enQueue(1)
	w circularQueue.deQueue(),!
	w circularQueue.deQueue(),!
	w circularQueue.deQueue(),!
	d circularQueue.enQueue(2)
	d circularQueue.enQueue(4)
	d circularQueue.enQueue(9)
	d circularQueue.output()
	q ""
ErrCircularQueue
	q $ze
}
DHC-APP>w ##class(PHA.YX.Arithmetic).CircularQueue()
3
5
6
8
1
2
4
9
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yaoxin521123

谢谢您的支持!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值