Assignment 5
HTML
<html>
<head>
<h2>
Juan Alban Franco
</h2>
<h4>
Assignment 5
</h4>
</head>
<body>
Input into calculator
<br>
<input type = textbox id = input_txt >
<br>
<br>
<input type=button id=s onclick="Addtostack()" value=test>
<p id = test></p>
</body>
</html>
JavaScript
var stack = new Stack();
function Stack (){
this.head = null;
this.tail = null;
this.length = 0;
}
function Node () {
this.content = null;
this.next = null;
this.prev = null;
}
Stack.prototype.push = function (data){
console.log("made it to push function");
var node = new Node();
console.log(node);
console.log(document.getElementById("input_txt").value);
node.content = data;
console.log(node);
if (this.length < 1){
this.head = node;
this.tail = node;
this.length ++;
}
else {
this.tail.next = node;
node.prev = this.tail;
this.tail = node;
return this;
}
this.length ++;
}
Stack.prototype.pop = function(data){
if (this.head == null){
alert("Stack is currently empty, there is nothing to pop");
return null;
}
else {
var holder_node = this.tail;
this.tail = this.tail.prev;
this.tail.next = null;
return holder_node;
}
}
Stack.prototype.display = function(data){
var traveler = this.head;
var s = "";
while (traveler){
s = traveler.content;
traveler = traveler.next;
}
return s;
}
console.log("at least made a stack");
function Addtostack() {
console.log("Made it to addtostack");
var input = document.getElementById("input_txt").value;
stack.push(input);
var test_string = stack.display();
document.getElementById("test").innerHTML = test_string;
}