The linked list structure in JavaScript

Source: Internet
Author: User

1. Definition

In many programming languages, the length of an array is fixed, that is, when you define an array, you need to define the length of the array, so it is difficult to add new elements when the array is already filled with data. It can only be said that this is the case in some languages, and the length of the array in JavaScript and PHP is arbitrarily increased. Adding and removing elements in an array is also cumbersome because it is not a problem in JavaScript to translate other elements in the array forward or backward, and there is a handy way to add or remove elements in JavaScript splice ().

But everything is relative, and the arrays in JavaScript have their own problems, and they are objects that are inefficient compared to other languages such as C + + and Java.

If the efficiency of an array is slow to find in the actual use, you can consider using a linked list instead. An array has the advantage of being able to access the values of the array conveniently based on the key value, and in addition, the list can be substituted for the array in any situation. If you need to randomly access elements, the array is still a better choice.

A linked list is a collection of nodes that are made up of a group. Each node uses a reference to an object to point to its subsequent borrowing points. A reference to another borrowing point is called a chain.

Array elements are referenced by their positions, and the linked list elements are referenced by the relationship of each other. In the array it is said that this element is the first element in the array, but in the list it is said that this element is the element behind an element. Traversing a linked list is followed by a list of links from the head of the linked list (head) to the tail element (but does not contain a list of the head lending point, the head is usually used as the access point of the list). Another problem is that the tail element of the list points to a null node. such as 1

Figure 1

Many of the list's implementations have a special node in front of the list, called the head node. The last node points to null, and all the last plus a null node. such as 2

Figure 2

Inserting a node into a linked list is highly efficient. Inserting a node into a linked list requires modifying the node in front of it to point to the newly joined node, and the newly joined node points to the node that is pointed to earlier. As shown in the eggs, add a cookie node behind it. such as 3.

Figure 3

Deleting a node from a linked list is also straightforward, pointing the precursor node of the element to be deleted to the subsequent node to be deleted, and releasing the element to be deleted to null. It's a coincidence. Deletes an element that precedes the null element. such as 4

Figure 4

2. Code implementation

There are two classes in the following linked list implementations. The node class is used to identify nodes, and the LinkedList class provides a way to insert nodes, delete nodes, display linked list node elements, and some other helper methods.

The node class contains two attributes, element is used to hold the data on the node, and next holds the link to the next node. We use a constructor to create the node, and the constructor sets the values of the two properties. As follows:

function Node (Element) {    this. Element = element;       This NULL ;}

The LinkedList class provides a way to manipulate a linked list, which includes an insert node that finds a given node in a linked list. The class also has a constructor, and the list has only one property, which is to use a node object to save the list's head node, the code is as follows:

function llist () {    thisnew Node (' head ');     this. Find = find;     this. Insert = insert;     // this.remove = remove;    this. display = display;}

The next property of the head node is initially Huawei Null, and when a new element is inserted, next points to the new element.

The method of inserting a node is insert, and when the method inserts a new node into the list, it needs to be explicitly inserted before or after that node. This first discusses inserting elements behind a known node. When inserting elements behind an element, you need to find the "back" node first. To do this, create a helper method, find (), which iterates through the list, finds the specified data, and returns the node that holds the data if it finds the data, the code for the Find () method is as follows:

function Find (item) {    varthis. Head;      while (Currnode.element! = Item)        {= currnode.next    ;    } return Currnode;}

The Find () method demonstrates how to move on a linked list. Start by creating a new node and assigning the head node of the linked list to the newly created node. Then loop through the list, moving from the current node to the next node if the element property of the current node does not match the information we are looking for. If a lookup succeeds, the method returns the node that contains the data, otherwise returns NULL.

Once the "back" node is found, the new node can be inserted into the linked list. First, set the next property of the new node to the value corresponding to the following node, and then set the next property of the following node to point to the new node, and insert () is defined as follows:

// Insert an element function Insert (newelement, item) {    varnew  Node (newelement);     var  This . Find (item);     = Current.next;     = NewNode;}

Finally, we define a way to print a list of elements, as follows:

function display () {    varthis. Head;      while NULL ) ) {        + '   ' );         = currnode.next;    }}

This method first assigns the head of the list to a variable, and then loops through the list, and the current node's next property is null when the loop ends. To display only the nodes that contain the data, we use the currNode.next.element expression to access the data in the node.

Finally, we use the following code to test the linked list. Save a few American cities in the list "Conway", "Russellville", "Alma" and print them out, the complete code is as follows:

functionNode (Element) { This. Element =element;  This. Next =NULL;}functionllist () { This. Head =NewNode (' head ');  This. Find =find;  This. Insert =Insert; //this.remove = remove;     This. display =display;}functionFind (item) {varCurrnode = This. Head;  while(Currnode.element! =Item) {Currnode=Currnode.next; }    returnCurrnode;}//Insert an elementfunctionInsert (newelement, item) {varNewNode =NewNode (newelement); varCurrent = This. Find (item); Newnode.next=Current.next; Current.next=NewNode;}functiondisplay () {varCurrnode = This. Head;  while(! (Currnode.next = =NULL) {document.write (currNode.next.element+ '   '); Currnode=Currnode.next; }}//Test ProgramvarCities =Newllist (); Cities.insert ("Conway", "head"); Cities.insert ("Russellville", "Conway"); Cities.insert ("Alma", "Russellville"); Cities.display ();

The result of the final output is as follows:

The linked list structure in JavaScript

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.