js data structure learning -- linked list

4. Linked list

  • Memory space is discontinuous, which can realize flexible dynamic memory management

  • There is no need to determine the size when creating the linked list, and the size can be expanded as needed

  • When inserting and deleting data, the time complexity can reach O(1)

4.1 train structure of linked list

A linked list is similar to a train. There is a locomotive. The locomotive is connected to a node (carriage). There are passengers (data) in the node (carriage), and this node is connected to the next node, and so on

4.2 common operations of linked list

  • append(element): adds a new item to the end of the list

  • insert(position,element): inserts a new item into a specific position in the list

  • get(position): get the element of the corresponding position

  • indexOf(element): returns the index of the element in the list. If there is no such element in the list, it returns - 1

  • Update (position, element): modify the element at a certain position

  • Remove at (position): removes an item from a specific location in the list

  • remove(element): removes the specified value from the list

  • isEmpty(): returns true if the linked list has no elements; otherwise, returns False

  • size(): returns the number of elements contained in the linked list

  • toString(): because the list item uses Nodeod, you need to override the default toString method inherited from js object to output only the value of the element

4.3 practice of linked list operation

  1. Preview the overall implementation of linked list

    function LinkedList () {
        function Node (data, next) {
            this.data = data
            this.next = next
        }
        this.head = null
        this.length = 0//Records the length of the current array
        //append
        LinkedList.prototype.append = function (element) {}
        LinkedList.prototype.insert = function (position,element) {}
        LinkedList.prototype.get = function (position){}
        LinkedList.prototype.indexOf = function (element){}
        LinkedList.prototype.update = function (positon,element){}
        LinkedList.prototype.removeAt = function (position){}
        LinkedList.prototype.remove = function (element){}
        LinkedList.prototype.isEmpty = function (){}
        LinkedList.prototype.size = function (){}
        LinkedList.prototype.toString = function (){}
    }
    
     
  2. Implementation of append(element) method

    There are two ways to append data to the end of the linked list

    • The linked list itself is empty, and the newly added node is the only node

    • The linked list is not empty. Add nodes after other nodes

    LinkedList.prototype.append = function (element) {
            var newNode = new Node(data)
            if (this.length == 0)//First node
            {
    ​
                this.head = newNode
            }
            else {
                var currentNode = this.head
                while (currentNode.next != null) {//Loop to find the last node
                    currentNode = currentNode.next
                }
                currentNode.next = newNode//Insert new node into
            }
            this.length+=1 
        }

  3. Implementation of insert(position,element) method

    Insert data anywhere

    • Add to first location

      Indicates that the newly added node is the head node. At this time, the original head node needs to be used as the next of the new node, and the head should point to the new node

    • Add to another location

      • First, through the while loop, you need to traverse from the beginning to find the location where the new node needs to be inserted, and save the previous node and the next node in the search process

      • When the correct location is found, the next of the new node points to the next node, and the next of the previous node points to the new node

    LinkedList.prototype.insert = function (position, data) {
            var currentNode = this.head
            var index = 0
            var newNode = new Node(data)
            if (position < 0 || position > this.length)
                return false
            if (position == 0) {
                this.head = newNode
                newNode.next = currentNode
            }
            else {
                while (currentNode != null) {
                    if (index++ == position - 1)
                        break
                    else {
                        currentNode = currentNode.next
                    }
                }
                var q = currentNode.next
                currentNode.next = newNode
                newNode.next = q
            }
            this.length += 1
            return true
    ​
        }
  4. Implementation of toString() method

    LinkedList.prototype.toString = function () {
            currentNode = this.head
            let elements = ''
            while (currentNode != null) {
                elements += currentNode.data + ' '
                currentNode = currentNode.next
            }
            return elements
        }
  5. get(position) method

    LinkedList.prototype.get = function (position) {
            var currentNode = this.head
            var index = 0
            if (position < 0 || position >= this.length)
                return false
            else {
                while (index++ < position) {
                    currentNode = currentNode.next
                }
                return currentNode.data
            }
        }
  6. indexOf(element) method

    LinkedList.prototype.indexOf = function (element) {
            var currentNode = this.head
            var index = 0
            while (currentNode != null) {
                if (currentNode.data == element)
                    return index
                else currentNode = currentNode.next
                index += 1
            }
            return -1
        }
  7. removeAt(position) method

    LinkedList.prototype.removeAt = function (position) {
        var index = 0
        var currentNode = this.head
        if (position < 0 || position >= this.length) return false
        if (position == 0)
            this.head = this.head.next
        else{
             while (index+1 < position) {
                currentNode = currentNode.next
                index+=1
            }
                currentNode.next = currentNode.next.next
        }
       
        
        this.length -= 1
        return true
    }
  8. update(position,element) method

     LinkedList.prototype.update = function (position, element) {
            var currentNode = this.head
            var index = 0
            if (position < 0 || position >= this.length) return false
            while (index++ < position) {
                currentNode = currentNode.next
            }
            currentNode.data = element
            return true
        }

  9. remove(element) method

    //Method 1:
      LinkedList.prototype.remove = function (element) {
        var currentNode = this.head
        var prev = this.head
        if (this.length == 0) return false
        if (this.length == 1 && element == currentNode.element) {
            this.head = null
             }
        else {
            while (currentNode != null) {
            if (element == currentNode.element) {
              if (currentNode === this.head) {
                 this.head = currentNode.next}
                  prev.next = currentNode.next
                   break }
                  else {
                   prev = currentNode
                   currentNode = currentNode.next
                            }
                        }
                    }
    this.length-=1
    return true
                }
    //Method 2
    LinkedList.prototype.remove = function (element) {
        var position = this.indexOf(element)
        this.removeAt(position)
    }

  10. isempty() method

    LinkedList.prototype.isEmpty = function () {
        if (this.length == 0) return true
        else return false
    }

  11. size() method

    LinkedList.prototype.size = function () {
        return this.length
    }

Tags: Javascript data structure

Posted on Sat, 20 Nov 2021 10:28:08 -0500 by BluePhoenixNC