链表是一种由一系列元素组成的数据结构,每个元素都包含对该序列中下一个元素的引用(或“链接”)。第一个元素称为头,最后一个元素称为尾。
链表与其他数据结构相比有很多优点。现在我们来看看如何使用 javascript 实现链表。
定义 node 类和 linkedlist 类这基本上是在 javascript 中实现链表的先决条件。在这一步中,需要创建2个类,一个用于节点,另一个用于链表。
node 类表示链表中的单个节点。它有两个属性:data 和 next。 data 属性用于存储节点的实际数据,而 next 属性是对列表中下一个节点的引用。 node 类由一个构造函数组成,该构造函数在创建新 node 时初始化数据和 next 属性。
class node { constructor(data) { this.data = data; this.next = null; }}
linkedlist 类是链表本身的表示。它有一个 head 属性,引用列表中的第一个节点。 linkedlist 类还有一个构造函数,用于在创建新的 linkedlist 时初始化 head 属性。
class linkedlist { constructor() { this.head = null; this.tail = null; this.length = 0; }}
linkedlist 类还包含一个方法,允许您在列表中插入、删除和搜索节点,同时允许其他操作,如打印列表、计算元素、反转列表等。打印链接列表通过遍历链表并打印每个节点的数据,可以打印链表的元素。
printall() { let current = this.head; while (current) { console.log(current.data); current = current.next; }}
向链表添加节点有多种方法可以将数据添加到链表,具体取决于新节点必须插入的位置,如下 -
将节点添加到链表的开头要在链表的开头添加节点/元素,一旦使用数据创建了新节点,只需将其 next 属性设置为链表的当前头即可。然后您可以将链表的头更新为新节点。这也称为链表头部插入,是最基本的数据添加类型。只需调用下面定义的 add 函数即可完成。
add(data) { const newnode = new node(data); if (!this.head) { this.head = newnode; this.tail = newnode; } else { this.tail.next = newnode; this.tail = newnode; } this.length++; return this;}
将节点添加到链表末尾要在链表末尾添加节点/元素,我们需要遍历链表并找到最后一个节点。之后创建一个带有数据的新节点,并将最后一个节点的 next 属性设置为新节点。这也称为链表尾部插入,是第二种最基本的数据添加类型。只需调用下面定义的 addtotail 函数即可完成。
addtotail(data) { let newnode = new node(data); if (this.head === null) { this.head = newnode; return; } let current = this.head; while (current.next !== null) { current = current.next; } current.next = newnode;}
在特定位置添加节点要在链表中的特定位置添加节点/元素,可以遍历链表找到插入点之前位置的节点,用数据创建一个新节点,设置新节点的next属性到该位置的当前节点,并将前一个节点的next属性设置为新节点。
addatposition(data, position) { let newnode = new node(data); if (position === 1) { newnode.next = this.head; this.head = newnode; return; } let current = this.head; let i = 1; while (i < position - 1 && current) { current = current.next; i++; } if (current) { newnode.next = current.next; current.next = newnode; }}
示例(向链表添加节点)在下面的示例中,我们实现了在开头、结尾和特定位置添加节点。
class node { constructor(data) { this.data = data; this.next = null; }}class linkedlist { constructor() { this.head = null; this.tail = null; this.length = 0; } // function to add data to linked list add(data) { const newnode = new node(data); if (!this.head) { this.head = newnode; this.tail = newnode; } else { this.tail.next = newnode; this.tail = newnode; } this.length++; return this; } //function to add data to tail addtotail(data) { let newnode = new node(data); if (this.head === null) { this.head = newnode; return; } let current = this.head; while (current.next !== null) { current = current.next; } current.next = newnode; } // function to insert data to linked list at a particular index addatposition(data, position) { let newnode = new node(data); if (position === 1) { newnode.next = this.head; this.head = newnode; return; } let current = this.head; let i = 1; while (i < position - 1 && current) { current = current.next; i++; } if (current) { newnode.next = current.next; current.next = newnode; } } // this function is used to iterate over the entire linkedlist and print it printall() { let current = this.head; while (current) { console.log(current.data); current = current.next; } }}const list = new linkedlist();// add elements to the linkedlistlist.add(node1);list.add(node2);list.add(node3);list.add(node4);console.log(initial list:);list.printall();console.log(list after adding nodex at position 2);list.addatposition(nodex,2);list.printall();console.log(list after adding nodey to tail);list.addtotail(nodey);list.printall();
输出initial list:node1node2node3node4list after adding nodex at position 2node1nodexnode2node3node4list after adding nodey to tailnode1nodexnode2node3node4nodey
删除节点也可以根据要求通过多种方法删除数据。
删除特定节点要从链表中删除特定节点,我们需要遍历链表并找到要删除的节点之前的节点,更新其 next 属性以跳过要删除的节点,并更新对的引用下一个节点。这将根据值删除节点。
remove(data) { if (!this.head) { return null; } if (this.head.data === data) { this.head = this.head.next; this.length--; return this; } let current = this.head; while (current.next) { if (current.next.data === data) { current.next = current.next.next; this.length--; return this; } current = current.next; } return null;}
删除特定位置的节点要删除链表中特定位置的节点,我们需要遍历链表并找到要删除的节点之前的节点,更新其 next 属性以跳过要删除的节点,然后更新对下一个节点的引用。这基本上是根据节点的索引值删除节点。
removeat(index) { if (index < 0 || index >= this.length) return null; if (index === 0) return this.remove(); let current = this.head; for (let i = 0; i < index - 1; i++) { current = current.next; } current.next = current.next.next; this.length--; return this;}
示例(从线性列表中删除节点)在下面的示例中,我们实现了删除特定节点和特定位置的节点。
class node { constructor(data) { this.data = data; this.next = null; }}class linkedlist { constructor() { this.head = null; this.tail = null; this.length = 0; } // function to add data to linked list add(data) { const newnode = new node(data); if (!this.head) { this.head = newnode; this.tail = newnode; } else { this.tail.next = newnode; this.tail = newnode; } this.length++; return this; } // function to remove data from linked list remove(data) { if (!this.head) { return null; } if (this.head.data === data) { this.head = this.head.next; this.length--; return this; } let current = this.head; while (current.next) { if (current.next.data === data) { current.next = current.next.next; this.length--; return this; } current = current.next; } return null; } // function to remove from a particular index removeat(index) { if (index < 0 || index >= this.length) return null; if (index === 0) return this.remove(); let current = this.head; for (let i = 0; i < index - 1; i++) { current = current.next; } current.next = current.next.next; this.length--; return this; } // this function is used to iterate over the entire linkedlist and print it printall() { let current = this.head; while (current) { console.log(current.data); current = current.next; } }}const list = new linkedlist();// add elements to the linkedlistlist.add(node1);list.add(node2);list.add(node3);list.add(node4);console.log(initial list:);list.printall();console.log(list after removing node2);list.remove(node2);list.printall();console.log(list after removing node at index 2);list.removeat(2);list.printall();
输出initial list:node1node2node3node4list after removing node2node1node3node4list after removing node at index 2node1node3
结论在 javascript 中实现链表涉及创建一个 node 类来表示列表中的每个节点和一个 linkedlist 类来表示列表本身,并向 linkedlist 类添加方法来执行添加和删除数据以及打印等操作列表。重要的是还要考虑边缘情况并在实施中相应地处理它们。根据用例,有多种方法可以在 linkedlist 中添加或删除数据。
以上就是javascript 中 linkedlist 的实现的详细内容。