Assignment 6 Queue

by austinmillett

HTML

<!-- Heading 1 -->
<h1> Austin Millett </h1>

<!-- Heading 2 -->
<h2> Assignment 6 - Queues </h2>

<!-- Textbox -->
Enter an integer:
<input type = "textbox" id = "Integer" placeholder = "Enter any integer" />

<!-- Button for "Enter Number" -->
<input type = "button" id = "Button" value = "Enter number" onClick = "populateQueue();" />

<!-- Div for output's -->
<br>
<div id = "output1">
<div id = "output2">
<br/>

CSS

/* Design for "Enter Number" button */
#Button {
background-color: black; 
border: 2px solid; 
color: white;
padding: 4px 8px; 
text-align: center; 
font-size: 15px; 
}

JavaScript

//Linked list
function Node(_content) {
this.content = _content;
this.next = null;
this.last = null;
return this;
}

//Function for Queue
function Queue() {
this.bottom = null;
this.top = null;
this.length = 0;

this.enqueue = function(_content) {

//Create a head if the head is null
if (this.bottom == null) {
this.bottom = new Node(_content);
this.top = this.bottom;

this.length++;

return this;
} else {

//else statement to the top node
var addedNode = new Node(_content);
addedNode.last = this.top;
this.top.next = addedNode;
this.top = addedNode;
this.length++;
return this;
}
}

this.dequeue = function() {
if (this.bottom == null) {
return null;
}

//Only if one Node Q1=" "
else if (this.bottom == this.top) {
var t = this.bottom.content;
this.bottom = null;
this.next = null;
this.last = null;
this.length = 0;
return t;
} 
else {
var t = this.bottom.content;
this.bottom = this.bottom.next;
this.bottom.last = null;
this.length--;
return t;
}
}

//Print to screen
this.toString = function() {
var str = "";
var node = this.bottom;

while (node != null) {
str += node.content + ", ";
node = node.next;
}
return str;
}
}

//Function for populating the queue
function populateQueue() {
var Q1 = new Queue();
var Q2 = new Queue();
var n = parseInt(document.getElementById("Integer").value);
for (var i = 2; i <= n; i++) {
Q1.enqueue(i);
}
document.getElementById("output1").innerHTML += "Q1 = " + Q1.toString();

Eratosthenes = function() {
while (Q1.length > 1) {
var node = Q1.bottom;
var p = Q1.dequeue();
Q2.enqueue(p);
for (var j = Q1.top.content - 2; j > 0; j--) {
var x = Q1.dequeue();
if (x % p != 0) {
Q1.enqueue(x);
Q1.next;
} else {
Q1.next;
}
}
document.getElementById("output2").innerHTML += "Q1 = " +
Q1.toString() + "Q2 = " + Q2.toString() + "<br/>";
}
}
Eratosthenes();
}

//Function for clearing display for outputs 1 and 2
function clearDisplay() {
document.getElementById("output1").innerHTML = "";
document.getElementById("output2").innerHTML = "";
}