Techno World Inc - The Best Technical Encyclopedia Online!

THE TECHNO CLUB [ TECHNOWORLDINC.COM ] => Shell Script => Topic started by: Mark David on March 16, 2007, 01:02:31 PM



Title: queue and stack using array
Post by: Mark David on March 16, 2007, 01:02:31 PM
queue and stack using array

here is a series of operation on array, we can use these functions to implement a queue or stack that can help us more

Code:
push: 
array=("${array[@]}" $new_element)

pop:
array=(${array[@]:0:$((${#array[@]}-1))})

shift:
array=(${array[@]:1})

unshift
array=($new_element "${array[@]}")

function del_array {
local i
for (( i = 0 ; i < ${#array[@]} ; i++ ))
do
if [ "$1" = "${array[$i]}" ] ;then
break
fi
done
del_array_index $i
}

function del_array_index {
array=(${array[@]:0:$1} ${array[@]:$(($1 + 1))})
}