JSFiddle - React, Tailwind, and code Playground

by Peyton Hessler

HTML

<input type="button" value="create list" onClick="createList();"/>
<input type="button" value="Bubble Sort" onClick="StartBubbleSort()"/>
<input type="button" value="Merge Sort" onClick="StartMergeSort()"/>
<input type="textbox" id="value" value=""/>
<input type="button" value="Insert Sort" onClick="InsertAndSort();"/>
<p id="demo"></p>
<br/>

JavaScript

// Here I have created a DoubleLinkedList, which points each node back at each other. The function makes sure that when the list is manipulated that the nodes will still be pointing in the right direction.
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 is my string function to state how items are in the list.
  this.toString = function() {
    var str = "The list has a length of " + this.length + " items" + "<br/>";
    var node = this.head;
    while (node != null) {
      str += node.content + "<br/>";
      node = node.next;
    }
    return str;
  };
}




var LinkedListNode = function(content) {
  this.next = null;
 ...