链表和数组一样,可以用于存储一系列的元素,但是链表和数组的实现机制完全不同。
存储多个元素,数组(或列表)可能是最常用的数据结构。
几乎每一种编程语言都有默认实现数组结构,提供了一个便利的 []
语法来访问数组元素。
数组缺点:
数组的创建需要申请一段连续的内存空间 (一整块内存),并且大小是固定的,当前数组不能满足容量需求时,需要扩容。 (一般情况下是申请一个更大的数组,比如 2 倍,然后将原数组中的元素复制过去)
在数组开头或中间位置插入数据的成本很高,需要进行大量元素的位移。
存储多个元素,另外一个选择就是使用链表。
不同于数组,链表中的元素在内存中不必是连续的空间。
链表的每个元素由一个存储元素本身的节点和一个指向下一个元素的引用 (有些语言称为指针) 组成。
链表优点:
内存空间不必是连续的,可以充分利用计算机的内存,实现灵活的内存动态管理。
链表不必在创建时就确定大小,并且大小可以无限延伸下去。
链表在插入和删除数据时,时间复杂度可以达到 O(1),相对数组效率高很多。
链表缺点:
访问任何一个位置的元素时,需要从头开始访问。(无法跳过第一个元素访问任何一个元素)
无法通过下标值直接访问元素,需要从头开始一个个访问,直到找到对应的元素。
虽然可以轻松地到达下一个节点,但是回到前一个节点是很难的。
单向链表类似于火车,有一个火车头,火车头会连接一个节点,节点上有乘客,并且这个节点会连接下一个节点,以此类推。
链表的火车结构
链表的数据结构
head 属性指向链表的第一个节点。 链表中的最后一个节点指向 null
。 当链表中一个节点也没有的时候,head 直接指向 null
。
给火车加上数据后的结构
append(element)
向链表尾部添加一个新的项。insert(position, element)
向链表的特定位置插入一个新的项。get(position)
获取对应位置的元素。indexOf(element)
返回元素在链表中的索引。如果链表中没有该元素就返回 -1。update(position, element)
修改某个位置的元素。removeAt(position)
从链表的特定位置移除一项。remove(element)
从链表中移除一项。isEmpty()
如果链表中不包含任何元素,返回 trun
,如果链表长度大于 0 则返回 false
。size()
返回链表包含的元素个数,与数组的 length 属性类似。toString()
由于链表项使用了 Node 类,就需要重写继承自 JavaScript 对象默认的 toString 方法,让其只输出元素的值。先创建单向链表类 LinkedList,添加基本属性,再逐步实现单向链表的常用方法。
class LinkedList {// 初始链表长度为 0length = 0;// 初始 head 为 null,head 指向链表的第一个节点head = null;// 内部类(链表里的节点 Node)Node = class {data;next = null;constructor(data) {this.data = data;}};}
代码实现:
// append() 往链表尾部追加数据append(data) {// 1、创建新节点const newNode = new this.Node(data);// 2、追加新节点if (this.length === 0) {// 链表长度为 0 时,即只有 head 的时候this.head = newNode;} else {// 链表长度大于 0 时,在最后面添加新节点let currentNode = this.head;// 当 currentNode.next 不为空时,// 循序依次找最后一个节点,即节点的 next 为 null 时while (currentNode.next !== null) {currentNode = currentNode.next;}// 最后一个节点的 next 指向新节点currentNode.next = newNode;}// 3、追加完新节点后,链表长度 + 1this.length++;}
过程图解:
首先让 currentNode
指向第一个节点。
通过 while
循环使 currentNode
指向最后一个节点,最后通过 currentNode.next = newNode
,让最后一个节点指向新节点 newNode
。
代码测试:
const linkedList = new LinkedList();// 测试 append 方法linkedList.append("A");linkedList.append("B");linkedList.append("C");console.log(linkedList);
代码实现:
toString() {let currentNode = this.head;let result = '';// 遍历所有的节点,拼接为字符串,直到节点为 nullwhile (currentNode) {result += currentNode.data + ' ';currentNode = currentNode.next;}return result;}
代码测试:
// 测试 toString 方法console.log(linkedList.toString()); //--> AA BB CC
代码实现:
// insert() 在指定位置(position)插入节点insert(position, data) {// position 新插入节点的位置// position = 0 表示新插入后是第一个节点// position = 1 表示新插入后是第二个节点,以此类推// 1、对 position 进行越界判断,不能小于 0 或大于链表长度if (position < 0 || position > this.length) return false;// 2、创建新节点const newNode = new this.Node(data);// 3、插入节点if (position === 0) { // position = 0 的情况// 让新节点的 next 指向 原来的第一个节点,即 headnewNode.next = this.head;// head 赋值为 newNodethis.head = newNode;} else { // 0 < position <= length 的情况// 初始化一些变量let currentNode = this.head; // 当前节点初始化为 headlet previousNode = null; // head 的 上一节点为 nulllet index = 0; // head 的 index 为 0// 在 0 ~ position 之间遍历,不断地更新 currentNode 和 previousNode// 直到找到要插入的位置while (index++ < position) {previousNode = currentNode;currentNode = currentNode.next;}// 在当前节点和当前节点的上一节点之间插入新节点,即它们的改变指向newNode.next = currentNode;previousNode.next = newNode;}// 更新链表长度this.length++;return newNode;}
代码测试:
// 测试 insert 方法linkedList.insert(0, "123");linkedList.insert(2, "456");console.log(linkedList.toString()); //--> 123 AA 456 BB CC
获取指定位置(position)的 data。
代码实现:
getData(position) {// 1、position 越界判断if (position < 0 || position >= this.length) return null;// 2、获取指定 position 节点的 datalet currentNode = this.head;let index = 0;while (index++ < position) {currentNode = currentNode.next;}// 3、返回 datareturn currentNode.data;}
代码测试:
// 测试 getData 方法console.log(linkedList.getData(0)); //--> 123console.log(linkedList.getData(1)); //--> AA
indexOf(data) 返回指定 data 的 index,如果没有,返回 -1。
代码实现:
indexOf(data) {let currentNode = this.head;let index = 0;while (currentNode) {if (currentNode.data === data) {return index;}currentNode = currentNode.next;index++;}return -1;}
代码测试:
// 测试 indexOf 方法console.log(linkedList.indexOf("AA")); //--> 1console.log(linkedList.indexOf("ABC")); //--> -1
update(position, data) 修改指定位置节点的 data。
代码实现:
update(position, data) {// 涉及到 position 都要进行越界判断// 1、position 越界判断if (position < 0 || position >= this.length) return false;// 2、痛过循环遍历,找到指定 position 的节点let currentNode = this.head;let index = 0;while (index++ < position) {currentNode = currentNode.next;}// 3、修改节点 datacurrentNode.data = data;return currentNode;}
代码测试:
// 测试 update 方法linkedList.update(0, "12345");console.log(linkedList.toString()); //--> 12345 AA 456 BB CClinkedList.update(1, "54321");console.log(linkedList.toString()); //--> 12345 54321 456 BB CC
removeAt(position) 删除指定位置的节点。
代码实现:
removeAt(position) {// 1、position 越界判断if (position < 0 || position >= this.length) return null;// 2、删除指定 position 节点let currentNode = this.head;if (position === 0) {// position = 0 的情况this.head = this.head.next;} else {// position > 0 的情况// 通过循环遍历,找到指定 position 的节点,赋值到 currentNodelet previousNode = null;let index = 0;while (index++ < position) {previousNode = currentNode;currentNode = currentNode.next;}// 巧妙之处,让上一节点的 next 指向到当前的节点的 next,相当于删除了当前节点。previousNode.next = currentNode.next;}// 3、更新链表长度 -1this.length--;return currentNode;}
代码测试:
// 测试 removeAt 方法linkedList.removeAt(3);console.log(linkedList.toString()); //--> 12345 54321 456 CC
remove(data) 删除指定 data 所在的节点。
代码实现:
remove(data) {this.removeAt(this.indexOf(data));}
代码测试:
// 测试 remove 方法linkedList.remove("CC");console.log(linkedList.toString()); //--> 12345 54321 456
isEmpty() 判断链表是否为空。
代码实现:
isEmpty() {return this.length === 0;}
代码测试:
// 测试 isEmpty 方法console.log(linkedList.isEmpty()); //--> false
size() 获取链表的长度。
代码实现:
size() {return this.length;}
代码测试:
// 测试 size 方法console.log(linkedList.size()); //--> 3
class LinkedList {// 初始链表长度为 0length = 0;// 初始 head 为 null,head 指向链表的第一个节点head = null;// 内部类(链表里的节点 Node)Node = class {data;next = null;constructor(data) {this.data = data;}};// ------------ 链表的常见操作 ------------ //// append() 往链表尾部追加数据append(data) {// 1、创建新节点const newNode = new this.Node(data);// 2、追加新节点if (this.length === 0) {// 链表长度为 0 时,即只有 head 的时候this.head = newNode;} else {// 链表长度大于 0 时,在最后面添加新节点let currentNode = this.head;// 当 currentNode.next 不为空时,// 循序依次找最后一个节点,即节点的 next 为 null 时while (currentNode.next !== null) {currentNode = currentNode.next;}// 最后一个节点的 next 指向新节点currentNode.next = newNode;}// 3、追加完新节点后,链表长度 + 1this.length++;}// insert() 在指定位置(position)插入节点insert(position, data) {// position 新插入节点的位置// position = 0 表示新插入后是第一个节点// position = 1 表示新插入后是第二个节点,以此类推// 1、对 position 进行越界判断,不能小于 0 或大于链表长度if (position < 0 || position > this.length) return false;// 2、创建新节点const newNode = new this.Node(data);// 3、插入节点if (position === 0) {// position = 0 的情况// 让新节点的 next 指向 原来的第一个节点,即 headnewNode.next = this.head;// head 赋值为 newNodethis.head = newNode;} else {// 0 < position <= length 的情况// 初始化一些变量let currentNode = this.head; // 当前节点初始化为 headlet previousNode = null; // head 的 上一节点为 nulllet index = 0; // head 的 index 为 0// 在 0 ~ position 之间遍历,不断地更新 currentNode 和 previousNode// 直到找到要插入的位置while (index++ < position) {previousNode = currentNode;currentNode = currentNode.next;}// 在当前节点和当前节点的上一节点之间插入新节点,即它们的改变指向newNode.next = currentNode;previousNode.next = newNode;}// 更新链表长度this.length++;return newNode;}// getData() 获取指定位置的 datagetData(position) {// 1、position 越界判断if (position < 0 || position >= this.length) return null;// 2、获取指定 position 节点的 datalet currentNode = this.head;let index = 0;while (index++ < position) {currentNode = currentNode.next;}// 3、返回 datareturn currentNode.data;}// indexOf() 返回指定 data 的 index,如果没有,返回 -1。indexOf(data) {let currentNode = this.head;let index = 0;while (currentNode) {if (currentNode.data === data) {return index;}currentNode = currentNode.next;index++;}return -1;}// update() 修改指定位置节点的 dataupdate(position, data) {// 涉及到 position 都要进行越界判断// 1、position 越界判断if (position < 0 || position >= this.length) return false;// 2、痛过循环遍历,找到指定 position 的节点let currentNode = this.head;let index = 0;while (index++ < position) {currentNode = currentNode.next;}// 3、修改节点 datacurrentNode.data = data;return currentNode;}// removeAt() 删除指定位置的节点removeAt(position) {// 1、position 越界判断if (position < 0 || position >= this.length) return null;// 2、删除指定 position 节点let currentNode = this.head;if (position === 0) {// position = 0 的情况this.head = this.head.next;} else {// position > 0 的情况// 通过循环遍历,找到指定 position 的节点,赋值到 currentNodelet previousNode = null;let index = 0;while (index++ < position) {previousNode = currentNode;currentNode = currentNode.next;}// 巧妙之处,让上一节点的 next 指向到当前的节点的 next,相当于删除了当前节点。previousNode.next = currentNode.next;}// 3、更新链表长度 -1this.length--;return currentNode;}// remove() 删除指定 data 的节点remove(data) {this.removeAt(this.indexOf(data));}// isEmpty() 判断链表是否为空isEmpty() {return this.length === 0;}// size() 获取链表的长度size() {return this.length;}// toString() 链表数据以字符串形式返回toString() {let currentNode = this.head;let result = "";// 遍历所有的节点,拼接为字符串,直到节点为 nullwhile (currentNode) {result += currentNode.data + " ";currentNode = currentNode.next;}return result;}}