Queue/Definition: Difference between revisions

Content added Content deleted
No edit summary
Line 366: Line 366:


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
{{incorrect|AutoHotkey|It implements a stack (LIFO), not a queue (FIFO).}}
<lang AutoHotkey>; TEST: push 2, 4 and 6 onto queue named "qu"
push("qu", 2), push("qu", 4), push("qu", 6)


While !empty("qu") ; Repeat until queue is not empty
MsgBox % pop("qu") ; Print popped values (2, 4, 6)
MsgBox % pop("qu") ; 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(stack,x) { ; push x onto stack named "stack"
push(queue,x) { ; push x onto queue named "queue"
Local _ ;
;---------------------------------------------------------------------------
_ := %stack%0 := %stack%0="" ? 1 : %stack%0+1
global
%stack%%_% := x
If (%queue% = "")
%queue% := x
Else
%queue% .= "|" x
}
}

pop(stack) { ; pop value from stack named "stack"

Local _ ;
;---------------------------------------------------------------------------
_ := %stack%0
pop(queue) { ; pop value from queue named "queue"
If (_ < 1)
;---------------------------------------------------------------------------
Return "", ErrorLevel := 1
global
Return %stack%%_%, %stack%0 := _-1
StringSplit, Array, %queue%, |
If (Array0 = 0) {
ErrorLevel := -1,
Return, ""
}
If (Array0 = 1)
%queue% := ""
Else
%queue% := SubStr(%queue%, InStr(%queue%, "|") + 1)
Return, Array1
}


;---------------------------------------------------------------------------
empty(queue) { ; check if queue named "queue" is empty
;---------------------------------------------------------------------------
global
Return, %queue% = ""
}
}
</lang>
empty(stack) { ; check if stack named "stack" is empty
Global
Return %stack%0<1
}</lang>


=={{header|C}}==
=={{header|C}}==