Assignment 6

Queues

by Jenni Meiklejohn

HTML

<input type = "textbox" id = "input"/>

<input type = "button" onClick = "Execute()" id = "button" value = "Enter"/>

<br><br>

<p id = "output">

</p>

CSS

#button
{
  background-color: grey;
  color: white;
}

JavaScript

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

function Queue() 
{
  this.head = null;
  this.tail = null;
}

Queue.prototype.toString = function() 
{
  var str = "";
  var node = this.head;

  while (node != null) 
  {
    str += node.content + " ";
    node = node.next;
  }
  return str;
}


Queue.prototype.isEmpty = function() 
{
	if (this.head == null)
  {
  	return true;
  }
  else return false;
}

Queue.prototype.dequeue = function() 
{
  var n;
  
  if (this.head !== null) 
  {
    n = this.head.content;
    this.head = this.head.next;
  }
  return n;
}

Queue.prototype.enqueue = function(content) 
{
  var n = new Node(content);

  if (this.head === null) 
  {
    this.head = n;
    this.tail = n;
  } 
  else 
  {
    this.tail.next = n;
    this.tail = n;
  }
}

function CreateQueuebySize(size)
{
	var i = 2;
  var q = new Queue();
	
  for (i = 2; i < size+1; i++) 
  { 
    q.enqueue(i);
    
  }
 	return q;
}

function Execute()
{
	var input = Number(document.getElementById("input").value);
  var q1 = CreateQueuebySize(input);
  var q2 = new Queue();
  var str1 = "";
  var str2 = "";
  var str = "";
  var count = 0;
  
  
  while (!q1.isEmpty()) 
  { 
  str = str + "Iteration " + String(count) + ": Queue 1: " + q1.toString()     + " Queue 2: " + q2.toString() + " <br>";
  
  document.getElementById("output").innerHTML = str;
  
  var prime = q1.dequeue();
  
  q2.enqueue(prime);
  
  var temp = new Queue();
  
  count = count + 1;
  
  while (!q1.isEmpty()) 
  { 
    var a = q1.dequeue();
    
    if (a % prime != 0) 
    { 
      temp.enqueue(a);
    }
 	}
  q1 = temp; 
	} 
  
  str = str + "Iteration " + String(count) + ": Queue 1: " + q1.toString()     + " Queue 2: " + q2.toString() + " <br>";
  
  document.getElementById("output").innerHTML = str; 
}