<input type="button" value="Repopulate Randomly" onClick="repopulateRandomly();" />
<br/>
<input type="button" value="Merge Sort" onClick="CallMergeSort();" />
<br/>
Once the Chain/List is sorted<br/>
(1) insert elements into the correct location in the Chain based on sort order
=====> Value to Insert: <input type="textbox" id="valueToInsert" value="8" size="3"/>
<input type="button" value="Insertion Sort" onClick="CallInsertSort();" />
<p id='output1'></p>
<p id='output2'></p>
<p id='reportOut'></p>
JavaScript
var Node = function(_content) {
this.next = null;
this.previous = null;
this.content = _content;
}
var Queue = function() {
this.front = null;
this.back = null;
// was addToBack, now called addToFront
this.addToFront = function(_content) {
// No front - create one
if (this.front == null) {
this.front = new Node(_content);
this.back = this.front;
return this;
}
// create a new node to be the back
var addedNode = new Node(_content);
// a) store pointer to current back
// as this.back of new node
addedNode.next = this.front;
// b) store pointer to new back
// as this.back.next of current back
this.front.previous = addedNode;
// c) store pointer to new back
// as this.back which becomes current back
this.front = addedNode; // which becomes new back
return this;
}
// now addToBack adds to the back
this.addToBack = function(_content) {
// No front - create one
if (this.front == null) {
this.front = new Node(_content);
this.back = this.front;
return this;
}
// create a new node to be the back
var addedNode = new Node(_content);
// a) store pointer to current back
// as this.back of new node
addedNode.previous = this.back;
// b) store pointer to new back
// as this.back.next of current back
this.back.next = addedNode;
// c) store pointer to new back
// as this.back which becomes current back
this.back = addedNode; // which becomes new back
return this;
}
this.insertAfter = function(valueToAdd) {
var listX = "";
var node = this.back;
if(valueToAdd < this.front.content){
return "goesInFront";
}
if(valueToAdd > this.back.content){
return "goesInBack";
}
// This goes through the queue from the back to the front
while (node != null) {
if(valueToAdd > node.content){
// create...
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.