Assignment 5

by Ebony McCoy

HTML

Enter Content Here:

<input type="textbox" id="content" />
<input type="button" value="Push to Stack" onclick="pushNode()" />


<br>
<br>

<p id="output1"></p>

JavaScript

//stack.push(content);
//stack.push(content);


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

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

  LinkedList.prototype.push = function(content) {
    var node = new Node(content);
    node.content = content;
    node.next = null;

    if (this.head == null) {
      this.head = node;
      this.head.next = null;
      //this.length = 1;
      return this;
    } else if (this.head !== null) {

      this.top = this.head;
      while (this.top.next !== null) {
        this.top.next = this.top;
      }

      this.top.next = node;

    }
  }

  LinkedList.prototype.pop = function(content) {
    if (this.head === null) {
      alert("The stack is empty!");
      return null;
    }
    if (this.head !== null) {
      this.head = null;
      return this.top;

    }
    var a = this.top;
    while (this.top !== null) {
      a = this.top;
      this.top = this.top.next;
    }
    a.next = null;
    return this.top;

  }
  LinkedList.prototype.print = function() {
    //if (this.head === null) return "Empty List";
    var display = " ";
    var node = this.head;

    while (node !== null) {
      display += node.content;
      node = node.next;


    }

    var a, b, x, y, result;
    if (LinkedList.top.content == "+") {
      stack.pop();
      a = stack.pop();
      b = stack.pop();

      x = parseInt(a.content);
      y = parseInt(b.content);
      result = x + y;
      stack.push(result);
      display += result;
    } else if (LinkedList.top.content == "-") {
      a = stack.pop();
      b = stack.pop();
      x = parseInt(a.content);
      y = parseInt(b.content);
      result = x - y;
      stack.push(result);
      display += result;
    } else if (LinkedList.top.content == "*") {
      a = stack.pop();
      b = stack.pop();
      x = parseInt(a.content);
      y = parseInt(b.content);
      result = x * y;
      stack.push(result);
     ...