Home>Article>Web Front-end> Data structure learning: using JavaScript to implement linked list operations (detailed examples)
This article brings you relevant knowledge about how to use JavaScript to implement linked lists in data structure learning. I hope it will be helpful to you.
The linked list has the following characteristics:
The space can be dynamically expanded (in js, the same is true for arrays , but the length of the array in some languages is fixed and cannot be added dynamically, such as c language)
Requires a head node
Requires Know the address of the next node
Each node in the linked list can be regarded as an object. This object has two attributes, one is the value of the node, and one is the address of the next node of the node (if it is a double linked list, add the attribute of the previous node address)
//在尾节点处添加节点 function append(element){ let node = new node(element); let current; if(head == null){ current = node }else{ while(current.next){ current = current.next; } current.next = node } length++; }
Code analysis:
Analysis:
Assign the next attribute of the previous node at this position to this node, save its original next node, and assign it to Now the next attribute of this node
function insert(position,element){ let node = new Node(element); let current = head; let previous;//当前节点的前一个节点,在position处添加节点,就是在previos和current之间添加 if(position = 0){ node.next = head; head = node; }else{ for(let i = 0;i< position;i++){ pervious = current; current = current.next; } pervious.next = node; node.next = current; } length++; return true; }
Code analysis:
#Analysis: The operation of deleting a node is to delete the node before the target node. The pointer points to the node after the target 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; } }
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; }
Analysis: Querying nodes is similar to deleting nodes. They both traverse to find the corresponding node or the corresponding location, and then perform the operation
function searchElement(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; } }
function searchPosition(position){ let i = 0; let current = head; while(i< position){ current = current.next; i++; } return current; }
There are many operations on linked lists, which are a bit more complicated. The linked lists also include double linked lists (adding a previous node when initializing the node) and circular linked lists (the next node of the tail node is the head node). The operations of these linked lists can also be implemented using js, so I won’t go into details here. To sum up, the core of the linked list is that
[Related recommendations:javascript learning tutorial]
The above is the detailed content of Data structure learning: using JavaScript to implement linked list operations (detailed examples). For more information, please follow other related articles on the PHP Chinese website!