My study of JS linked list

Keywords: Javascript Attribute

My study of JS linked list

What is Linked List

To store multiple elements, arrays are probably the most commonly used data structures. This data structure is very convenient, but it has one drawback: the cost of inserting or removing items from the beginning or middle of an array is very high, because elements need to be moved (for example, all the elements after you insert an element move "position").

The linked list stores an orderly set of elements, but unlike arrays, elements in the linked list are not placed continuously in memory. Each element consists of a node that stores the element itself and a reference to the next element (also known as a pointer or link).

The advantage of linked lists over arrays is that no other elements need to be moved when adding or deleting elements. But pointers are needed to operate the linked list. An advantage of arrays is that you can access any element directly anywhere, but if you want to access an element in the list, you have to iterate from the start until you find the target element.

Link List Learning

Create a linked list

function LinkedList() {
    var Node = function(element) {
        this.element = element;
        this.next = null;
    }

    //Various methods
}

Node denotes the item to be added to the list. It contains an element attribute and a next attribute. Element denotes the value to be added to the list. Next denotes the pointer to the next node item in the list.

When a Node element is created, its next pointer is always null

  • Adding elements to the end of the list

The list is empty and the first element is added. The list is not empty, add elements to it.

this.add = function(element) {
    var node = new Node(element), //Input Value Creates Node Item
        current;

    if(head === null) { //If the list is empty
        head = node; //Set node to head (head is a reference to the first node)
    } else {
        current = head; //Start with the header
        while(current.next) { 
            //Loop through the list to find the last item (the next element of the last node in the list is always null)
            current = current.next;
        }
        //Pointer the current last item to node
        current.next = node;
    }
    length++; //Update list length
};

Using append

var list = new LinkedList();
list.append(15);
list.append(10);
  • Remove elements from the list

Remove an element from a specific location

this.removeAt = function(position) {
    if(position > -1 && position < length) { //Effectiveness Detection
        var current = head, //Loop lists with current
        previous,
        index = 0;

        if(position === 0) {
            head = current.next; //Remove the first element and point the head directly to the next element
        } else {
            while(index++ < position) { //Loop lists find the element that meets the criteria
                previous = current; //
                current = current.next; //Override the next variable to current
            }
            //Skip current and connect the previous element to the next one.
            previous.next = current.next;
        }
        length --;
        return current.element;
    } else {
        return null;
    }
}

Insert an element anywhere

this.insert = function (position, element) {
    if(position > -1 && position < length) {
        var node = new Node(element),
            current = head; //Iterate from the head position through current
            previous,
            index = 0;

        if(position === 0) { //First position
            node.next = current; //At this point, current = head, pointing to head, node becomes the first
            head = node; //node points to head
        } else {
            while (index++ < position ) { //Cyclic iteration to target location
                previous = current; 
                current = current.next;
            }

            node.next = current; // The next node is current
            previous.next = node; // The last location of node is previous
        }
        length++;
        return true;
    } else {
        return false;
    }
}

Unfinished...

Posted by aaronrb on Fri, 19 Apr 2019 16:33:33 -0700