A5 Final

Finally works RPN calculator

by scotp71

HTML

<input type="textbox" id="tbValue"/>
<input type="button" id="btnPush" value="Push to Stack" onclick="toStack()"/>

<div id="out"/>
<div id="out2"/>

JavaScript

function Node(content){
	this.content=content;
  this.next=null;
  this.last=null;
}

function LinkedList(){
	this.length = 0;
  this.head = null;
  this.top = null;
}

LinkedList.prototype.push = function(content){
	//if there is no head set new node to head of list
  if(this.head==null){
  	this.head=new Node(content)
    //set top equal to head because they are the same thing
    this.top = this.head;
    this.length +=1;
    return this;
  }
  //if there is a head already, create a new node
  var addedNode = new Node(content);
  addedNode.next = this.top;  //set next pointer set to exsisting top
  this.top.last = addedNode;	//exsisting top's last pointer set to new node
  this.top = addedNode;		//set the top equal to the new node
  this.length ++
  return this;
}

LinkedList.prototype.pop = function(){
	if(this.head == null){
  	alert("The stack is empty");
    return null
  }
  if(this.length==1){
  	var cur = this.head;
    this.head = null;
    this.top = null;
    this.length = 0;
    return cur;
  }
  //now remove the top node
	var t = this.top;  //hold value for return
  this.top = this.top.next   //current top pointer becomes the last node
  this.top.last = null;		//next pointer of last node points to null now
  this.length -=1;
  return t;
  
}

LinkedList.prototype.toPrint = function(){
	if (this.head == null) return "Empty List"
  var s = "";
  var node = this.head;
  while (node != null) {
  	s += node.content + ", ";
    node = node.last;
  }
  document.getElementById("out").innerHTML = s;
	return s;
}

var stack = new LinkedList();

function toStack(event){
	debugger;
  var v = document.getElementById("tbValue").value;
  var a, b, x, y, answer;
  if(v=="+"){
  	a = stack.pop();
  	b = stack.pop();
  	x = parseInt(b.content);
  	y = parseInt(a.content);
  	answer = x + y;
	document.getElementById("out").innerHTML = x + v + y + " = " + answer;
  stack.push(answer);
	}
  else if(v=="-"){
  	a = stack.pop();
  	b = stack.pop();
  	x =...