Data structure and algorithm JavaScript description--linked list

Source: Internet
Author: User

1. Disadvantages of ArraysAn array does not always organize the best data structure for the information, for the following reasons. In many programming languages, the length of an array is fixed, so it is very difficult to add new elements when the array is filled with data. In arrays, adding and deleting elements is also cumbersome because you need to pan other elements in the array forward or backward to reflect that the array has just been added or deleted. However, there is no such problem with arrays of JavaScript, because using the splice () method does not require any further access to other elements in the array. The main problem with arrays in JavaScript is that they are implemented as objects and are inefficient compared to arrays in other languages (such as C + + and Java) If you find that the array is slow to use in practice, consider using a linked list instead. In addition to random access to data, a linked list can be used in almost any case where a one-dimensional array can be used. If random access is required, the array is still a better choice.2. Define the linked listA 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 successor. A reference to another node is called a chain. Array elements are referenced by their positions, and the linked list elements are referenced by the relationship of each other. Inserting a node into a list is highly efficient. Inserting a node into a linked list requires modifying the node (precursor) in front of it to point to the newly joined node, and the newly joined node points to the node that the predecessor pointed to. It is also easy to delete an element from the list.  The predecessor node of the element to be deleted points to the succeeding node of the element to be deleted, while the element to be deleted points to null, the element is deleted successfully. The list has other operations, but inserting and deleting elements best describes why a linked list is so useful.3. Designing an Object-based linked listThe linked list we have designed contains two classes. The node class is used to represent nodes, and the LinkedList class provides methods for inserting nodes, deleting nodes, displaying list elements, and other helper methods. Node class: LinkedList class: Insert a new node: INSERT, which inserts a node into the linked list. When inserting a new node into a linked list, you need to explicitly indicate which node to insert before or after. How to insert an element after a known node: When you insert an element after a well-known node, you first find the "back" node. To do this, create a helper method, find (), which iterates through the list to find the given data. If the data is found, the method returns the node that holds the data. The Find () method demonstrates how to move on a linked list. First, create a new node and assign 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 the lookup succeeds, the method returns the node that contains the data, otherwise, NULL is returned. Remove a node from a linked list: When you delete a node from a linked list, you need to first find the node that precedes the node you want to delete. After locating this node, modify its next property so that it no longer points to the node to be deleted, but instead points to the next node of the node to be deleted. Code implementation:
<script type= "Text/javascript" >/** * Node class: * element is used to hold the data on the node * next to save a link to the next node*/functionNode (Element) { This. Element =element;  This. Next =NULL;}/** * The LinkedList class * Llist class provides a way to manipulate a linked list. * Includes inserting a delete node, finding the given value in the list. * The list has only one attribute, which is to use a node object to hold the head node of the linked list. * * The next property of the head node is initialized to null* when a new element is inserted, next points to the new element, so here we do not modify the value of next. */functionllist () { This. Head =NewNode ("Head");  This. Find =find;  This. Insert =Insert;  This. remove =remove;  This. display =display;  This. findprevious =findprevious;}/** * Traverse the linked list to find the given data. * If data is found, the method returns the node that holds the data*/functionFind (item) {varCurrnode = This. Head;  while(Currnode.element! =Item) {Currnode=Currnode.next; }    returnCurrnode;}/** Insert new node * Insert Newelement after item element (known node)*/functionInsert (newelement, item) {varNewNode =NewNode (newelement); varCurrent = This. Find (item); Newnode.next=Current.next; Current.next=NewNode;}/** * Find the previous node of the element to be deleted * This method iterates through the elements in the list and checks to see if the data to be deleted is stored in the next node of each node. * If found, returns the node (that is, the "previous" node)*/functionfindprevious (item) {varCurrnode = This. Head;  while(currnode.next!=NULL&& currNode.next.element! =Item) {Currnode=Currnode.next; }    returnCurrnode;}/** * Deleting a node from a linked list * requires first locating the node in front of the node to be deleted. * After locating this node, modify its next property so that it no longer points to the node to be deleted, but instead points to the next node of the node to be deleted. */functionRemove (item) {varPrevNode = This. findprevious (item); if(Prevnode.next! =NULL) {Prevnode.next=PrevNode.next.next; }}/** Show elements in a linked list*/functiondisplay () {varCurrnode = This. Head;  while(Currnode.next! =NULL) {console.log (currNode.next.element); Currnode=Currnode.next; }}//Test CodevarCities =Newllist (); Cities.insert ("Conway", "head"); Cities.insert ("Russellville", "Conway"); Cities.insert ("Carlisle", "Russellville"); Cities.insert ("Alma", "Carlisle"); Cities.display (); Cities.remove ("Carlisle"); Cities.display ();</script>

Print:

Data structure and algorithm JavaScript description--linked list

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.