JSFiddle - React, Tailwind, and code Playground
by zazagalaxy
HTML
<p id='output1'></p>
<p id='output2>'></p>
JavaScript
var Node = function(_content) {
this.next = null;
this.last = null;
this.content = _content;
}
var Stack = function() {
this.bottom = null;
this.top = null;
this.push = function(_content) {
if (this.bottom == null) {
this.bottom = new Node(_content);
this.top = this.bottom;
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;
}
if (this.bottom == this.top) {
alert("You must implement this case");
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.bottom;
while (node != null) {
str += node.content + ":";
node = node.next;
}
return str;
}
}
var stack = new Stack();
stack.push('A');
stack.push('B');
stack.push('C');
stack.push('D');
stack.push('E');
document.getElementById('output1').innerHTML = 'Push some nodes ' + stack.toString();
stack.pop();
stack.pop();
document.getElementById('output2').innerHTML = 'Pop some objects: ' + stack.toString();