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

输出结果截图:

在这里插入图片描述

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值