Assignment 5

by Ebony McCoy

HTML

Enter Content Here:

<input type="textbox" id="content" />
<input type="button" value="Add Node to List" onclick="addNode()" />

<br>
<br>

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

JavaScript

var stack = new LinkedList();

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

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

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

  if(this.head === null) {
    this.head = node;
    this.top = this.head;
    this.length = 1;
    return node;
  }

  if(this.head !== null) {
   var addedNode = new Node();
   addedNode.last = this.top;
    this.top.next = addedNode;
    this.top = addedNode;
    this.length = 2;
    return addedNode;
  }

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


function addNode() {
  var content = document.getElementById("content").value;
  stack.push(content);
  stack.print();
}

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

  while (node !== null) {
    display += " Content: " + node.content + "</br>";
    node = node.next;

  }
  document.getElementById("output1").innerHTML = display;
};

stack.pop();
stack.pop();
calc();

document.getElementById("output2").innerHTML = "";

/*function createList(content) {
  for (var i = 0; i < 5; i++) {
    content = String.fromCharCode(65 + i);
    stack.push(content);
    stack.print();
  }

}
*/