<html>
<body>
<h1>The Stack</h1>
Enter first number and push the "Add Number" button, enter second number the same.
<br/>
<br/>
<input type="number" id="tbInput">
<br/>
<input type="button" value="Add Number" onclick="Push()">
<input type="button" value="Pop Number" onclick="Pop()">
<br/>
<br/>
Now enter your operand, click one to use. Calculation will be solved on click.
<br/>
<input type="button" value="+" onclick="Add()">
<input type="button" value="-" onclick="Subtract()">
<input type="button" value="*" onclick="Multiply()">
<input type="button" value="/" onclick="Divide()">
<br/>
-------------------------------------------------------------------------------------------------
<br/>
<div id="output"><br>
</div>
<div id="Message">
</div>
</body>
</html>
JavaScript
///////////////////////////////////////////////////////////////////////////////////////////
var str;
var Node = function(_content)
{
this.next = null;
this.last = null;
this.content = _content;
}
////////////////////////////////////////////////////////////////////////////////////////
var Stack = function()
{
this.length = 0;
this.head = null;
this.top = null;
}
Stack.prototype.push = function(_content)
{
if(this.top == null){
this.top = new Node(_content);
this.head = this.top;
return this;
}
var addedNode = new Node(_content);
addedNode.last = this.head;
this.head.next = addedNode;
this.head = addedNode;
return this;
}
//////////////////////////////////////////////////////////////////////////////////////////
Stack.prototype.pop = function()
{
if(this.top == null)
{
alert("The stack is empty");
return null;
}
if (this.top == this.head)
{
this.top = null;
this.head = null;
return this.head;
}
var a = this.head
this.head = this.head.last;
this.head.next = null;
return a;
}
////////////////////////////////////////////////////////////////////////////////////////////
Stack.prototype.toString = function()
{
var node = this.head;
str = " ";
while(node != null)
{
str += node.content + "<br>" ;
node = node.last;
}
return str;
}
//////////////////////////////////////////////////////////////////////////////////////////
var stack = new Stack();
function Push()
{
var tb = parseFloat(document.getElementById('tbInput').value);
if (isNaN(tb))
{
str = "Please enter a Number";
document.getElementById('output').innerHTML = str;
document.getElementById('Message').innerHTML = " ";
return false;
}
stack.push(tb);
document.getElementById('output').innerHTML = stack.toString();
}
//////////////////////////////////////////////////////////////////////////////////
function Pop()
{
...
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.