Asignment 8_Resubmission

by sheila massey

HTML

<h1>Sorting Resubmission</h1>

<button id='initList' onclick='initList()'> Start node list </button>
<br>
<br>

<input type="textbox" id="v" value="" />
<br>
<br>

<button id = 'add' onclick = 'add()'> Add 'Node' to the end of list</button>
<button id = 'pop' onclick = 'pop()'> Remove the 'node' </button>
<br>
<br>

<button id = 'nq' onclick = 'enqueue()'> Add 'Node' to the start of list)</button>
<button id = 'dq' onclick = 'dequeue()'> Remove the 'Node' </button>
<br>
<br>

<div id="output">

</div>

JavaScript

/////////////////////////////////////////////////////////////////////////////////////////////////
var i;

function LinkedList() 
{
  this.head = null;
  this.tail = null;
  this.length = 0;
}
/////////////////////////////////////////////////////////////////////////////////////////////
function Node() 
  {
    this.next = null;
    this.prev = 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.prev = this.head;
      this.head.next = this.tail;
      this.length++;

      return node;
    }

  this.tail.next = node;
  node.prev = this.tail;
  this.tail = node;
  return node;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
LinkedList.prototype.pop = function () 
{
	if (this.head == null )
    { 
    alert('Empty list');
    document.getElementById('v').focus();
    return null;
    }
 	
  if (this.head == this.tail) 
    {
      var temp = this.head;
      this.head = null;
      this.tail = null;
      this.length = 0;
      return temp;
    }
 var oldtail = this.tail;
 var newtail = this.tail.prev;
 
 newtail.next = null;
 this.tail = newtail;
 this.length--;
 return oldtail;
}  
////////////////////////////////////////////////////////////////////////////////////////////////
LinkedList.prototype.enqueue = function(_content) 
{
  var node = new Node();
  node.content = _content;
  if (document.getElementById('v').value == '')
    {
     alert('Error, enter data');
     document.getElementById('v').focus();
    }

  else if (this.head == null) 
    {
      this.head = node;
      this.tail = this.head;
      this.length++;
     ...