<h1>Queues</h1>
Enter a number greater than 2 and click(Create List) to create a number list then click(Sieve of Eratosthenes) to view Iteration.
<input type="textbox" id="numForQ" value="">
<br/ >
<br/ >
<input type="button" value="Create Lists" onClick="createQ();">
<br/ >
<br/ >
<input type="button" value="Remove from the Queues" onClick="removeQ();">
<br/ >
<br/ >
<input type="button" value="Sieve of Eratosthenes" onClick="sieve();">
<br/ >
<br/ >
<div id="output1">
</div>
<p id="output2">
</p>
<p id="output3">
</p>
<div id="output4">
</div>
<div id="output5">
</div>
JavaScript
var Node = function(_content) {
this.content = _content;
this.prev = null;
this.next = null;
return this;
};
//////////////////////////////////////////////////////////////////////////////////////////////
var Queue = function(_content) {
this.length = 0;
this.front = null;
this.last = null;
return this;
};
Queue.prototype.enqueue = function(_content) {
var node = new Node(_content);
node.content = _content;
if (this.front == null) {
this.front = node;
this.last = this.front;
this.length++;
return this;
} else if (this.front == this.last) {
this.last = node;
this.front.next = this.last;
this.last.prev = this.front;
this.length++;
return this;
} else {
this.last.next = node;
node.prev = this.last;
this.last = node;
this.length++;
return this;
}
};
Queue.prototype.dequeue = function(_content) {
if (this.front == null) {
alert("Queue is empty.");
return null;
} else if (this.front == this.last) {
this.last = null;
this.front = null;
this.length--;
return this;
} else {
var removedNode = this.front;
this.front = this.front.next;
this.front.prev = null;
this.length--;
return removedNode;
}
};
Queue.prototype.toString = function() {
var str = "";
var node = this.front;
while (node != null) {
str += node.content + " ";
node = node.next;
}
return str;
};
var Q1 = new Queue();
var Q2 = new Queue();
/////////////////////////////////////////////////////////////////////////////////////////////
function createQ() {
var numForQ = document.getElementById("numForQ").value;
Q1.enqueue(2);
for (var i = 3; i <= numForQ; i++) {
Q1.enqueue(i);
}
document.getElementById("output1").innerHTML = Q1.toString();
}
//////////////////////////////////////////////////////////////////////////////////////////////
function removeQ() {
Q1.dequeue();
document.getElementById("output1").innerHTML =...
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.