放肆青春的博客
首页
前端
算法
网络
面试
技术
后端
运维
杂项
数据库
工具
网址
电脑
个人
文章
  • 分类
  • 标签
  • 归档
github (opens new window)
gitee (opens new window)

放肆青春

一个前端菜鸟的技术成长之路
首页
前端
算法
网络
面试
技术
后端
运维
杂项
数据库
工具
网址
电脑
个人
文章
  • 分类
  • 标签
  • 归档
github (opens new window)
gitee (opens new window)
  • 算法基础

    • 数据结构和算法
    • 算法blog
    • 算法基础
    • 算法思维
  • 算法汇总

    • 常见算法题
    • 穷举算法(枚举算法)
    • 递归
    • 二分法
    • 双指针

      • 双指针
      • 双指针算法题
      • 滑动窗口
      • 滑动窗口算法题
    • 回溯法

      • 回溯法
      • 回溯法算法题
    • 分治算法
    • 贪心算法

      • 贪心算法
      • 贪心算法题
    • 动态规划

      • 动态规划
      • 动态规划算法题
  • 排序算法

    • 排序算法
    • 1. 冒泡排序
    • 2. 选择排序
    • 3. 插入排序
    • 4. 希尔排序
    • 5. 归并排序
    • 6. 快速排序
    • 7. 堆排序
    • 8. 计数排序
    • 9. 桶排序
    • 10. 基数排序
  • 数据结构

    • 数组

      • 数组概览
      • 数组算法题
    • 字符串

      • 字符串概览
      • 字符串算法题
    • 树

      • 树概览
      • 树算法题
    • 链表

      • 链表概览
      • 双向链表
        • 双向链表
          • 代码实现
      • 链表算法题
    • 队列

      • 队列概览
      • 队列算法题
    • 堆

      • 堆概览
      • 堆算法题
    • 散列表(哈希表)

      • 散列表(哈希表)概览
      • 散列表(哈希表)算法题
    • 栈

      • 栈概览
      • 栈算法题
    • 图

      • 图概览
      • 图算法题
  • algorithm
放肆青春
2020-11-27

双向链表

# 双向链表

# 代码实现

class Node {
  data;
  next = null;
  previous = null;
  constructor(data) {
    this.data = data;
  }
}

class DoublyLinkedList {
  length = 0;
  // 存放头结点的引用
  head = null;
  // 存放尾结点的引用
  tail = null;

  // 双向链表的方法
  // append(data) 向尾部添加一个节点
  append(data) {
    // 1. 创建新的链表节点
    const newNode = new Node(data);

    // 追加元素
    if (this.length === 0) {
      // 如果链表为空的话
      this.head = newNode;
      this.tail = newNode;
    } else {
      // 追加到链表尾部
      // 巧妙的一点是不用循环找链表的尾部节点,现在 tail 中有尾节点的引用
      this.tail.next = newNode;
      newNode.previous = this.tail;
      this.tail = newNode;
    }
    // 长度增加
    this.length++;
  }

  // insert(position, data) 指定位置插入数据
  insert(position, data) {
    // 判断是否越界
    if (position < 0 || position > this.length) return false;

    // 创建新的链表节点
    const newNode = new Node(data);

    // 追加链表节点;共用一下几种情况:
    // 1. 空链表追加到头部
    // 2. 链表有数据追加到头部
    // 3. 追加到尾部
    // 4. 0----this.length 之间的追加
    if (position === 0) {
      // 追加到头部
      if (this.head === null) {
        // 链表无数据添加到头部
        this.head = newNode;
        this.tail = newNode;
      } else {
        // 链表有数据添加到头部
        newNode.next = this.head;
        this.head.previous = newNode;
        this.head = newNode;
      }
    } else if (position === this.length) {
      // 在链表尾部插入节点
      this.tail.next = newNode;
      newNode.previous = this.tail;
      this.tail = newNode;
    } else {
      // 0 --- this.length 中间插入

      // 准备变量
      let targetIndex = 0; // 代表节点位置的索引
      let currentNode = this.head;
      let previousNode = null; // 代表上一个节点

      // 循环查找节点
      while (targetIndex++ < position) {
        previousNode = currentNode;
        currentNode = currentNode.next;
      }

      // 插入
      previousNode.next = newNode;
      newNode.previous = previousNode;

      newNode.next = currentNode;
      currentNode.previous = newNode;
    }

    // 增加长度
    this.length++;
    return true;
  }

  // getData(position) 获取指定位置的链表节点
  getData(position = 0) {
    // 判断越界
    if (position < 0 || position >= this.length) return false;
    // 循环查找位置

    let targetIndex = 0; // 代表节点位置的索引
    let currentNode = this.head;

    while (targetIndex++ < position) {
      currentNode = currentNode.next;
    }
    // 返回
    return currentNode.data;
  }

  // indexOf(data) 查找数据对应的index
  indexOf(data) {
    // 循环查找数据

    let targetIndex = 0;
    let currentNode = this.head;

    while (currentNode) {
      if (data === currentNode.data) {
        return targetIndex;
      }
      currentNode = currentNode.next;
      targetIndex++;
    }
    // 否则返回-1
    return -1;
  }

  // removeAt(position) 删除指定位置的节点
  removeAt(position) {
    // 判断是否越界
    if (position < 0 || position >= this.length) return false;
    let currentNode = this.head;

    // 根据不同的情况删除
    if (position === 0) {
      // 1. 删除头部的情况
      if (this.length === 1) {
        // 如果链表只有一个节点的情况
        this.head = null;
        this.tail = null;
      } else {
        // 如果链表有多个节点
        this.head = this.head.next;
        this.head.previous = null;
      }
    } else if (position === this.length) {
      // 删除链表最后的一个节点
      this.tail.previous.next = null;
      this.tail = this.tail.previous;
    } else {
      // 0-----this.length 的情况
      let targetIndex = 0;
      let previousNode = null;

      // 循环找到positions位置
      while (targetIndex++ < position) {
        previousNode = currentNode;
        currentNode = currentNode.next;
      }

      // 删除
      previousNode.next = currentNode.next;
      currentNode.next.previous = previousNode;
    }
    // 长度递减
    this.length--;
    return currentNode.data;
  }

  // update(position, data) 修改指定位置的节点
  update(position, data) {
    // 判断边界
    if (position < 0 || position >= this.length) return false;

    // 循环找的位置
    let targetIndex = 0;
    let currentNode = this.head;

    while (targetIndex++ < position) {
      currentNode = currentNode.next;
    }
    currentNode.data = data;
    return currentNode.data;
  }

  //remove(data) 删除指定 data 所在的节点(继承单向链表)

  remove(data) {
    return this.removeAt(this.indexOf(data));
  }

  // isEmpty() 判断链表是否为空
  isEmpty() {
    return this.length === 0;
  }
  // size() 获取链表的长度
  size() {
    return this.length;
  }
  // forwardToString() 链表数据从前往后以字符串形式返回
  forwardToString() {
    let currentNode = this.head;
    let result = "";

    // 遍历所有的节点,拼接为字符串,直到节点为 null
    while (currentNode) {
      result += currentNode.data + "--";
      currentNode = currentNode.next;
    }

    return result;
  }

  // backwardString() 链表数据从后往前以字符串形式返回
  backwardString() {
    let currentNode = this.tail;
    let result = "";

    // 遍历所有的节点,拼接为字符串,直到节点为 null
    while (currentNode) {
      result += currentNode.data + "--";
      currentNode = currentNode.previous;
    }

    return result;
  }
}

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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230

参考:https://juejin.cn/post/6893428188655910926#heading-3 (opens new window)

更新时间: 3/22/2021, 4:43:19 PM
链表概览
链表算法题

← 链表概览 链表算法题→

最近更新
01
前端权限管理
02-24
02
vue2指令
02-24
03
vue2 hook
02-24
更多文章>
Theme by Vdoing | Copyright © 2019-2022 放肆青春
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式