testing

by Peyton Hessler

HTML

<input type="textbox" id="v" value="aaa" />
<input type="button" value="Add Node" onClick="addNode()"/>
<input type="button" value = " " /> 
<div id="output">

</div>

JavaScript

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

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

LinkedList.prototype.add = function(_content) {
	var node = new Node(); node.content = _content;
  
  if (this.head == null){
  	this.head = node; this.length = 1;
    return node;
  }
  
  if (this.tail == null){
  	this.tail = node;
    this.tail.previous = this.head;
    this.head.next = this.tail;
    this.length = 2;
    return node;
  }
  
  this.tail.next = node;
  node.previous = this.tail;
  this.tail = node;
  this.lenth++;
  return node;
  
}





LinkedList.prototype.addSorted = function(_content){
	if (this.head == null) {return this.add(_content);}
  var node = new Node(); 
  node.content = _content;
  
  if (_content < this.head.content) {
  	this.head = node;
    node.next = this.head;
    this.head = node;
  	return node;
  }

	var cnode = this.head;
  while (cnode.next != null) {
  	if ((_content > cnode.content) && (_content < cnode.next.content)){
    	node.previous = cnode; 
      node.next = cnode.next;
      cnode.next.previous = node;
      cnode.next = node;
    }
    cnode = cnode.next;
    return node;
  }
	
  return this.add(_content);
  
}




LinkedList.prototype.print = function() {
	if (this.head == null) return "Empty List";
  var s = "";
  var node = this.head;
  while (node != null){
		s += node.content + " <br/> ";
    node = node.next;
  }
  return s;
}

var aList = new LinkedList();

function addNode() {
	var c = document.getElementById("v").value;
  aList.addSorted(c);
  document.getElementById("output").innerHTML = aList.print();
  document.getElementById("v").value = randomString(3);
}

function randomString(_length){
	var chars = "abcdefghijklmnopqrstuvwxyz0123456789";
  var randomstring = '';
  for (var i = 0; i < _length; i++) {
  var ranNum = Math.floor(Math.random() * chars.length);
  randomstring += chars.substring(ranNum, ranNum + 1);
  }
  return...