A linked list is a common data structure. It is a structure for dynamic storage allocation. This article mainly introduces the implementation of the linked list in the JavaScript data structure, which has good reference value. Next, let's take a look at the chain table as a common data structure. It is a structure for dynamic storage allocation. This article mainly introduces the implementation of the linked list in the JavaScript data structure, which has good reference value. Let's take a look at it with the small Editor.
The previous author discussed the implementation of Data Structure stacks and queues. The data structures used at that time were implemented using arrays, but arrays are sometimes not the best data structures, for example, when adding and deleting elements in an array, you need to move other elements, but you do not need to access other elements by using the spit () method in javascript. If you find it slow when using arrays, you can consider using linked lists.
Concept of linked list
A linked list is a common data structure. It is a structure for dynamic storage allocation. The linked list has a "header Pointer" variable, which is represented by a head. It stores an address and points to an element. Each node uses the reference indicator of an object. The reference pointing to another node is called a chain.
Array elements rely on subscript (position) for reference, while linked list elements rely on the relationship between each other for reference. As a result, the insertion efficiency of the linked list is very high, demonstrating the insertion process of the linked list node d:
Deletion process:
Object-based linked list
We define two classes: Node and worker list. Node is the Node data, and worker list is the method for saving the operation linked list.
First, let's look at the Node class:
function Node(element){ this.element = element; this.next = null; }
Element is used to save the data on the node, and next is used to save the link pointing to the node.
Sort list class:
function LinkedList(){ this.head = new Node('head'); this.find = find; this.insert = insert; this.remove = remove; this.show = show;}
The find () method starts from the first node and continues searching along the linked list node until an element with the same content as the item is found, the node is returned. If no content is found, an empty value is returned.
Function find (item) {var currentNode = this. head; // start from the first node while (currentNode. element! = Item) {currentNode = currentNode. next;} return currentNode; // locate the returned node data. If no return null}
Insert method. We can see from the demonstration of element insertion above that the following four steps can be taken to insert data:
1. Create a node
2. Find the target node
3. Modify the next link of the target node
4. Assign the next value of the target node to the next value of the node to be inserted.
function insert(newElement,item){ var newNode = new Node(newElement); var currentNode = this.find(item); newNode.next = currentNode.next; currentNode.next = newNode; }
Remove () method. To delete a node, you must first find the front node of the deleted node. For this reason, we define the frontNode () method ():
function frontNode(item){ var currentNode = this.head; while(currentNode.next.element!=item&¤tNode.next!=null){ currentNode = currentNode.next; } return currentNode;}
Step 3:
1. Create a node
2. Locate the front node of the target node
3. Change the next of the previous node to the next node of the deleted node.
function remove(item){ var frontNode = this.frontNode(item); //console.log(frontNode.element); frontNode.next = frontNode.next.next; }
Show () method:
Function show () {var currentNode = this. head, result; while (currentNode. next! = Null) {result + = currentNode. next. element; // to not display the head node currentNode = currentNode. next ;}}
Test procedure:
var list = new LinkedList();list.insert("a","head");list.insert("b","a");list.insert("c","b");console.log(list.show());list.remove("b");console.log(list.show());
Output:
Two-way linked list
It is easy to traverse from the head node of the linked list to the end node, but sometimes we need to go forward from the back. In this case, you can add an attribute to the Node object to store the link pointing to the front-end Node. The working principle of the two-way linked list used by the landlord.
First, add the front attribute to the Node class:
function Node(element){ this.element = element; this.next = null; this.front = null; }
Of course, we also need to modify the corresponding insert () and remove () methods:
Function insert (newElement, item) {var newNode = new Node (newElement); var currentNode = this. find (item); newNode. next = currentNode. next; newNode. front = currentNode; // Add front pointing to the front node currentNode. next = newNode;} function remove (item) {var currentNode = this. find (item); // locate the node to be deleted if (currentNode. next! = Null) {currentNode. front. next = currentNode. next; // point the front-end node to the next node of the node to be deleted currentNode. next. front = currentNode. front; // point the successor node to the current node of the node to be deleted. next = null; // and set the frontend and successor to null currentNode. front = null ;}}
Display the linked list in reverse order:
You need to add a method to the two-way linked list to find the last node. The findLast () method is used to locate the last node in the linked list, which can avoid the previous and later successive links.
Function findLast () {// find the last node var currentNode = this. head; while (currentNode. next! = Null) {currentNode = currentNode. next;} return currentNode ;}
Implement reverse output:
function showReverse() { var currentNode = this.head, result = ""; currentNode = this.findLast(); while(currentNode.front!=null){ result += currentNode.element + " "; currentNode = currentNode.front; } return result;}
Test procedure:
var list = new LinkedList();list.insert("a","head");list.insert("b","a");list.insert("c","b");console.log(list);list.remove("b");console.log(list.show());console.log(list.showReverse());
Output:
Cyclic linked list
Cyclic linked list is another form of chained storage structure. It points the pointer field of the last node in the table to the header node, and the entire linked list forms a ring. The cyclic linked list is similar to the unidirectional linked list, and the node types are the same. The only difference is that when creating a circular linked list, let the next attribute of its header node point to itself, that is:
head.next = head
This behavior is transmitted to each node in the linked list so that the next attribute of each node points to the head node of the linked list. The poster is used to indicate a circular linked list:
Modify the constructor:
Function compute list () {this. head = new Node ('head'); // initialize this. head. next = this. head; // direct the next of the header node to form a circular linked list this. find = find; this. frontNode = frontNode; this. insert = insert; this. remove = remove; this. show = show ;}
Note that the output methods of the linked list are show () and find (). The original method will be stuck in an endless loop in the linked list, the loop condition of the while loop needs to be modified to exit the loop when the loop reaches the first node.
Function find (item) {var currentNode = this. head; // start from the first node while (currentNode. element! = Item & currentNode. next. element! = 'Head') {currentNode = currentNode. next;} return currentNode; // locate the returned node data, and return null} function show () {var currentNode = this. head, result = ""; while (currentNode. next! = Null & currentNode. next. element! = "Head") {result + = currentNode. next. element + ""; currentNode = currentNode. next;} return result ;}
Test procedure:
var list = new LinkedList();list.insert("a","head");list.insert("b","a");list.insert("c","b");console.log(list.show());list.remove("b");console.log(list.show());
Test results:
The above is a detailed description of the implementation method (text) of the linked list of JavaScript data structures. For more information, see other related articles in the first PHP community!