JSFiddle - React, Tailwind, and code Playground
by arosado417
HTML
<input type="button" id="AddNode" value="Create Random String List" onClick="addNode();" />
<div id="output1"></div>
<br />
<input type="button" id="Sort List" value="Sort List" onClick="addSorted();" />
<div id="output2"></div>
<br />
<input type="button" id="AddNode" value="2nd sort" onClick="" />
<br />
<input type="button" id="CreateList" value="Insert String" onClick=/>
<br />
<input id="a" Value="abcde" />
<div id="output3"></div>
<div id="output4"></div>
JavaScript
function LinkedList() {
this.head = null;
this.tail = null;
this.length = 0;
}
function Node() {
this.next = null;
this.prev = null;
this.content = null;
}
LinkedList.prototype.add = function(_content) {
var node = new Node();
node.content = _content;
if (this.head == null) {
this.head = node;
this.length = 1;
return node;
}
if (this.tail == null) {
this.tail = node;
this.tail.prev = this.head;
this.head.next = this.tail;
this.length++;
return node;
}
this.tail.next = node;
node.prev = this.tail;
this.tail = node;
this.length++;
return node;
}
LinkedList.prototype.addSorted = function(_content) {
if (this.head == null) {
return this.add(_content);
}
var node = new Node();
node.content = _content;
if (_content < this.head.content) {
this.head.prev = node;
node.next = this.head;
this.head = node;
return node;
}
//if current node is head
var curnode = this.head;
while (curnode.next != null) {
if ((_content > curnode.content) && (_content < curnode.next.content)) {
node.prev = curnode;
node.next = curnode.next;
curnode.next.prev = node;
curnode.next = node;
}
curnode = curnode.next;
return node;
}
return this.add(_content);
}
LinkedList.prototype.print = function() {
if (this.head == null) return "Empty List";
var s = "";
var node = this.head;
while (node != null) {
s += node.content + " " + " ,";
node = node.next;
}
return s;
}
var aList = new LinkedList();
var sortedList = new LinkedList();
var text;
function randomString(_length) {
var text = "";
var possible = "abcdefghijklmnopqrstuvwxyz0123456789";
for (j = 0; j < 20; j++) {
for (var i = 0; i < _length; i++) {
var ranNum = Math.floor(Math.random() * possible.length);
text += possible.substring(ranNum, ranNum + 1);
}
return text;
}
}
function addNode() {
var c =...