A5(forked from a4)
by Ebony McCoy
HTML
<br>
<br/>Enter Content Here:
<input type="textbox" id="_content" />
<input type="button" value="Push Node to List" onclick= isNumber() />
<input type="button" value="Pop Results" onclick="pop()" />
<br/>
<p id="output"></p>
JavaScript
var stack = new Stack();
function Node() {
this.content = _content;
this.last = null;
this.next = null;
return this;
}
function Stack() {
this.bottom = null;
this.top = null;
this.length = 0;
Stack.prototype.push = function(_content) {
var node = new Node(_content);
if (this.bottom === null) {
// if there is not head,set node to heade of list
this.bottom = node;
this.length += 1;
return node;
}
};
LinkedList.prtototype.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.bottom;
this.bottom = this.bottom.last;
this.bottom.next = null;
return a;
};
Stack.prototype.toString = function() {
var str = "";
var node = this.head;
while (node !== null) {
str += node.content + ":";
node = node.next;
}
return str;
};
var stack = new Stack();
function isNumber(_content) {
var i = document.getElementById('userinput').content;
if (isNaN(i)) {
alert('Input must only be a number or an operator (+, -, *, /)');
document.getElementById('userinput').content = '';
return false;
}
function pop() {
document.getElementById('output1').innerHTML = "" + Stack.toString();
Stack.pop();
}
// function isOperator(_content) {
var x, y, result;
if (Stack.top.content == "+") {
x = parseInt(Stack.bottom._content);
y = parseInt(Stack.bottom.next);
result(x + y);
} else if (Stack.top.content == "-") {
x = parseInt(Stack.bottom._content);
y = parseInt(Stack.bottom.next);
result(x - y);
} else if (Stack.top.content == "*") {
x = parseInt(Stack.bottom._content);
y = parseInt(Stack.bottom.next);
result(x * y);
} else if (top.content == "/")...