var stack var head -1 function isEmpty return head -1 function push if

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
var stack = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
var head = -1;
function isEmpty() {
return head == -1;
}
function push(x) {
if (head + 1 >= stack.length) {
console.log("Stack overflow");
return;
}
stack[head++] = x;
}
function pop(x) {
if (!isEmpty()) {
return stack[head--];
}
console.log("EmptyStack");
}