Data Structures and Algorithms in JavaScript (III): linked list _ javascript skills

Source: Internet
Author: User
This article mainly introduces the data structure and algorithm in JavaScript (3): linked list. This article describes the single-chain table and double-chain table, as well as code instances for adding and deleting sections, for more information about the queue and stack in javascript, we can see that the queue and stack 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, data = (information + address)

In the chain structure, the address can also be referred to as a "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.

Array elements use positional relationships for logical reference, while linked lists use each data element to store reference pointer relationships for reference.

This structural advantage is very obvious. inserting a data does not need to concern about its arrangement, as long as the "chain" is directed to the link.

In this way, the linked list will not be limited to Arrays. we can use objects as long as there is a reference relationship between objects.

Linked lists generally include single-chain tables, static linked lists, cyclic linked lists, and two-way linked lists.

Single-chain table: it is a very single downward transmission. every node only records the information of the next node. like Liang Chaowei in the same channel, it is used to launch and deprecate connections through man-in-the-middle. Once the man-in-the-middle is disconnected, so I cannot prove my identity, so there is a sentence at the end of the title: "I am a good person, 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

Circular linked list: because a single-chain table is transmitted only to the rear, it is very troublesome to trace back to the first end when it reaches the end. Therefore, link the chain of the tail node with the header to form a loop.

Two-Way linked list: Optimized for single-chain tables, so that every section can know who is before and after, so there will be a pre-pointer field in addition to the post-pointer field, which improves the search efficiency, however, it brings some design complexity. In general, it is time for space to change.

In summary, the linked list is actually an optimization method for the sequential storage structure in a linear table, but in javascript, because of the special nature of the array (automatic update of the reference location ), therefore, we can use the object method to create a chain table storage structure.

Single-chain table

We implement a simple linked list relationship.

Function createLinkList () {var _ this = {}, prev = null; return {add: function (val) {// save the current reference prev = {data: val, next: prev | null }}} var linksList = createLinkList (); linksList. add ("arron1"); linksList. add ("arron2"); linksList. add ("arron3"); // The next link of the node section is-arron3-arron2-arron1

Using the next of the node object to directly reference the next node object is initially implemented by referencing through the linked list. This chained idea is the then method in jQuery's asynchronous deferred, and jsderferre of cho45 in 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, find the corresponding data, insert the new section into the current chain, and rewrite the location record.

// Find the corresponding section var findNode = function createFindNode (currNode) {return function (key) {// find the executed section in a loop in the linked list. If no while (currNode. data! = Key) {currNode = currNode. next;} return currNode ;}} (headNode );

This is a method for finding the current section. you can pass the original headNode section in the header to continue searching for next until the corresponding section information is found.

This is implemented using the curry method.


This is the conversion relationship between linked list addresses when inserting segments.

In the linked list of a-B-c-d, if you want to insert an f

A-B-c-f-d, then c, next-> f, f. next-d

Add a section using the insert method

// Create the 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 "); // find the corresponding section var findNode = function createFindNode (currNode) {return function (key) {// find the executed section in a loop in the linked list. If no while (currNode. data! = 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 creation of the createNode section is separated. during initialization, a header section object is created to initialize the object at the beginning of the section.

In the insert add section method, find the corresponding section by searching the headNode chain, add the new section, and modify the link.

How do I delete a node from a 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 the next address of its linked list. this is a bit like removeChild in the dom operation. find its parent node and call to remove the child node.

Similarly, in the design of the remove method, we need to design a traversal to backtrack a parent node.

// 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 chain table relation prevNode. next = prevNode. next. next ;}};

Test code:

Insert multiple data entriesDelete Russellville data


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.