JSFiddle - React, Tailwind, and code Playground
by Nyle Anderson
HTML
Input value:
<input type="textbox" id="value" value="" />
<input type="button" id="enter" value="Push" onClick="addToStack()" />
<input type="button" id="enter" value="Pop" onClick="popFromStack()" />
<p id = "output">
</p>
JavaScript
//working stack
var Node = function(_content) {
this.above=null;
this.below=null;
this.content=_content;
}
var Stack = function() {
this.top=null;
this.height = 0;
}
Stack.prototype.push = function(_content) {
var n = new Node(_content);
if (this.top == null) {
this.top = n;
this.height++;
return;
}
this.top.above = n;
n.below = this.top;
this.top = n;
this.height++;
}
Stack.prototype.pop = function() {
if (this.top == null) return null;
if (this.top.below == null) {
var temp = this.top;
this.top = null;
return temp;
}
var temp = this.top;
this.top.below.above = null;
this.top = this.top.below;
return temp;
}
Stack.prototype.asString = function(){
if (this.top == null) return "Empty";
var s = "";
var n = this.top;
while (n != null) {
s += n.content + " : <br>";
n = n.below;
}
return s;
}
var stack = new Stack();
function addToStack() {
var c = document.getElementById("value").value;
stack.push(c);
document.getElementById("output").innerHTML = stack.asString();
}
function popFromStack() {
stack.pop();
document.getElementById("output").innerHTML = stack.asString();
}