JavaScript doubly linked list data structure and algorithm study notes of (3)

Remove elements from anywhere

Remove from the doubly linked list elements are very similar with the list. The only difference is that you need to set the pointer in front of a position. We look at its implementation:

    this.removeAt = function(position){
        //检查越界值
        if(position > -1 && position < length) {
            var current = head,
            previous,
            index = 0;
            //移除第一项
            if(position === 0){
                head = current.next;// {1}
                //如果只有一项,更新tail //新增的
                if(length === 1) {// {2}
                    tail = null;
                } else {
                    head.prev = null;// {3}
                }
            } else if( position === length - 1){//最后一项 //新增的
                current = tail;// {4}
                tail = current.prev;
                tail.next = null;
            } else{
                while(index++ < position){// {5}
                    previous = current;
                    current = current.next;
                }
                //将previous与current的下一项链接起来——跳过current
                previous.next = current.next;// {6}
                current.next.prev = previous;//新增的
            }
            length--;
            return current.element;
        } else {
            return null;
        }
    }

We need to deal with three scenarios: removing an element from the middle and from the tail from the head.

Let's see how to remove the first element.

  1. current variable is a reference to the first element of the list, which is the element we want to remove.
  2. Need to do is to change the reference head, to read the next element (current.next-- row {1}) from the current.
  3. But we also need to update current.next point to an element pointer (prev pointer as the first element is null).
  4. Accordingly, references to the head.prev null (line {3} - also head point list as a new first element, or may be used current.next.prev).
  5. As the need to control the tail of reference, we can check if you want to remove the element is the first element, if it is, simply put tail is also set to null (line {2}).

The following diagram outlines the process removes the first element from a doubly linked list:

 One scenario is to remove the element from the last position.

  1. Now that you have a reference to the last element (tail), we do not need to find it and iterate through the list.
  2. And we also can assign the tail current reference variable (row {4}).
  3. Next, we need to update the list of references tail penultimate element (current.prev, or tail.prev can).
  4. Since the tail pointing to the penultimate element, we just need to update the next pointer is null (tail.next = null).

The following figure illustrates this behavior:

The third and final scenario: removing an element from the middle of the list.

  1. First, a list of required iterations, until reaching a position looking ({5} lines).
  2. current variable is referenced to remove elements.
  3. So you want to remove it, we can update the reference previous.next and current.next.prev skipped it in the list.
  4. Therefore, previous.next will point current.next, and current.next.prev will point to previous.

As shown below: 

 

 

发布了72 篇原创文章 · 获赞 6 · 访问量 2万+

Guess you like

Origin blog.csdn.net/hongyu799/article/details/104127912