Assignment 6--Queue
by arosado417
HTML
<fieldset>
<legend>
Sieve of Eratosthenes</legend>
<input type="button" value="Populate" onclick="startSieve()"/>
<input type="number" id="input"/>
<legend>Output
</legend>
<textarea id="output" rows="15" style="width : 100%";></textarea>
</fieldset>
JavaScript
function startSieve(){
var counter = 0;
var v = parseInt(document.getElementById("input").value);
Q1 = populateQueue(v);
primes = new LinkedList();
/*
//test to check contents of the lists
console.log(Q1);
console.log(primes);
// test to chech the print function
console.log(Q1.print());
console.log(primes.print());
*/
document.getElementById("output").innerHTML += "Iteration " + counter + ": Q1 = " + Q1.print() + ", Q2 = " + primes.print() + "\n";
while(Q1.head != null){
var headElem = Q1.dequeue();//dequeue 1st element in Q1
console.log("Removed value" + headElem);
primes.enqueue(headElem);//enqueue this element into Q2
var l = Q1.length;
for(i = 0; i<= l -1; i++){
var val = Q1.dequeue();
if(val%headElem == 0){//iterate and Dequeue each successive element of Q1
console.log("Element not a prime " + val);//if the val is div by headElem go to next element
}
else{
console.log("Element is not div by num " + val) ;//if cal is not divisible by headElem enqueue back onto Q1, go to the next element
Q1.enqueue(val);
}
}
counter ++;
console.log(Q1.print());
console.log(primes.print());
document.getElementById("output").innerHTML += "Iteration " + counter + ": Q1 = " + Q1.print() + ", Q2 = " + primes.print() + "\n";
}
}//starting at the head grab the node content and send to s while node is not null
LinkedList.prototype.print = function() {
var s = "";
var node = this.head;
while (node != null) {
s += node.content + " ";
node = node.next;
}
return s;
}
function LinkedList(){
this.head = null;
this.tail = null;
this.length = 0;
}
function Node(){
this.next = null;
this.content = null;
}
LinkedList.prototype.enqueue = function(_content){
var node = new Node(_content); node.content = _content;
if(this.head == null){
this.head = node;
this.length = 1;
return node.content;
}
if(this.tail == null){
this.tail = node;
this.head.next = this.tail;
this.length +=1;
return node.content;
}
this.tail.next = node;
this.tail = node;
this.length += 1;
return...