Assignment 5 Main

by Ron Eaglin

HTML

<head>
Enter a value to push onto the stack: 
<br/>
<input type = "textbox" id = "pushBox" value = "">
<input type = "button" id = "pushButton" value = "push value" onClick = "pushValue();">
</head>

<div id = "output">

</div>

<div id = "output2">

</div>

JavaScript

var Node = function(_value) {
  this.next = null;
  this.last = null;
  this.value = _value;
 }

var Stack = function() {
  this.head = null;
  this.top = null;

  this.push = function(_value) {
    if (this.head == null) {
      this.head = new Node(_value);
      this.top = this.head;
      return this;
    
   } 
    var addedNode = new Node(_value);
    addedNode.last = this.top;
    this.top.next = addedNode;
    this.top = addedNode;
    return this;
    
  }
  
  this.pop = function() {
    if (this.head == null) {
      alert("Stack is empty");
      return null;
    }
    
   
    var a = this.top;
    this.top = this.top.last;
    this.top.next = null;
    return a;
  }
  

  this.toString = function() {
    var str = "";
    var node = this.head;
    
    while(node!=null){
    str += node.value;
    node = node.next;
    }
    
  return str; 

  	}
    
}

var stack = new Stack(); 

function multiply(){
  let pop1 = stack.pop();
  let pop2 = stack.pop();
  let result = stack.push(pop1.value * pop2.value);
  return result;
  
  }
  
 function addition(){
 let pop1 = stack.pop();
 let pop2 = stack.pop();
 let sum = pop1.value + pop2.value;
 let result = stack.push(sum);
 return result;
 } 
 
 function subtract(){
 let pop1 = stack.pop();
 let pop2 = stack.pop();
 let result = stack.push(pop1.value - pop2.value);
 return result;
 }
 
 function divide(){
 let pop1 = stack.pop();
 let pop2 = stack.pop();
 let result = stack.push(pop1.value/pop2.value);
 return result;
 }
  
function pushValue(){

var value = document.getElementById("pushBox").value;
debugger;
if ((value != '*') && (value != '+') && (value != '-') && (value != '/')){
 stack.push(value);  
 document.getElementById("output").innerHTML = stack.toString();
 }
  
 if(value == '*'){
 console.log("*");
 multiply();
 document.getElementById("output").innerHTML = stack.toString();
  }
  
 if(value == '+'){
 addition();
 document.getElementById("output").innerHTML = stack.toString();
  }

 else...