Stack Calculator COMPLETE
by aren_anderson
HTML
<h3>
Enter a Number
</h3>
<div>
<input type="textbox" id="tbStack" value="Enter a Number"/>
<input type="button" id="btnPush" value="Push to Stack" onclick="pushToStack()"/>
<br/>
<br/>
<input type="button" id="btnPop" value="Pop!" onclick="pop()"/>
</div>
<div id="output"></div>
<div id="output1"></div>
JavaScript
//Node object
Node = function(){
this.content = null;
this.next = null;
}
//Stack object
Stack = function(){
var head = null;
var size = null;
var top = null;
//push function
this.push = function(content){
var node = new Node();
node.content = content;
node.next = null;
if(size<1 && head == null){
head = node;
head.next = null;
size = 1;
}
else{
var current = head;
while(current.next != null){
current = current.next;
}
current.next = node;
size += 1;
}
}
//pop function
this.pop = function(){
var current = head;
if(size == 0){
return;
}
if(size == 1){
head = null;
size = 0;
return current;
}
var previous = current;
while(current.next != null){
previous = current;
current = current.next;
}
previous.next = null;
size -= 1;
return current;
}
//check if the stack is empty
this.stackEmpty = function(){
if (size < 1){
document.getElementById("output").innerHTML = "The Stack is Empty!";
}
}
//check for top value in stack
this.top = function(){
var current = head;
if(size > 0 && head != null){
while(current.next != null){
current = current.next;
}
return current.content;
}
else{
out += "There is no item in the stack";
return null;
}
}
//function to print stack
this.print = function(){
var current = head;
var top = stack.top();
if (current.content != "+","*","-","/"){
out = current.content + ", ";
}
out1 = "";
while(current.next != null){
if(current.content != "+","*","-","/"){
current = current.next;
out += current.content + ", ";
}
}
//perform mathematical operations
if (current.content == "+"){
stack.pop();
var n1 = stack.pop();
var n2 = stack.pop();
var n3 = parseInt(n1.content);
var n4 = parseInt(n2.content);
answer = n3 + n4;
stack.push(answer);
out...