Assignment 6

Queue

by MimiE

HTML

<label>Input a number for the Queue size:  </label><br><br>
<input type="text" id=yourNumber value="10"><br><br>
<button onclick="queue();">
Calculate
</button><br><br>
Output: <br>
<div id="output"></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

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 queue(){

	var yourNumber = Number(document.getElementById("yourNumber").value);
  var q1 = CreateQueuebySize(yourNumber);
  var q2 = new Queue();
  var str1 = "";
  var str2 = "";
  var str = "";
  var count = 0;
  
  while (!q1.isEmpty()) { 
  str = str + "Iteration " + String(count) + "---> Q1: " + q1.toString() + " Q2: " + 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) + "---> Q1: " + q1.toString() + " Q2: " + q2.toString() + " <br>";
  document.getElementById('output').innerHTML = str; 
  
  
  
  
  
  
  
  
  
  
}