Assignment 10

Needs work

by Jenni Meiklejohn

HTML

<h1><center> Assignment 10</center></h1>
<h2><center> Indexing </center></h2>


<input type="button" class="button" value="Start Indexing" onClick="fillStack();" />


<p id="output"> </p>

JavaScript

var Node = function(content) {
  this.next = null;
  this.previous = null;
  this.content = content;
  this.length = 0;
}

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

  this.push = function(content) {
    if (this.bottom == null) {
      this.bottom = new Node(content);
      this.top = this.bottom;
      return this;
    }

    var addedNode = new Node(content);
    addedNode.last = this.top;
    this.top.next = addedNode;
    this.top = addedNode;
  }
}

this.copy = function() {
  var newList = new Stack();
  var node = this.top;

while (node != null) {
    newList.add(node.content)
    node = node.next;
  }
  return newList
}

this.pop = function() {
  if (this.top == null) {
    this.length = 0;
    return null;
  }

  if (this.length == 1) {
    var a = this.top;
    this.bottom = null;
    this.top = null;
    this.length = 0;
    return a;
	  }
    var a = this.top;
 	 this.top = this.top.next;
 	 this.top.last = null;
    this.length--;
 	 return a;
  }

this.toString = function() {
	var str = this.length;
    var node = this.top;
    while (node != null) {
     str += node.content + node.contentB;
        str += '<br/>';
      node = node.next;
    }
    return str;
  }

var linkedList = function(content){
this.next = null;
this.last = null;
this.content = content;
var hash = hashingFunction(content);
this.contentB = hash;

}

//http://mediocredeveloper.com/wp/?p=55
//Algorithm s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
hashingFunction = function(str) {
  var hash = 0
  if (str.length == 0) return hash;
  for (i = 0; i <str.length; i++) {
    char = str.charCodeAt(i);
    hash = ((hash << 5) - hash) + char;
    hash = hash & hash;
      return hash;
  
  }

}

var Chain = new Stack();

function fillStack() {
  for (i = 1; i <= 10; i++) {
    Chain.push(randomString());
    document.getElementById("output").innerHTML += str + "<br/>";
  }
}

function randomString() {
  var charOptions = "0123456789";
  str = "";
  for (var i = 0;...