用JS手写双向链表LinkedList和删除等等功能

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

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());

输出结果截图:

用JS手写双向链表LinkedList和删除等等功能_第1张图片

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

你可能感兴趣的:(javascript,js,数据结构,链表)