队列是一种只能在表的一端进行插入运算,在表的另一端进行删除运算的线性表(头删尾插)
基于第二章数组原理类
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