数据结构——用js实现链表

最近复习数据结构,使用 ES6 语法实现了链表,若有更好的方法,希望大佬们指点指点,谢谢~ ????

实现的操作链表函数:

  • append 在链表尾部添加一个新的项
  • insert 向列表的特定位置插入一个新的项
  • removeAt 从列表特定位置移除一项
  • remove 从列表中移除一项
  • indexOf 返回元素在列表中的索引
  • isEmpty 是否是为空链表
  • size 返回链表包含的元素个数
  • getHead 返回表头
  • toString 输出元素的值

代码

//节点
class Node{
  constructor(element){
    this.element = element;//节点内容
    this.next = null;
  }
}
//链表
class LinkedList{
  /**
   * 传入数组创建链表
   * @param {*} array 
   */
  constructor(array){
    this.length = 0;//链表长度
    this.head = null;//链表头
    if(array.length != 0){
      this.head = new Node(array.shift());
      let current = this.head;
      array.forEach(element => {
        let node = new Node(element);
        current.next = node;
        current = node;
      });
      this.length = array.length + 1;
    }
  }

  /**
   * 在链表尾部添加一个新的项
   * @param {*} element 
   */
  append(element){
    let node = new Node(element),
    current;
    if(this.head === null){
      this.head = node;
    }else{
      current = this.head;
      while(current.next){
        current = current.next;
      }
      current.next = node;
    }
    this.length++;
    return this.length;
  };

  /**
   * 向列表的特定位置插入一个新的项
   * @param {*} position 从0开始
   * @param {*} element 
   */
  insert(position, element){
    //检查越界
    if(position >= 0 && position <= this.length){
      let node = new Node(element),
        current = this.head,
        previous,
        index = 0;
      if(position == 0){
        node.next = this.head;
        this.head = node;
      }else{
        while(index < position){
          previous = current;
          current = current.next;
          index++;
        }
        node.next = current;
        previous.next = node;
      }
      this.length++;
      return true;
    }else{
      return false;
    }
  };

  /**
   * 从列表特定位置移除一项
   * @param {*} position 从0开始
   */
  removeAt(position){
    if(position > -1 && position < this.length){
      let current = this.head,
        previous,
        index = 0;
      if(position == 0){
        this.head = current.next;
      }else{
        while(index < position){
          previous = current;
          current = current.next;
          index++;
        }
        previous.next = current.next;
      }
      this.length--;
      return current.element;
    }else{
      return null;
    }
  };

  /**
   * 从列表中移除一项
   * @param {*} element 
   */
  remove(element){
    if(element != null){
      let current = this.head,
        previous;
      if(element === this.head.element){
        this.head = current.next();
        this.length--;
        return current.element;
      }else{
        while(current.element != element && current.next != null){
          previous = current;
          current = current.next;
        }
        if(current.element == element){
          previous.next = current.next;
          this.length--;
          return current.element;
        }
      }
    }
    return false;
  };

  /**
   * 返回元素在列表中的索引
   * @param {*} element 
   */
  indexOf(element){
    let current = this.head,
      index = -1;
    while(current){
      if(element === current.element){
        return index + 1;
      }
      index++;
      current = current.next;
    }
    return -1;
  };

  /**
   * 是否是为空链表
   * 如果列表中不包含任何元素,返回true
   */
  isEmpty(){
    return this.length === 0;
  };

  /**
   * 返回链表包含的元素个数
   */
  size(){
    return this.length;
  };

  /**
   * 返回表头
   */
  getHead(){
    return this.head;
  };

  /**
   * 输出元素的值
   */
  toString(){
    let current = this.head,
        arr = [];
    while(current){
      arr.push(current.element);
      current = current.next;
    }
    return arr.toString();
  };

}

结果

数据结构——用js实现链表
数据结构——用js实现链表