Javascript Stack
Stack implementation in javascript
by infiniteloops
JavaScript
function StackNode(value) {
this.data = value;
this.next = undefined;
}
function Stack() {
this.head = undefined;
this.push = function(item) {
var node = new StackNode(item);
node.next = this.head;
this.head = node;
};
this.pop = function() {
var node = this.head;
if (node !== undefined) {
this.head = node.next;
}
return node.data;
};
this.isEmpty = function() {
return this.head === undefined;
};
}
var stackObj = new Stack();
for (var i = 0; i < 10; i++) {
stackObj.push(i);
}
while(!stackObj.isEmpty()){
var item = stackObj.pop();
$('p').append(item);
}