Realization of--javascript of data structure linked list

Source: Internet
Author: User

The concept of a linked list

A linked list is a common data structure. It is a structure for dynamic storage allocation. The list has a "head pointer" variable, which, in head, holds an address that points to an element. Each node uses an object's reference indicator to its successor, and a reference to another node is called a chain.

  

Array elements depend on the subscript (position) for reference, and the linked list elements are referenced by their relationship to each other. Therefore, the insertion efficiency of the linked list is very high, demonstrating the insertion process of the linked table node D:

Delete procedure:

Object-based linked list

We define 2 classes, the node class and the LinkedList class, node as the node data, and LinkedList the way to save the list of operations.

First look at node class:

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

The element is used to hold the data on the node, and next is the link that points to the node.

LinkedList class:

  

function LinkedList () {         thisnew Node (' head ');          this. Find = find;          this. Insert = insert;          this. remove = remove;          this. Show = Show;          this. Frontnode = frontnode;//modified Place}

The Find () method, starting at the beginning of the node, finds it along a linked list node until it finds an element equal to the item content, returns the node, and returns null if it is not found.

function Find (item) {         varthis. Head;   start from the node while         (currentnode.element!=Item) {                  =   Currentnode.next;         }          return CurrentNode; // found return node data, not found return null}

Insert method. By inserting the previous element into the demo you can see that the implementation of the insert is simple four steps:

1. Create a Node

2. Find the target node

3, modify the target node of the next point link

4. Assign the next value of the target node to the next node to be inserted

function Insert (newelement,item) {      varnew  Node (newelement);       var  This . Find (item);       = Currentnode.next;       = NewNode;}

Remove () method. To delete a node you need to first find the front node of the deleted nodes, for which we define the method Frontnode ():

function Frontnode (item) {    varthis. Head;      while (currentnode.next.element!=item&&currentnode.next!=null) {                  =  Currentnode.next;        }              return CurrentNode;}

Jane answers three steps:

1. Create a Node

2. Find the front node of the target node

3, modify the previous node next point to the deleted node n a node

function Remove (item) {          varthis. Frontnode (item);           // Console.log (frontnode.element);          Frontnode.next = frontNode.next.next;}

Show () Method:

function Show () {         varthis. Head         var result = ';   Modify while         (currentnode.next!=null) {                  + = CurrentNode.next.element; // in order not to show the head node.                  CurrentNode = currentnode.next;         }          return result; // modified }

Test procedure:

var New LinkedList (); List.insert ("A", "Head"), List.insert ("B", "a"), List.insert ("C", "B"); Console.log (List.show ()); List.remove ("B"); Console.log (List.show ()) ;

Output:

Doubly linked list

It is simple to traverse from the head node of the list to the tail node, but sometimes we need to move forward from the back. At this point we can add a property to the node object that stores the link to the precursor node. Landlord for two-way linked list working principle.

First, let's add the front attribute to the node class first:

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

LinkedList class:

 function   LinkedList () { this . Head = new  Node (' head '          this . Find = find;          this . Insert = insert;          this . Remove = remove;          this . Show = show;          this . Frontnode = Frontnode;          this . Showreverse = Showreverse;  this . FindLast = FindLast;}  

Of course, the corresponding insert () method and the Remove () method also need to be modified:

functionInsert (Newelement,item) {//modified pre-insert Operation    varNewNode =NewNode (newelement); varCurrentNode = This. Frontnode (item); Newnode.next=Currentnode.next; Newnode.front= CurrentNode;//increase the front point to the precursor nodeCurrentNode.next.front = NewNode;//of the addedCurrentnode.next =NewNode;}functionRemove (item) {varCurrentNode = This. Find (item);//Locate the node you want to delete    if(Currentnode.next! =NULL) {CurrentNode.front.next= Currentnode.next;//to point the precursor node to the next node of the node that needs to be deletedCurrentNode.next.front = Currentnode.front;//to point the successor node to the previous node of the node that needs to be deletedCurrentnode.next =NULL;//and set the precursor and the subsequent point to be emptyCurrentnode.front =NULL; }    }

Reverse-order display linked list:

You need to add a method to the doubly linked list to find the last node. The FindLast () method finds the last node in the list, eliminating the previous traversal of the chain.

function findLast () {// Find the last node of the linked list    varthis. Head    ;  while NULL ) {        = currentnode.next;    }     return CurrentNode;}

To implement reverse-order output:

function Showreverse () {    varthis. Head, result = "";      This . FindLast ();        while (currentnode.front!=null) {        + = currentnode.element + ""        ; = currentnode.front;    }     return result;}

Test procedure:

var New LinkedList (); List.insert ("A", "Head"), List.insert ("B", "a"), List.insert ("C", "B"); Console.log (list); List.remove ("B"); Console.log (List.show ()); Console.log (List.showreverse ()) ;

Output:

Circular link List

The circular chain list is another form of chain-storage structure. Its characteristic is that the pointer field of the last node in the table points to the head node, and the entire list forms a ring. The loop list is similar to the one-way list, and the node types are the same. The only difference is that when you create a circular list, the next property of its head node points to itself, namely:

1 Head.next = Head

This behavior is transmitted to each node in the list, so that the next property of each node points to the head node of the linked list. The landlord used to represent the circular chain list:

  

  

To modify the construction method:

functionLinkedList () { This. Head =NewNode (' head ');  This. Head.next = This. Head;//direct the head node's next point to the head node to form a circular link list          This. Find =find;  This. Insert =Insert;  This. remove =remove;  This. Show =Show;  This. Frontnode =Frontnode;  This. Showreverse =Showreverse;  This. FindLast =FindLast;}

At this point, you need to pay attention to the output method of the list show () and the Find () method, the original way in the loop list will fall into a dead loop, while loop of the loop condition needs to be modified to when the loop to the head node exit loop.

functionFind (item) {varCurrentNode = This. Head;//start from the beginning.          while(currentnode.element!=item&&currentnode.next.element!= ' head ') {CurrentNode=Currentnode.next; }         returnCurrentNode;//Returned node data found, no return null found}functionShow () {varCurrentNode = This. Headvarresult = ';  while(Currentnode.next! =NULL&& currentNode.next.element! = "Head") {result+ = CurrentNode.next.element;//in order not to show the head node.CurrentNode =Currentnode.next; }         returnResult//Modify}

Test procedure:

var New LinkedList (); List.insert ("A", "Head"), List.insert ("B", "a"), List.insert ("C", "B"); Console.log (List.show ()); List.remove ("B"); Console.log (List.show ()) ;

Test results:

This article is for bloggers to revise the article, turn from: http://www.cnblogs.com/qq503665965/

Realization of--javascript of data structure 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.