JSFiddle - React, Tailwind, and code Playground
by hesster92
HTML
<input type="button" value="create list" onClick="createList();" />
<input type="button" value="sort1" onClick="startSort()" />
<input type="button" value="sort2" onClick="startSort2()" />
<input type="textbox" id="value" value="node content" />
<input type="button" value="Add String" onClick="InsertAndSort();" />
<p id="demo"></p>
<br/>
JavaScript
var DoubleLinkedList = function() {
this.head = null;
this.tail = null;
this.length = 0;
this.clear = function() {
this.head = null;
this.tail = null;
this.length = 0;
};
this.add = function(content) {
if (this.head == null) {
this.head = new LinkedListNode(content);
this.length++;
return this.head;
}
if (this.tail == null) {
this.tail = new LinkedListNode(content);
this.head.next = this.tail;
this.tail.last = this.head;
this.length++;
return this.tail;
}
this.tail.next = new LinkedListNode(content);
this.tail.next.last = this.tail;
this.tail = this.tail.next;
this.tail.next = null;
this.length++;
return this.tail;
};
this.copy = function() {
var newlist = new DoubleLinkedList();
var node = this.head;
while (node != null) {
newlist.add(node.content);
node = node.next;
}
return newlist;
}
this.pop = function() {
if (this.head == null) {
this.length = 0;
return null;
}
// Case of one node
if (this.length == 1) {
var a = this.head;
this.tail = null;
this.head = null;
this.length = 0;
return a;
}
// Case of length > 1
var a = this.head; // hold value for return
this.head = this.head.next;
this.head.last = null;
this.length--;
return a;
};
this.toString = function() {
var str = this.length + ' strings: <br>';
var node = this.head;
while (node != null) {
str += node.content + "<br/>";
node = node.next;
}
return str;
};
}
var LinkedListNode = function(content) {
this.next = null;
this.last = null;
this.content = content;
}
var MyLinkedList = new DoubleLinkedList();
function bubbleSort(list) {
var length = list.length;
if (length <= 1) return list;
var node = list.head;
while ((node != null) && (node.next != null)){
if(node.content > node.next.content){
var tmp =...