Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use JavaScript to realize the operation of linked list

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "how to use JavaScript to operate linked lists". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The linked list has the following characteristics:

You can expand space dynamically (in js, arrays are the same, but in some languages the length of arrays is fixed and cannot be added dynamically, such as c)

A header node is required

Need to know the address of the next node

   can treat each node in the linked list as an object, which has two attributes, one is the value of the node and the other is the address of the next node of the node (if it is a double linked list, add the attribute of the address of the previous node)

Implement the operation of adding nodes:

1 add node at trailing node / / add node function append (element) {let node = new node (element) at trailing node; let current; if (head = = null) {current = node} else {while (current.next) {current = current.next } current.next = node} length++;}

Code analysis:

Defines a node based on the passed-in element, which is the value of this node

Define a variable to represent the current node

Determine whether there is a header node, if there is no header node, it means that there is no value in the linked list, and use the value passed in as the header node; otherwise, traverse the linked list, find the last node, and assign its next attribute to the new node

Length of linked list + 1

two。 Add nodes anywhere

Analysis:

   assigns the next attribute of the previous node in this location to this node, saves its previous next node, and assigns it to the next attribute of the current node.

Function insert (position,element) {let node = new Node (element); let current = head; let previous;// the previous node of the current node. To add a node at position is to add if (position = 0) {node.next = head; head = node;} else {for (let I = 0th I < position) between previos and current. Pervious +) {pervious = current; current = current.next;} pervious.next = node; node.next = current;} length++; return true;}

Code analysis:

Check whether the postion is out of bounds, and if not, create a node

Define a variable to represent the current node, initialized to the header node, indicating that the traversal starts from the header node; a variable represents the previous node of the current node, and its function is to find the previous node easily when inserting the node

Determine whether to add it in front of the header node, if so, assign the header node to the next attribute of node, and change the header node to this node; otherwise, traverse the node in this position and insert the node in front of the node in this position

Length of linked list + 1

Implement the operation of deleting nodes

Analysis: the operation of deleting a node is to point the pointer of the node in front of the target node to the next node of the target node

1. Delete the specified node function removed (element) {let node = new Node (element); let pervious; let nextNode; let current = head; if (head! = null) {while (current! = node) {pervious = current; current = current.next; nextNode = current.next;} pervious.next = nextNode; length--; return true } else {return false;}} 2. Delete the node function removedAt (position) {let current = head; let pervious; let nextNode; let i = 0; while (I < position) {pervious = current; current = current.next; nextNode = current.next;} pervious.next = nextNode; length--; return true;} to query the node

Analysis: querying a node is similar to deleting a node, both by traversing, finding the corresponding node or corresponding location, and then performing the operation

1. Query which node function searchElement (element) {/ / input element a location is. Find the element and return the location of the element if (head! = null) {let node = new Node (element); let current; let index = 0; if (head = = node) {return 0;} else {current = head While (current! = node) {current = current.next; index++;} return index;}} else {return-1;}} 2. Query where a node is located function searchPosition (position) {let I = 0; let current = head; while (I < position) {current = current.next; ilist operations;} return current;} "how to use JavaScript to implement linked list operations" is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report