|
第一次在GA发主题帖,有点紧张....
这是研究中途弄的一点东西,看到最近大家都在研究数组存储,所以发上来一起交流下.
第一个是动态栈,第二个是动态队列.由于我自己使用不需要太大尺度的栈和队列,所以直接就用了一个数组...嗯,所以在通用性上还有所限制.
另外,由于时间关系,只是简单测试了一下.不保证逻辑结构完全正确,不过想必大家应该能看懂我的思路吧.
[jass]//多栈共享存储空间--可动态分配的栈
globals
integer array StackData
integer array StackPriorElementPointer
integer array StackTop
endglobals
//! textmacro Get_Rec takes NAME
globals
integer $NAME$Top=1
integer array $NAME$Pointer
boolean array $NAME$IsBeingUsed
endglobals
function $NAME$Get_Int takes nothing returns integer
local integer temp=$NAME$Pointer[$NAME$Top]
if temp==0 then
set temp=$NAME$Top
endif
set $NAME$Top=$NAME$Top+1
if temp>8191 then
return 0
endif
set $NAME$IsBeingUsed[temp]=true
return temp
endfunction
function $NAME$Rec_Int takes integer i returns nothing
set $NAME$IsBeingUsed=false
set $NAME$Top=$NAME$Top-1
set $NAME$Pointer[$NAME$Top]=i
endfunction
//! endtextmacro
//! runtextmacro Get_Rec("StackData")
//! runtextmacro Get_Rec("StackName")
function CreateStack takes nothing returns integer
local integer stack=StackNameGet_Int()
if StackNameIsBeingUsed[stack] then
set StackTop[stack]=0
return stack
endif
return 0
endfunction
function IsStackEmpty takes integer stack returns boolean
if StackNameIsBeingUsed[stack] then
return StackTop[stack]==0
endif
return true
endfunction
function GetStackLength takes integer stack returns integer
local integer top
local integer length=0
if StackNameIsBeingUsed[stack] then
set top=StackTop[stack]
loop
exitwhen top==0
set length=length+1
set top=StackPriorElementPointer[top]
endloop
return length
endif
return 0
endfunction
//压栈
function Push takes integer stack, integer i returns nothing
local integer top
local integer next
if StackNameIsBeingUsed[stack] then
set top=StackTop[stack]
set next=StackDataGet_Int()
if StackDataIsBeingUsed[next] then
set StackPriorElementPointer[next]=top
set StackTop[stack]=next
set StackData[next]=i
endif
endif
endfunction
//退栈
function Pop takes integer stack returns integer
local integer top=StackTop[stack]
local integer prior
if (StackNameIsBeingUsed[stack] and top!=0) then
set prior=StackPriorElementPointer[top]
call StackDataRec_Int(top)
set StackTop[stack]=prior
return StackData[top]
endif
return -174
endfunction
//取得栈顶元素的值
function GetTop takes integer stack returns integer
local integer top=StackTop[stack]
if (StackNameIsBeingUsed[stack] and top>0) then
return StackData[top]
endif
return -174
endfunction
function ClearStack takes integer stack returns nothing
local integer top
local integer prior
if (not StackNameIsBeingUsed[stack]) then
return
endif
set top=StackTop[stack]
loop
exitwhen top==0
set prior=StackPriorElementPointer[top]
call StackDataRec_Int(top)
set top=prior
endloop
set StackTop[stack]=0
endfunction
//删除栈
function DestroyStack takes integer stack returns nothing
local integer top
local integer prior
if (not StackNameIsBeingUsed[stack]) then
return
endif
set top=StackTop[stack]
loop
exitwhen top==0
set prior=StackPriorElementPointer[top]
call StackDataRec_Int(top)
set top=prior
endloop
call StackNameRec_Int(stack)
endfunction[/jass]
下面是队列的代码:
[jass]//多队列共享存储空间--可动态分配的队列
globals
integer array QueueData
integer array QueueNextElementPointer
integer array QueueHead
integer array QueueEnd
endglobals
//! textmacro Get_Rec takes NAME
globals
integer $NAME$Top=1
integer array $NAME$Pointer
boolean array $NAME$IsBeingUsed
endglobals
function $NAME$Get_Int takes nothing returns integer
local integer temp=$NAME$Pointer[$NAME$Top]
if temp==0 then
set temp=$NAME$Top
endif
set $NAME$Top=$NAME$Top+1
if temp>8191 then
return -1
endif
set $NAME$IsBeingUsed[temp]=true
return temp
endfunction
function $NAME$Rec_Int takes integer i returns nothing
set $NAME$IsBeingUsed=false
set $NAME$Top=$NAME$Top-1
set $NAME$Pointer[$NAME$Top]=i
endfunction
//! endtextmacro
//! runtextmacro Get_Rec("QueueData")
//! runtextmacro Get_Rec("QueueName")
//创建一个空队列
function CreateQueue takes nothing returns integer
local integer queue=QueueNameGet_Int()
local integer head
if QueueNameIsBeingUsed[queue] then
set head=QueueDataGet_Int()
if QueueDataIsBeingUsed[head] then
set QueueHead[queue]=head
set QueueEnd[queue]=head
return queue
endif
endif
return 0
endfunction
//清空一个队列
function ClearQueue takes integer queue returns nothing
local integer head
local integer end
local integer temp
if (not QueueNameIsBeingUsed[queue]) then
return
endif
set head=QueueHead[queue]
set end=QueueEnd[queue]
set temp=head
loop
exitwhen temp==end
set temp=QueueNextElementPointer[temp]
call QueueDataRec_Int(head)
endloop
set QueueEnd[queue]=head
endfunction
//判断一个队列是不是为空
function IsQueueEmpty takes integer queue returns boolean
if QueueNameIsBeingUsed[queue] then
return QueueHead[queue]==QueueEnd[queue]
endif
return true
endfunction
//获取队列长度,因为感觉这个函数不是必须得,所以没有单独用变量存储长度
function GetQueueLength takes integer queue returns integer
local integer head
local integer end
local integer length
if QueueNameIsBeingUsed[queue] then
set head=QueueHead[queue]
set end=QueueEnd[queue]
loop
exitwhen head==end
set length=length+1
set head=QueueNextElementPointer[head]
endloop
return length
endif
return 0
endfunction
//入列
function EnQueue takes integer queue, integer i returns nothing
local integer end
local integer next
if QueueNameIsBeingUsed[queue] then
set end=QueueEnd[queue]
set next=QueueDataGet_Int()
if QueueDataIsBeingUsed[next] then
set QueueNextElementPointer[end]=next
set QueueEnd[queue]=next
set QueueData[next]=i
endif
endif
endfunction
//出列
function DeQueue takes integer queue returns integer
local integer head=QueueHead[queue]
local integer end=QueueEnd[queue]
local integer next
if (QueueNameIsBeingUsed[queue] and head!=end) then
call QueueDataRec_Int(head)
set next=QueueNextElementPointer[head]
set QueueHead[queue]=next
return QueueData[next]
else
return -174
endif
endfunction
//取得队列首元素的值
function GetHead takes integer queue returns integer
local integer head=QueueHead[queue]
local integer end=QueueEnd[queue]
if (QueueNameIsBeingUsed[queue] and head!=end) then
return QueueData[QueueNextElementPointer[head]]
else
return -174
endif
endfunction
//删除队列
function DestroyQueue takes integer queue returns nothing
local integer head
local integer end
local integer next
if (not QueueNameIsBeingUsed[queue]) then
return
endif
set head=QueueHead[queue]
set end=QueueEnd[queue]
loop
exitwhen head==end
set next=QueueNextElementPointer[head]
call QueueDataRec_Int(head)
set head=next
endloop
call QueueNameRec_Int(queue)
endfunction[/jass]
最后简单来个2-10之间的进制转换,说明下栈的用法.至于队列...还想不出啥例子来..
[jass]//进制转换,输出结果为十进制整数,可以转成字符串看
function Conversion takes integer a, integer b returns integer
local integer temp
local integer result=a
local integer forLoopA=0
local integer stack=CreateStack()
loop
exitwhen result/b==0
set temp=result-result/b*b
set result=result/b
call Push(stack,temp)
set forLoopA=forLoopA+1
endloop
call Push(stack,result)
set result=0
set temp=10
loop
exitwhen IsStackEmpty(stack)
set result=result*temp+Pop(stack)
set forLoopA=forLoopA+1
endloop
call DestroyStack(stack)
return result
endfunction[/jass] |
评分
-
查看全部评分
|