COP3530 Queue

Assignment 6

by ClarenceDowns

HTML

<h2>
  Sieve of Eratosthenes Algorithm:</h2>
<input type="textbox" id="QueueSize" Value="" />
<input type="button" id="CreateQueue" value="Run" onClick="createQueue();"/>
<br/>
<h3>
  Q One:
</h3>
<p id="demo"></p><br/>
<h3>
  Q Two:
</h3>
<p id="demo2"></p>

JavaScript

var q1 = new List();
var q2 = new List();


// When button is clicked createQueue() is called
function createQueue() {
  var x;
  var y;
  var value = parseInt(document.getElementById("QueueSize").value);

  //create the queue
  for (let i = 2; i <= value; i++) {
    q1.enqueue(i);
  }
  var display = "";
  var display2 = "";
  var l = q1.length;
  for (let i = 2; i <= l; i++) {
    display += "Iteration " + i + " = " + q1.print() + "<br/>";
    display2 += "Iteration " + i + " = " + q2.print() + "<br/>";

    var d = q1.length;
    x = q1.dequeue();
    document.getElementById("demo").innerHTML = display;
    document.getElementById("demo2").innerHTML = display2;
    q2.enqueue(x);
    //document.getElementById("demo2").innerHTML = q2.print();
    //looping through q1: Dequeue Q1, assign that value to
    // if y % by x does not = zero(in other words, if it is divisible by x we are getting rid of it. Otherwise insert it back into Q1)
    for (let e = 2; e <= d; e++) {
      y = q1.dequeue();

      if (y % x !== 0) {
        q1.enqueue(y);
      }

    }
    //document.getElementById("demo").innerHTML = "Iteration " + i + ": " + " Q1 =" + q1.print();
    // document.getElementById("demo2").innerHTML = "Iteration " + i + ": " + " Q2 =" + q2.print();
  }
}
// Define the link object
function Node(_value, _last) {
  this.value = _value; // The value stored 
  this.last = _last; // A pointer to the previous link
  this.next = null; // a pointer to the next link
  return this; // returns the created node
}

Node.prototype.asString = function() {
  return this.value + " "; //+ "<br/>";
};


// Define the List object
function List(_value) { // We will define the list with the first link defined
  this.length = 0;
  this.head = null; // Pointer TO the head is null
  this.last = this.head; // When created - head and last are the same.
}

List.prototype.enqueue = function(value) {
  let node = new Node(value);
  let current;
  if (this.head == null) {
    this.head =...