// In this code I am trying to make a stack calculator. It is how a calculator actually works within a computer. In those regards I mean that two numbers are placed within the stack and then an operator is used with those two numbers. Calculators the way they are usually used are fixed with post predocution of the code. Here I do not have it that way just to simply show how a stack works.
var Node = function(_content) { // Here is where I create my node properties for the stack.
this.next = null;
this.last = null;
this.content = _content;
}
var Stack = function() { // Here is where I create the stack properties.
this.bottom = null;
this.top = null;
this.push = function(_content) { // Here is the push function
if (this.bottom == null) {
this.bottom = new Node(_content);
this.top = this.bottom;
return this;
}
var addedNode = new Node(_content);
addedNode.last = this.top; // pointer to previous node
this.top.next = addedNode; // current top points to new
this.top = addedNode; // which becomes new top
return this;
}
this.pop = function() { // Here is the pop function
if (this.bottom == null) {
alert("The Stack has less than 2 nodes, add more to the stack");
return null;
}
// Case of one node
if (this.bottom == this.top) {
var a = this.top.content;
this.bottom = null;
this.top = null;
return a;
}
// Now remove top Node
var a = this.top.content; // hold value for return
this.top = this.top.last;
this.top.next = null;
return a;
}
this.toString = function() { // Here is the function that will create the string shown on the sceen.
var str = "";
var node = this.bottom;
while (node != null) {
str += node.content + " | ";
node = node.next;
}
return str;
}
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.