Stack: Difference between revisions

→‎{{header|UNIX Shell}}: Add implementation
(→‎{{header|6502 Assembly}}: Fix offset for peek operation.)
(→‎{{header|UNIX Shell}}: Add implementation)
Line 5,917:
you
me</lang>
 
=={{header|UNIX Shell}}==
{{works with|Bourne Again SHell}}
{{works with|Zsh}}
{{works with|Korn Shell}}
 
<lang sh>init() { stack=(); }
 
push() {
local item=$1
stack=("$item" "${stack[@]}")
}
 
stack_top() {
# this approach sidesteps zsh indexing difference
set -- "${stack[@]}"
printf '%s\n' "$1"
}
 
pop() {
stack_top
stack=("${stack[@]:1}")
}
 
empty() {
(( ${#stack[@]} == 0 ))
}
 
# Demo
push fred; push wilma; push betty; push barney
printf 'peek(stack)==%s\n' "$(stack_top)"
while ! empty; do
pop
done</lang>
 
{{Out}}
<pre>peek(stack)==barney
barney
betty
wilma
fred</pre>
 
=={{header|VBA}}==
1,480

edits