Queue

Assignment 6

by Alan Harris

HTML

<label>Input a number for the Queue size:  </label><br><br>
<input type="text" id=nodeinput value="10"><br><br>
<button onclick="Sieve();">
Calculate
</button><br><br>
Output: <br>
<div id="print"></div>

CSS

button:focus {
  border: 1px solid black;
  padding: 4px 4px;
}

button {
    background-color: grey;
    border: 1px solid black;
    color: white;
    padding: 4px 8px;
    text-decoration: none;
    margin: 4px 2px;
    }
 button:hover {
   background-color:black;
 }

JavaScript

var ANode = new Node();
var aQueue = new Queue();


function Node(input) {
//	this.id = null;
  this.content = input;
  this.next = null;
  this.prev = null;
  return this;
}  

function Queue(input) {
	this.end = null;
  this.head = null;
  this.length = 0;
}

Queue.prototype.Enqueue = function(ANode) {
	var element = new Node();
  element.content = ANode;
  
  if (this.head == null) {
  	this.head = element;
    this.length++;
    return this;
  }
 if (this.end == null) {
 	this.end = element;
 	this.head.next = this.end;
  this.length++;
  return this;
 }
 this.end.next = element;
 this.end = element;
 this.length++;
}

Queue.prototype.Dequeue = function() {


if (this.head === null) {
	alert("Nothing in Queue");
}


if (this.head !== null) {
	var f = this.head;
  this.head = this.head.next;
  return f.content;
}
else {
	if (this.end !== null) {
  	this.back = null;
  }
}






/*	var p;
  if (this.head == null) {
  	alert("Nothing in Queue");
  }
  
  if (this.head !== null) {
  	 p = this.head.content; 
    this.head = this.head.next;
   this.length--;
  }
  return p;
  
/*
  var g = this.head;
  g.next = g.prev;
  this.prev = this.head;
  this.tail = null;
  this.length--;
  
  return g.content;
  
 */
}



function size() {
	var x = 2;
  var g = document.getElementById("nodeinput").value;
  
  var q = new Queue();
  
  for(x = 2; x < g+1; x++) {
  	q.Enqueue(x);
  }
}


function Sieve() {
	
  //var s = document.getElementById("nodeinput").value;
  
  var qu1 = size();
  var qu2 = new Queue();
  var i1 = "";
  var i2 = "";
  var count = 0;

	while(qu1.head != null) {
  	i1 += "Interation " + String(count) + "Q1: " + qu1.prototype.print() + "Q2: " + qu2.prototype.print() + "<br>";
    document.getElementById("print").innerHTML = i1;
    var prime = qu1.Dequeue();
    qu2.Enqueue(prime);
    var t = new Queue();
    count++;
    
    while (qu1.head != null) {
    	var g = qu1.Dequeue();
      if (g % prime != 0) {
      	t.Enqueue(g);
      }
    }
    qu1 =...