Queue/Definition: Difference between revisions

Content added Content deleted
(Added Bracmat example)
(add UNIX Shell)
Line 3,265:
us
them</lang>
 
=={{header|UNIX Shell}}==
{{works with|ksh93}}
<lang bash>queue_push() {
typeset -n q=$1
shift
q+=("$@")
}
 
queue_pop() {
if queue_empty $1; then
print -u2 "queue $1 is empty"
return 1
fi
typeset -n q=$1
print "${q[0]}" # emit the value of the popped element
q=( "${q[@]:1}" ) # and remove the first element from the queue
}
 
queue_empty() {
typeset -n q=$1
(( ${#q[@]} == 0 ))
}
 
queue_peek() {
typeset -n q=$1
print "${q[0]}"
}</lang>
 
Usage:
<lang bash># any valid variable name can be used as a queue without initialization
 
queue_empty foo && echo foo is empty || echo foo is not empty
 
queue_push foo bar
queue_push foo baz
queue_push foo "element with spaces"
 
queue_empty foo && echo foo is empty || echo foo is not empty
 
print "peek: $(queue_peek foo)"; queue_pop foo
print "peek: $(queue_peek foo)"; queue_pop foo
print "peek: $(queue_peek foo)"; queue_pop foo
print "peek: $(queue_peek foo)"; queue_pop foo</lang>
 
Output:
<pre>foo is empty
foo is not empty
peek: bar
peek: baz
peek: element with spaces
peek:
queue foo is empty</pre>
 
=={{header|V}}==