数据结构2--链表

数组的特点:

  1. 数组的创建通常需要申请一段连续的内存空间(一整块的内存), 并且大小是固定的(大多数编程语言数组都是固定的),
  2. 如果当前数组不能满足容量需求时, 需要扩容。(一般情况下是申请一个更大的数组, 比如2倍. 然后将原数组中的元素复制过去)。

链表的特点:

  1. 链表中的元素在内存中不必是连续的空间,可以充分利用计算机的内存实现灵活的内存动态管理。
  2. 链表的每个元素由一个存储元素本身的节点和一个指向下一个元素的引用。内存空间不是比是连续的。
  3. 链表不必在创建时就确定大小, 并且大小可以无限的延伸下去.
  4. 链表在插入和删除数据时, 时间复杂度可以达到O(1). 相对数组效率高很多.
  5. 链表访问任何一个位置的元素时, 都需要从头开始访问.(无法跳过第一个元素访问任何一个元素).

在这里插入图片描述

//封装链表
function LinkedList(){
    this.headNode = null ;//设置头部结点
    this.length = 0;
    function NewNode(data){
        this.data = data;
        this.next = null;
    }

    /*
    向链表尾部追加数据可能有两种情况:
    1.链表本身为空, 新添加的数据时唯一的节点.
    2.链表不为空, 需要向其他节点后面追加节点.
     */
    LinkedList.prototype.appendNode = function (data) {
        let newNode = new NewNode(data);

        if(this.length == 0){
            this.headNode = newNode; //如果链表为空,将插入的节点设置为头部节点
        }else{
            //链表不为空 从头部节点开始遍历一直找到链表的最后一个节点 然后将要插入的节点设置为最后一个节点的next
            let currentNode = this.headNode;
            while(currentNode.next){
                currentNode = currentNode.next;
            }
            currentNode.next = newNode;
        }
        this.length +=1; //链表长度加1
    }

    /*
    根据传递的位置获取对应位置的元素
     */
    LinkedList.prototype.getData = function(position){
        //position判断,防止越界
        if(position <0 || position > this.length - 1) return false;
        let index =0 ;//从头节点开始遍历
        let currentNode = this.headNode;
        while (index < position){
            currentNode = currentNode.next;
            index++;
        }
        return currentNode.data;
    }


    /*
    * 根据传递的位置和新数据更新
     */
    LinkedList.prototype.updateData = function(position,newData){
        //位置越界判断
        if(position < 0 || position > this.length-1) return false;
        let index = 0;
        let currentNode = this.headNode;
        while(index < position){
            index ++;
            currentNode = currentNode.next;
        }
        currentNode.data = newData;
    }

    /*
    * 根据传递的数据获取对应的位置
     */
    LinkedList.prototype.getIndex = function(data){
         let index = 0;
         let currentNode = this.headNode;
         while (currentNode.data !== data){
             currentNode = currentNode.next;
             index ++;
         }
         if(index > this.length-1) return -1;
         return index;
    }

    /*
    向任意位置插入元素的两种情况:
    1.向头节点插入
    2.向其他位置插入
     */
    LinkedList.prototype.insertNode = function (data,position) {
        //需要传递两个参数,一个是要插入的节点的数据,一个是要插入的位置
        //position越界判断
        if(position < 0 || position > this.length) return false;
        //positon == this.length 表示要向链表的尾部插入数据
        let currentNode = this.headNode;
        let newNode = new NewNode(data);//创建要插入的节点
        let index = 0;
        let previousNode = null;
        //1.判断是否要向头节点插入
        if(position == 0){
            newNode.next = this.headNode;
            this.headNode = newNode;
        }else{
            while(index < position){
                previousNode = currentNode;
                currentNode = currentNode.next;
                index++;
            }
            previousNode.next = newNode;
            newNode.next = currentNode;
        }
    }

    /*
    根据传递的索引删除对应位置的节点 两种情况
    1.删除头节点
    2.删除其他位置的节点
     */
    LinkedList.prototype.removeAt = function(position){
        //位置越界判断
        if(position<0 || position > this.length -1) return -1;
        let index = 0;
        let currentNode = this.headNode;
        let previousNode = null;
        if(position == 0){
            this.headNode = currentNode.next
        }else{
            while(index < position){
                previousNode = currentNode;
                currentNode = currentNode.next;
                index++;
            }
            previousNode.next = currentNode.next;
        }
        this.length -=1;
    }

    /*
    根据传递的数据删除对应位置的节点
     */
    LinkedList.prototype.removeNode =  function (data) {
        let position = this.getIndex(data);
        this.removeAt(position);
    }
}

发布了136 篇原创文章 · 获赞 1 · 访问量 8723

猜你喜欢

转载自blog.csdn.net/weixin_42139212/article/details/104561757
今日推荐