Rodrigo Munoz - Assignment 5 - COP3530 Daytona State

Stack Calculator

by wooozy

HTML

<!-- forked from Sample by Dr. Eaglin
https://jsfiddle.net/reaglin/0LLt6q55/ -->
<p id='reportOut'></p>

JavaScript

// start code by Dr. Eaglin, modified by Rodrigo Munoz
  var Node = function(_content) {
      this.next = null;
      this.last = null;
      this.content = _content;

    }
  var Stack = function() {
    this.head = null;
    this.top = null;
    
    this.push = function(_content) {
      // No head - create one
      if (this.head == null) {
        this.head = new Node(_content);
        this.top = this.head;
        return this;
      }
      
      var addedNode = new Node(_content);
      addedNode.last = this.top;   // pointer to previous node
      this.top.next = addedNode;   // current top points to new
      this.top = addedNode;        // which becomes new top
      return this;
    }


    this.pop = function() {
      if (this.head == null) {
        // alert("The Stack is Empty");
        return null;
      }
//				// this line was incorrect:
//        // var a = this.top;   // hold value for return
//        // it actually sends the object, not the value
//        // this line would hold the value
			var a = this.top.content;

      if(this.top == this.head){
        // alert("The Stack Only Has one item");
				    this.head = null;
    				this.top = null;
           	return a;
      }else{
        this.top = this.top.last;
        this.top.next = null;      
      	return a;
      }
    }


    this.toString = function() {
      var str = "";
      var node = this.head;
      if (this.head == null){
        str = "Stack is Empty";
      }else{
      //  str = "Stack: ";
      }
      while (node != null) {
        str += node.content + "&nbsp";
        node = node.next;
      }
      return str;
    }
    
    this.countElements = function() {
      var countX = 0;
      var node = this.head;
      while (node != null) {
        node = node.next;
        countX= countX+1;
      }
      return countX;
    }
  }
// End Code By Dr. Eaglin

// Make some html buttons
  var buttonRunCalc = '<input type="textbox" id="operatorIn" value="+"...