Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize linked list structure by js

Shulou Source: shulou.com Published: 2022-06-03 08:28:42 09月18日 Update

This article mainly introduces how to achieve the linked list structure of js, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

1. You can build a Node class to describe the nodes in the linked list. This class has two attributes, one to hold the value of the node and the other to hold the pointer to the next node.

Let Node = function (element) {this.element = element; this.next = null;}

2. The basic skeleton of a linked list is actually a linked list class and related operation functions.

Class LinkedList {constructor () {this.length = 0; this.head = null } / / find the index indexOf (element) of a given node in the linked list {} / return the node corresponding to the index in the linked list find (position) {} / / add the node append (element) {} / / insert the node insert (position, element) {} / / delete the node at the specified location in the linked list And return the value of this node removeAt (position) {} / delete the corresponding node remove (element) {} / / determine whether the linked list is empty isEmpty () {} / / return the length of the linked list size () {} / / return the header node of the linked list getHead () {} / / clear the linked list clear () {} / / helper method Traverse the entire linked list, output all the nodes in the linked list according to the specified format, and facilitate the test verification results toString () {}} Thank you for reading this article carefully. I hope the article "how to achieve the linked list structure of js" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Tags: Nodes articles structure location index two middle finger value interest function and actually properties points pointers methods more friends format Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Shulou Technology macOS Huawei OPPO Reno