用JS手写单链表LinkedList和删除等等性能

原创
小哥 3年前 (2022-11-14) 阅读数 38 #大杂烩

开始实现我们的数据结构,直接进入代码

class LinkedList {
  constructor(equalsFn = defaultEquals) {
    this.count = 0;
    this.head = undefined;
    this.equalsFn = equalsFn;
  }
  // 插入链接列表的尾部
  push(element) {
    var node = new Node(element);
    let cur;
    if (this.head == null) {
      this.head = node;
    } else {
      cur = this.head;
      while (cur.next != null) {
        cur = cur.next;
      }
      cur.next = node;
    }
    this.count++;
  }
  removeFront() {
    let cur;
    if (this.head == null) {
      return;
    } else {
      this.head = this.head.next;
    }
    this.count--;
  }
  // 删除指定的位置元素
  removeAt(index) {
    if (index > 0 && index < this.count) {
      let cur = this.head;
      if (index === 0) {
        this.head = cur.next;
      } else {
        for (let i = 0; i < index; i++) {
          var pre = cur;
          cur = cur.next;
        }
        pre.next = cur.next;
      }
      this.count--;
      return cur.element;
    }
    return undefined;
  }
  // 通过index索引查找元素
  getElementAt(index) {
    if (index >= 0 && index <= this.count) {
      let cur = this.head;
      if (index === 0) {
        this.head = cur.next;
      } else {
        for (let i = 0; i < index && cur != null; i++) {
          cur = cur.next;
        }
      }
      return cur;
    }
    return undefined;
  }
  // 重构remove方法, 通过我们上面写的getElementAt
  remove(index) {
    if (index > 0 && index < this.count) {
      let cur = this.head;
      if (index === 0) {
        this.head = cur.next;
      } else {
        const pre = this.getElementAt(index - 1);
        cur = pre.next;
        pre.next = cur.next;
      }
      this.count--;
    }
  }
  // 插入操作
  insert(element, index) {
    if (index >= 0 && index <= this.count) {
      const node = new Node(element);
      if (index === 0) {
        const cur = this.head;
        node.next = cur;
        this.head = node;
      } else {
        const pre = this.getElementAt(index - 1);
        const cur = pre.next;
        node.next = cur;
        pre.next = node;
      }
      this.count++;
      return true;
    }
    return false;
  }
  // 吧LinkedList 对象将转换为字符串。
  toString() {
    if (this.head == null) {
      return "";
    }
    let objString = ${this.head.element};
    let current = this.head.next;
    for (let i = 0; i < this.count && current != null; i++) {
      objString = ${objString},${current.element};
      current = current.next;
    }
    return objString;
  }
}

function defaultEquals(a, b) {
  return a === b;
}
// 节点类
class Node {
  constructor(element) {
    this.element = element;
    this.next = undefined;
  }
}

var list = new LinkedList();
list.push(5);
list.push(10);
list.push(15);
list.push(20);
// list.removeFront();
if (list.insert(30, 4)) {
  console.log(---------------成功插入-------------------);
} else {
  console.log(---------------插入失败--------------------);
}

console.log(--------找到:${list.getElementAt(1)} 元素------------);
console.log(list);
var a = list.removeAt(4);
console.log(--------已删除:${a} 元素------------);
console.log(list.toString());

输出结果截图:

如果你自己写这些东西,你可以加深你的记忆, 帮助理解

版权声明

所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除

热门