数据结构-链表

121 阅读2分钟

Javascript数据结构

本文参考链接 数据结构是相互之间存在一种或多种特定关系的数据元素的集合

  • 数组(Array) 特点:查询快,增删慢。原因:连续的内存空间(一整块内存)
  • 栈(Stack) 特点:先入后出
  • 堆 (Heap) 特点:一种特殊的树形数据结构,一般讨论的堆都是二叉堆。堆的特点是根结点的值是所有结点中最小的或者最大的,并且根结点的两个子树也是一个堆结构。
  • 队列 (Queue) 特点:先入先出
  • 链表(Linked List) 特点:内存中非连续,无序,查询慢,增删快
  • 树(Tree)
  • 图(Graph)
  • 散列表(Hash) 疑问点:js中数据结构Set和Map的数据结构和上面提到的接种数据结构类型有什么关系,望大神指教!

单向链表

链表每个数据节点有数据域和指针域组成,即链表中每个数据单元有数据本身和指向下一个数据引用位置的指针组成。

image.png

封装一个链表类:参考地址

  1. 封装链表类,子类
// 封装链表类
function LinkedList(){
    this.currentNode = null;
    this.length = 0;
}

// 封装子类
function Node(data){
    this.data = data;
    this.next= null;
}
  1. 新增append()
// 增加节点
LinkedList.prototype.append = function(data){
    let newNode = new Node(data); // 新节点内容
    if(this.length === 0){ // 链表长度为0,直接将新节点赋值给当前节点
        this.currentNode = newNode;
    }else{ // 链表长度不为0,去除当前节点,判断当前节点的next是否为null,为null即为最后一个节点,将新节点赋值给最后一个节点的next上
        let current = this.currentNode;
        while(current.next){
            current = current.next;
        }
        current.next = newNode;
    }
    this.length += 1;
}
  1. 移除remove()
// 移除节点
LinkedList.prototype.remove = function(position){
    // 判断边界
    if(position < 0 || position >= this.length){
        return;
    }
    let current = this.currentNode; // 当前节点信息
    if(position === 0){
        this.currentNode = this.currentNode.next;
    }else{
        let index = 0,previous = null; // 从头开始取出data,直到取到position数据
        while(index++ < position){
            previous = current;
            current = current.next
        }
        previous.next = current.next;
    }
    this.length -= 1;
}
  1. 更新节点update()
// 更新节点
LinkedList.prototype.update = function(position,element){
    // 判断边界
    if(position < 0 || position >= this.length){
        return;
    }
    let current = this.currentNode; // 当前节点信息
    let index = 0; // 从头开始取出data,直到取到position数据
    while(index++ < position){ // 当遍历到position位置时将position数据取出来
        current = current.next
    }
    current.data = element;
}
  1. 获取节点数据get()
// 获取节点数据
LinkedList.prototype.get = function(position){
    // 判断边界
    if(position < 0 || position >= this.length){
        return;
    }
    let current = this.currentNode; // 当前节点信息
    let index = 0; // 从头开始取出data,直到取到position数据
    while(index++ < position){ // 当遍历到position位置时将position数据取出来
        current = current.next
    }
   return current.data;
}
  1. 插入数据insert()
// 插入数据
LinkedList.prototype.insert = function(position,data){
    let newNode = new Node(data); // 新节点内容
     // 判断边界
     if(position < 0 || position >= this.length){
        return;
    }
    // 当前节点信息
    if(position === 0){
        newNode.next = this.currentNode;
        this.currentNode = newNode;
    }else{
        let current = this.currentNode; 
        let index = 0, previous = null;
        while(index++ < position){
            previous = current; // 保存position位置之前数据
            current = current.next; // 取出position位置数据
        }
        console.log(current,'0000')
        newNode.next = current;
        previous.next = newNode;
        // this.currentNode = previous;
    }
    this.length += 1;
}