<h1 style="text-align:center;">Enter a number for the size of the queue </h1>
<input type="text" id="yourNumber" />
<br/><br/>
<input type="button" onclick="queue()" value="Submit" /
><br/><br/>
Output: <br/><br/>
<p id='output'></p>
CSS
body {
color: #0F0E0E;
background-color: #E7DAD7;
}
h1 {
color: #0F0E0E;
}
p {
color: #0F0E0E;
}
JavaScript
//introduce the node
function Node(content) {
this.content = content;
this.next = null;
}
//introduce the queue
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;
}
//if empty run as null if not put something there
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 this is empty...it will list the iteration
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;
}
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.