Stack Implementation
by joshpauljohnson
HTML
<section>
<header>
<h1>Stack Implementation</h1>
</header>
<input id="push" type="button" value="Add Item"/>
<input id="pop" type="button" value="Remove Item"/>
<article><ul id="out"></ul></article>
</section>
JavaScript
var Stack = function() {
var Node = function(val, nextNode) {
this.val = val;
this.next = nextNode;
};
var first = null;
var getNext = function(node) {
return node === null ? null : node.next;
};
this.push = function(item) {
if (first === null) {
first = new Node(item, null);
} else {
first = new Node(item, first);
}
};
this.pop = function() {
first = first.next;
return first;
};
};
var stack = new Stack();
var itemNum = 0;
printStack(stack);
$("#push").click(function() {
var item = "Item "+ ++itemNum;
stack.push(item);
$("#out").append('<li>'+item+'</li>');
});
$("#pop").click(function() {
stack.pop();
$("#out li:first").remove();
});