This is the 15th day of my participation in Gwen Challenge

Inserts an element at any position

This time, let’s implement the INSERT method. Use this method to insert an element anywhere. This method takes two arguments:

  • Position: Position to be inserted.
  • Element: The item to be inserted.
function LikedList () {
    / / the node class
    let Node = function (element) {
           this.element = element
           this.next = null
    }
    // List number
    let length = 0 
    // Reference to the first node
    let head = null
    
    this.insert = function (position, element) {
        // Check for out of bounds
        if(position >= 0 && position <= length) {
            // Generate the node to insert
            let node = new Node(element)
            // Declare the variable current to represent the current node for subsequent iterations of the loop. The initial value is head, the first node in the list
            let current = head
            // previous Previous node of the current node, initial value null, that is, the previous node of the first current node is empty
            let previous = null
            // index indicates the position in the list when we loop through the current node
            let index = 0
            if (position === 0) {
                 node.next = current
                 head = node
            } else {
                while (index++ < position) {
                    previous = current
                    current = current.next
                }
                node.next = current
                previous.next = node
            }
            // Update the length of the list
            length++
            return true
        } else {
            return false}}Copy the code

As with the removeAt method implementation in the previous article, we first need to check if the position passed is out of bounds. If it is, we return false to indicate that the item was not added to the list.

Next we need to deal with different scenarios, which determine whether position is inserted at the beginning of the list, the end of the list, or the middle of the list.

In the above code, when position is 0, we need to set the next property of the node item (to be added to the list) from the previous step to current (the first element in the list). Head and Node. next point to current. Then you need to change the reference to head to Node. Insert node to the first position in the list.

When position is not 0, we need to loop through the list to find the target position. In the loop body, assign current to previous and set current to cuurent.next to be used in the next loop. When out of the loop body, current will be a reference to the element after the position at which the new element is to be inserted, and previous will be a reference to the element before the position at which the new element is to be inserted. In this case, we add a new item between current and Previous. Therefore, we need to point the next of node to current and the next of previous to node.

Finally, be sure to update the length of the list

Other ways to implement linked lists will be continued in the next section