A5
by Ebony McCoy
HTML
<input type="textbox" id="userinput">
<input type="button" value="Push to stack" id='push' onClick=isNumber()>
<input type="button" id='calculate' onClick=isOperator() value="Calculate">
<br>
<br>
<br>
<input type="button" id='pop' onClick=pop() value="Pop from stack">
<p id='output1'></p>
JavaScript
var DoublyList = new LinkedList();
var Node = function() {
this.next = null;
this.last = null;
this.value = _value;
}
// create stack
var Stack = function() {
this.bottom = null;
this.top = null;
return this;
}
LinkedList.prototype.push = function(_value) {
if (this.bottom == null) {
this.bottom = new Node(_value);
this.top = this.bottom;
return this;
}
var addNode = new Node(_value);
addedNode.last = this.top;
this.top.next = addedNode;
this.top = addedNode;
return this;
}
this.pop = function() {
if (this.bottom == null) {
alert("This stack is empty!");
return null;
}
if (this.bottom == this.top) {
this.bottom = null;
this.top = null;
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.top;
while (node != null) {
str += node.value + "<br>";
node = node.last;
}
return str;
}
function pop() {
document.getElementById('output1').innerHTML = "" + stack.toString();
stack.pop();
stack.pop();
}
var stack = new Stack();
function isNumber(_value) {
var i = document.getElementById('userinput').value;
alert(i);
if (isNaN(i)) {
alert('Input must only be a number or an operator (+, -, *, /)');
document.getElementById('userinput').value = '';
return false;
}
function isOperator(_value) {
var x, y, result
}
if (stack.top.value == "+") {
x = parseInt(stack.bottom.value);
y = parseInt(stack.bottom.next);
result(x + y);
} else if (stack.top.value == "-") {
x = parseInt(stack.bottom.value);
y = parseInt(stack.bottom.next);
result(x - y);
} else if (stack.top.value == "*") {
x = parseInt(stack.bottom.value);
y = parseInt(stack.bottom.next);
result(x * y);
} else if (top.value == "/") {
x = parseInt(stack.bottom.value);
...