Stack
Stack
by dhizzybusy
HTML
<h1>
<Center>Stack</Center>
</h1>
<br><br/>
<input type="textbox" id="stackName" Value="" />
<input type="button" id="PushtoStack" value="Push to Stack" onClick="pushtoStack();" />
<div id="output">
</div>
JavaScript
// Linked List
function Node(data) {
this.data = data;
this.next = null;
}
// Stack implemented using LinkedList
function Stack() {
this.top = null;
}
Stack.prototype.push = function(data) {
var newNode = new Node(data);
newNode.next = this.top; //Special attention
this.top = newNode;
}
Stack.prototype.pop = function() {
if (this.top !== null) {
var topItem = this.top.data;
this.top = this.top.next;
return topItem;
}
return null;
}
Stack.prototype.print = function() {
var str="";
var curr = this.top;
while (curr) {
console.log(curr.data);
str+= "</br> "+ JSON.stringify(curr)+"</br>";
console.log(str);
curr = curr.next;
}
return str;
}
// implementation of function
var d = " ";
var stack = new Stack();
function clearDisplay()
{
//Global string d is used to hold display
d = " ";
// The div element named output is used to display output
document.getElementById("output").innerHTML = "";
}
function pushtoStack(){
clearDisplay();
var newStack= (document.getElementById("stackName").value);
stack.push(newStack)
d=stack.print()
document.getElementById("output").innerHTML =d;
}
// stack.push(3);
// stack.push(5);
// stack.push(7);
// stack.print();