惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

小众软件
小众软件
量子位
阮一峰的网络日志
阮一峰的网络日志
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
美团技术团队
J
Java Code Geeks
Apple Machine Learning Research
Apple Machine Learning Research
腾讯CDC
V
Visual Studio Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 三生石上(FineUI控件)
IT之家
IT之家
博客园 - 【当耐特】
L
LangChain Blog
A
About on SuperTechFans
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
N
Netflix TechBlog - Medium
博客园_首页
WordPress大学
WordPress大学
博客园 - Franky
Engineering at Meta
Engineering at Meta
C
Check Point Blog
aimingoo的专栏
aimingoo的专栏
M
MIT News - Artificial intelligence

时间的朋友

Windows 命令行密码重置 Anaconda安装 typescript 注解解读1 Konvajs Shape加载自定义图片 sshpass 使用 why-is-node-running webgl笔记 SharedArrayBuffer is not defined blender 常用快捷键 | 时间的朋友 vue -- v3.4commit提交记录2 vue2 升级vue3报错问题整理 着色器 expressjs 源码 hyper-V arch linux 网络配置 element input数字格式化 three 拼接货架 WebAudio笔记 Windows nginx重启bat脚本 vue -- v3.4commit提交记录 URI malformed vue3 -- Class 对象在组件中使用范例 | 时间的朋友 ruby 安装和升级 element-plus 老版本cascader使用卡死问题 vue3 内置Transition组件 | 时间的朋友 前端memo的实现 | 时间的朋友 Vue -- vue-class-component源码 | 时间的朋友 linux 优化脚本 typescript 装饰器 | 时间的朋友 microbundle 源码 | 时间的朋友 WSL2问题解决WslRegisterDistribution failed with error: 0x800701bc
单向链表 | 时间的朋友
2023-06-20 · via 时间的朋友

单向链表

Published: · LastMod: June 20, 2023 · 429 words

链表 🔗

链表是一种物理储存上非联系,数据元素的逻辑顺序通过链表中的指针链接次序,实现的一种线性储存结构。

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
class LinkNode {
  value: string;
  next: LinkNode;
  constructor(value: string) {
    this.value = value;
  }
}

class LinkedList {
  head: LinkNode;
  tail: LinkNode;
  constructor() {}

  prepend(value: string) {
    const node = new LinkNode(value);
    node.next = this.head;
    this.head = node;

    if (!this.tail) {
      this.tail = node;
    }

    return this;
  }

  append(value: string) {
    const node = new LinkNode(value);

    if (!this.head && !this.tail) {
      this.head = node;
      this.tail = node;
      return this;
    }

    this.tail.next = node;
    this.tail = node;
    return this;
  }

  insert(value: string, index: number) {
    if (index === 0) {
      return this.prepend(value);
    } else {
      let count = 1;
      let current = this.head;
      const node = new LinkNode(value);

      while (current) {
        if (count === index) break;
        current = current.next;
        count++;
      }

      if (current) {
        node.next = current.next;
        current.next = node;
      } else {
        this.tail.next = node;
        this.tail = node;
      }
    }
    return this;
  }

  delete(value: string, compare: Function) {
    if (!this.head) return null;

    let deleteNode: LinkNode | null = null;

    let current = this.head;

    while (current) {
      if (compare(current.value, value)) {
        deleteNode = current;
        current.next = current.next.next;

        if (current === this.head) {
          this.head = current.next;
        }
      } else {
        current = current.next;
      }
    }

    return deleteNode;
  }

  find(value: string, compare: Function) {
    if (!this.head) return null;

    let current = this.head;

    while (current) {
      if (compare(current.value, value)) {
        return current;
      } else {
        current = current.next;
      }
    }
    return null;
  }

  fromArray(array: string[]) {
    array.forEach((value) => this.append(value));
    return this;
  }

  toArray() {
    const nodes: any = [];
    let current = this.head;
    while (current) {
      nodes.push(current);
      current = current.next;
    }
    return nodes;
  }
}