Stack Computer
by aren_anderson
HTML
<input type="textbox" id="tbStack" value="Enter a Number"/>
<input type="button" id="btnPush" value="Push to Stack" onclick="push()"/>
<p id="output1"/>
JavaScript
var Node = function(_content){
this.next = null;
this.last = null;
this.content = _content;
}
var Stack = function(){
this.bottom = null;
this.top = null;
this.push = function(_content){
if (this.bottom == null){
this.bottom = new Node(_content);
this.top = this.bottom;
return this;
}
var addedNode = new Node(_content);
addedNode.last = this.top;
this.top.next = addedNode;
this.top = addedNode;
return this;
}
this.pop = function(){
if (this.bottom == null) {
alert("The Stack is Empty");
return null;
}
if (this.bottom == this.top){
//YOU MUST IMPLEMENT THIS
alert("You must implement this case");
return this.top;
}
var a = this.top;
this.top = this.top.last;
this.top.next = null;
return a;
}
this.toString = function(){
var str = "";
var node = this.bottom;
while (node != null){
str += node.content + ":";
node = node.next;
}
return str;
}
}
var stack = new Stack();
document.getElementById("output1").innerHTML = "Contents of Stack: " + stack.toString();