Data Structure and algorithm JavaScript (3) linked list

Source: Internet
Author: User
Tags javascript array

Data Structure and algorithm JavaScript (3) linked list
We can see that the queue and stack in the javascript concept are both a special linear table structure and a simple array-based Sequential storage structure. Because the javascript interpreter directly optimizes arrays, there is no fixed length of arrays in many programming languages (it is difficult to add an array after it is filled up, to add and delete all elements in the array, you must change their positions. The javascript array is indeed optimized directly, such as the push, pop, shift, unshift, and split methods ...) The biggest drawback of the sequential storage structure of a linear table is that changing the arrangement of one of the elements will lead to changes in the entire collection. The reason is that the storage in the memory is consistent and there is no gap, to delete a file, you must add it. After the optimization of this structure, the chain storage structure emerged. In another way, we don't care about data arrangement at all, we only need to place the next data location within each element to record, so the linear table stored in the Link Method is short for the linked list. In the chain structure, in the chain structure of data = (Information + address), the address can also be referred to as "chain". A data unit is a node, so the linked list is a collection of nodes. Each node has a data block reference pointing to its next node. The image array element is a logical reference based on the positional relationship, the chain table uses the reference pointer relationship stored in each data element for reference. This structure has obvious advantages. inserting a data does not need to worry about its arrangement, as long as we point the "chain" to the link, the idea of doing this is not limited to the array, we can use the object, as long as there is a reference relationship between objects, the Linked List generally has, single-chain table, static linked list, cyclic linked list, and two-way linked list single-chain table: each node records only the information of the next node, just like Liang chaowei, who is in the middle of the road, is working as an undercover engineer. He goes online and goes offline through a man-in-the-middle. Once the man-in-the-middle is disconnected, he cannot prove his identity. Therefore, there is a sentence at the end of the film, who knows?" Static linked list: A linked list described in arrays. That is, each table in the array is a "section" that contains data and points to a circular linked list: because a single-linked list is only transmitted to the rear, when it reaches the end, it is very troublesome to trace back to the first part. Therefore, it is difficult to connect the chain of the tail section with the header to form a circular two-way linked list: To optimize the single-chain table, so that each section can know who is before and after, therefore, in addition to the post pointer field, there will be a pre-pointer field, which improves the search efficiency, but brings some design complexity. In general, it is a combination of space and time, in fact, linked lists are an optimization method for the sequential Storage Structure in linear tables. However, in javascript, because of the special nature of arrays (the reference location is automatically updated ), so we can use the object method to store the linked list structure of a single-chain table. We implement the simplest Chain List link copy code 1 function createLinkList () {2 var _ this = {}, 3 prev = null; 4 return {5 add: function (val) {6 // Save the current reference 7 prev = {8 data: val, 9 next: prev | null10} 11} 12} 13} 14 var linksList = createLinkList (); 15 linksList. add ("arron1"); 16 linksList. add ("arron2"); 17 linksList. add ("arron3"); // the next link of the node section is-The arron3-arron2-arron1 replication Code uses the next of the node object to directly reference the next node object, the then method in asynchronous deferred of jQuery is also useful in cho45 jsderferre of Japan. There is another key issue in this implementation. How can we dynamically insert data after the execution section? Therefore, we must design a Traversal method to search for the data on the node chain, and then find the corresponding data to insert the new section into the current chain, and rewrite the location record // find the corresponding section in the linked list var findNode = function createFindNode (currNode) {return function (key) {// find the Execution Section in a loop, if no while (currNode. data! = Key) {currNode = currNode. next;} return currNode;} (headNode); this is a method for finding the current section. You can continue searching for next by passing the original header headNode section, until the corresponding section information is found, which is implemented using the curry method, when the section is inserted, the conversion relationship for the linked list address is like this. This is the linked list of a-B-c-d, if you want. next-> d) insert an f a-B-c-f-d, then c, next-> f, f. next-d Add the section // create the Section function createNode (data) {this. data = data; this. next = null;} // initialize the header node // form a chain starting from headNode // connect var headNode = new createNode ("head") through next; // locate in the linked list Go to the corresponding section var findNode = function createFindNode (currNode) {return function (key) {// find the Execution Section in a loop. If no while (currNode. data!) is returned! = Key) {currNode = currNode. next;} return currNode;} (headNode); // Insert a new section this. insert = function (data, key) {// create a new section var newNode = new createNode (data ); // locate the corresponding data node in the chain // then mount the newly added node to var current = findNode (key); // Insert a new connection, change the reference link // 1: a-B-c-d // 2: a-B-n-c-d newNode. next = current. next; current. next = newNode;}; first, the createNode section construction is separated. during initialization, a header section object is created to initialize the object starting with the Section. In the insert add section method, find the corresponding section by searching the headNode chain and The last step is to modify the link. How can I delete a node from the linked list? Due to the special nature of the linked list, if we want to delete a-> B-> c-> d, we must modify B. next-> c is B. next-> d, so find the previous section and modify its linked list next address, this is a bit like how removeChild finds its parent node in the dom operation and calls to remove the child node. We are also designing the remove Method, you need to design a traversal to backtrack a parent node. // you can find the previous section var findPrevious = function (currNode) {return function (key) {while (! (CurrNode. next = null) & (currNode. next. data! = Key) {currNode = currNode. next;} return currNode;} (headNode); // insert method this. remove = function (key) {var prevNode = findPrevious (key); if (! (PrevNode. next = null) {// modify the linked list relation prevNode. next = prevNode. next. next ;}}; the principle of double-chain table is the same as that of single-chain table. It is nothing more than adding a section to the image pointer of the previous chain table for each section. // Insert a new section this. insert = function (data, key) {// create a new section var newNode = new createNode (data ); // locate the corresponding data node in the chain // then mount the newly added node to var current = findNode (headNode, key); // Insert a new connection, change the reference relationship newNode. next = current. next; newNode. previous = current. next = newNode;}; Delete the section this. remove = function (k Ey) {var currNode = findNode (headNode, key); if (! (CurrNode. next = null) {currNode. previous. next = currNode. next; currNode. next. previous = currNode. previous; currNode. next = null; currNode. previous = null ;}}; there is an obvious optimization in the delete operation: you do not need to find the parent node, because the two-way reference of the double-stranded table is more efficient than the single-linked table.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.