Assignment 5
The Stack
by Nyle Anderson
HTML
Input value:
<input type="textbox" id="value" Value="1" />
<input type="button" id="enter" value="Enter" onClick="stackFunction" />
<p id="output">
</p>
JavaScript
var Node = function(_content) {
this.next = null;
this.last = null;
this.content = _content;
}
var Stack = fucntion()
this.head = null;
this.top = null;
this.push = function(_content) {
if (this.head == null) {
this.head = new Node(_content);
this.top = this.head;
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.head == null) {
alert("The stack is empty");
return null
}
var a = this.top;
this.top = this.top.last;
this.top.next = null;
return a
}
this.toString = function() {
var str = "";
var node = this.head;
while (node != null) {
str += node.content + ":";
node = node.next;
return string
}
function stackFunction()
var con = document.getElementById("value").value;
var a = stack.pop();
var b = stack.pop();
var c = a * b;
stack.push(c);
stack.push(con);
//operators
if (stack.top.content == "/") {
stack.pop();
stack.division();
} else if (stack.top.content == "*")
stack.pop();
stack.multiply();
else if (stack.top.content == "+")
stack.pop();
stack.addition();
else(stack.top.content == "-") {
stack.pop();
stack.sub();
}
document.getElementById('output1').innerHTML = 'current operands ' + stack.toString();
}
var stack = new Stack();
stack.push('A');
stack.push('B');
stack.push('C');
stack.push('D');
stack.push('E');