JSFiddle - React, Tailwind, and code Playground

by HSilvaDaytona

HTML

This is an example<br/>
    <br/><br />
    <input type="button" id="CreateList" value="Create List" onClick="createList();" />
    <br /><br /> Name of Node being added:
    <input id="LinkName" Value="New Node" />
    <input type="button" id="AddLink" value="Add Node to List" onClick="addNode();" />
    <p id="demo"></p>
    <br />

JavaScript

var list = null;

        function createList() {
            var value = document.getElementById("LinkName").value;
            list = new List(value);
            document.getElementById("demo").innerHTML = list.print();
        }


        function addNode() {
            var value = document.getElementById("LinkName").value;
            list.addNode(value,list.last);
            document.getElementById("demo").innerHTML = list.print();
        }


        // Define the link object
        function Node(_value, _last) {
            // This is a possible implementation of a Doubly Linked List
            this.value = _value; // The value stored
            this.last = _last; // A pointer to the previous link
            this.next = null; // a pointer to the next link
            return this; // returns the created node
        }

        Node.prototype.asString = function () {
            return "a" + this.value + "<br/>";
        }


        // Define the List object
        function List(_value) { // We will define the list with the first link defined
            this.length = 1;
            this.head = new Node(_value, null); // Pointer TO the head is null
            this.last = this.head; // When created - head and last are the same.
        }

        //Function to add node to list (Changed this code)
        List.prototype.addNode = function (_value,_last) {
            var node = new Node(_value, _last); // create a new node to attach it to the list

            if (this.length) { //If list has some node already
                this.last.next = node; //Setting next node of current last node of the list
                node.last = this.last; //setting last of new node
                this.last = node; /// Setting node as a last node od the list
            } else {
                this.head = node;
                this.last = node;
            }

            this.length++;
        }

        // This is a very simple print routine - it starts at the head
...