Sample Queue Functions
by scotp71
HTML
<input type="button" value="Go" onclick="alert(doFun([1,2,3,4,5,6]));" />
<input type="button" value="Go" onclick="alert(doFun2(15))" />
<input type="button" value="Go" onclick="alert(doFun3([1,2,3,4]));" />
JavaScript
function doFun(q) {
// Enqueue is equivalent to push, Dequeue is equivalent to shift
var s = [];
while(q.length != 0) {s.push(q.shift());}
while(s.length != 0) {q.push(s.pop());}
return q;
}
function doFun2(n) {
// Enqueue is equivalent to push, Dequeue is equivalent to shift
var q = [];
q.push(0); q.push(1);
for (var i = 0; i < n; i++ ){
var a = q.shift();
var b = q.shift();
q.push(b);
q.push(a + b);
}
return q.pop();
}
function doFun3(q) {
// Enqueue is equivalent to push, Dequeue is equivalent to shift
if (q.length > 0) {
var i = q.shift();
doFun3(q);
q.push(i);
}
return q;
}