Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
25
Task/Queue-Definition/UNIX-Shell/queue-definition-1.sh
Normal file
25
Task/Queue-Definition/UNIX-Shell/queue-definition-1.sh
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
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]}"
|
||||
}
|
||||
14
Task/Queue-Definition/UNIX-Shell/queue-definition-2.sh
Normal file
14
Task/Queue-Definition/UNIX-Shell/queue-definition-2.sh
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
# 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue