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(c){
var counter = 0;
var v = parseInt(document.getElementById("input").value);
Q1 = populateQueue(v);
primes = new LinkedList();
/*if(counter == 0){
clearDisplay(Q1,0);
}
console.log(Q1);
console.log(primes);
*/
while(Q1.head != null){
/*var d = "Iteration " + counter + ": Q1 = " + q1OP + ", Q2 = " + pOP + "<br />";
*/
document.getElementById("output").innerHTML = Q1.asString();
/*var q1OP = Q1.headElem;
var pOP
*/
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);
console.log(primes);
}
}
LinkedList.prototype.print = function() {
var s = "";
var n = this.head;
while (n != null) {
s += n.asString();
n = n.next;
}
}
/*function clearDisplay(qlist, counter) {
if(qlist == 1) {
d= "Iteration " + counter + ": Q1 = " + " ";
}
else{
d = "Iteration " + counter + ": Q2 =" + " ";
// The div element named output is used to display output
document.getElementById("output").innerHTML = d;
}
}
*/
Node.prototype.asString = function() {
return this.value + "<br/>";
};
/* this.toString = 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(); node.content = _content;
if(this.head == null){
this.head =...