Queue/Definition: Difference between revisions

No edit summary
Line 366:
 
=={{header|AutoHotkey}}==
{{incorrect|<lang AutoHotkey|It>; implementsTEST: apush stack (LIFO)2, not4 aand 6 onto queue (FIFO).}}named "qu"
push("qu", 2), push("qu", 4), push("qu", 6)
 
While !empty("stqu") ; Repeat until stackqueue is not empty
MsgBox % pop("stqu") ; Print popped values (2, 4, 26)
MsgBox % pop("stqu") ; Empty
MsgBox %ErrorLevel% ; ErrorLevel = -1: popped too much
 
ahk forum: [http://www.autohotkey.com/forum/viewtopic.php?t=44657&postdays=0&postorder=asc&start=132 discussion]
<lang AutoHotkey>push("st",2),push("st",4) ; TEST: push 2 and 4 onto stack named "st"
While !empty("st") ; Repeat until stack is not empty
MsgBox % pop("st") ; Print popped values (4, 2)
MsgBox % pop("st") ; Empty
MsgBox %ErrorLevel% ; ErrorLevel = 1: popped too much
 
;---------------------------------------------------------------------------
push(stackqueue,x) { ; push x onto stackqueue named "stackqueue"
Local _ ;
;---------------------------------------------------------------------------
_ := %stack%0 := %stack%0="" ? 1 : %stack%0+1
global
%stack%%_% := x
If (_%queue% <= 1"")
%stack%%_queue% := x
Else
%queue% .= "|" x
}
 
pop(stack) { ; pop value from stack named "stack"
 
Local _ ;
;---------------------------------------------------------------------------
_ := %stack%0
pop(stackqueue) { ; pop value from stackqueue named "stackqueue"
If (_ < 1)
;---------------------------------------------------------------------------
Return "", ErrorLevel := 1
global
Return %stack%%_%, %stack%0 := _-1
StringSplit, Array, %queue%, |
If (Array0 = 0) {
Return "", ErrorLevel := -1,
Return, ""
}
If (Array0 = 1)
%queue% := ""
Else
%queue% := SubStr(%queue%, InStr(%queue%, "|") + 1)
Return, %stack%0<1Array1
}
 
 
;---------------------------------------------------------------------------
empty(stackqueue) { ; check if stackqueue named "stackqueue" is empty
;---------------------------------------------------------------------------
global
Return %stack%%_%, %stackqueue%0 := _-1""
}
}</lang>
empty(stack) { ; check if stack named "stack" is empty
Global
Return %stack%0<1
}</lang>
 
=={{header|C}}==
138

edits