JavaScript Data Structure: single-chain table and circular linked list; javascript single-chain

Source: Internet
Author: User

JavaScript Data Structure: single-chain table and circular linked list; javascript single-chain

Data Structure series preface:

As the basic knowledge of programmers, data structure requires us to master it. Recently, I also launched a secondary study on the data structure to make up for the pitfalls I dug in the past ...... At that time, I followed the course and did not implement any data structure, let alone solve the problem by using the data structure. Now I am trying to fill in the traps. Here, I am reminding you that if you are still in school, never underestimate any basic course, either double effort is required, or a person's ability is directly affected ...... Never dig yourself

Go to the topic. Here is a brief introduction to the data structure of the linked list:

A linked list is a non-linear and non-continuous data structure on physical storage units (it is logically linear in data). Each node of a linked list consists of two domains: data domains and pointer domains. The actual data is stored in the data field, and the pointer field stores pointer information, pointing to the next element or the previous element in the linked list. Because of the existence of pointers, the storage of linked lists in physical units is discontinuous.

The advantages and disadvantages of linked lists are also obvious. Compared with a linear table, a linked list is more efficient in adding and deleting nodes, because it only needs to modify the pointer information to complete the operation, rather than moving elements like a linear table (array. Similarly, the length of the linked list is theoretically unlimited (within the memory capacity range) and can dynamically change the length, which has a great advantage over linear tables. Correspondingly, because linear tables cannot access nodes randomly, they can only use pointers to traverse and query the nodes along the linked list. Therefore, the efficiency of accessing data elements is relatively low.

Below is the JS Section

Common methods and descriptions encapsulated here:

Method Description
Append (element) Add a node element to the end of the linked list
Insert (position, element) Insert a node element to the position
RemoveAt (position) Delete a node based on the index position
Remove (element) Search for and delete a given node element
Remove () Delete the last node in the linked list
IndexOf (element) Search for and return the index value of the given node element
IsEmpty () Judge whether the linked list is empty
Size () Get chain table length
ToString () Convert to string output
GetHead () Get the header Node
GetTail () Get the End Node

The algorithm descriptions for common methods are not written here. I believe everyone can easily understand and understand them. After all, they are very basic knowledge.

Single-chain table:

Function compute list () {/* Node definition */var Node = function (element) {this. element = element; // store the node content this. next = null; // pointer} var length = 0, // store the length of the linked list head = null; // the header pointer this. append = function (element) {var node = new Node (element), current; // pointer used for the operation if (! Head) {head = node;} else {current = head; while (current. next) {current = current. next;} current. next = node;} length ++; return true ;}; this. insert = function (position, element) {if (position> = 0 & position <= length) {var node = new Node (element), current = head, previous, index = 0; if (position = 0) {node. next = current; head = node;} else {while (index ++ <position) {previous = current; Current = current. next;} node. next = current; previous. next = node;} length ++; return true;} else {return false ;}}; this. removeAt = function (position) {if (position>-1 & position <length) {var current = head, previous, index = 0; if (position = 0) {head = current. next;} else {while (index ++ <position) {previous = current; current = current. next;} previous. next = current. next;}; length --; Return current. element;} else {return null ;}}; this. remove = function (element) {var current = head, previous; if (element = current. element) {head = current. next; length --; return true;} previous = current; current = current. next; while (current) {if (element = current. element) {previous. next = current. next; length --; return true;} else {previous = current; current = current. next ;}} retu Rn false;}; this. remove = function () {if (length <1) {return false;} var current = head, previous; if (length = 1) {head = null; length --; return current. element;} while (current. next! = Null) {previous = current; current = current. next;} previous. next = null; length --; return current. element;}; this. indexOf = function (element) {var current = head, index = 0; while (current) {if (element = current. element) {return index;} index ++; current = current. next;} return false;}; this. isEmpty = function () {return length = 0 ;}; this. size = function () {return length ;}; this. toString = function () {var current = head, string = ''; while (current) {string + = current. element; current = current. next;} return string;}; this. getHead = function () {return head ;}}

Cyclic linked list: based on a single-chain table, the pointer of the tail node points to the header node, which constitutes a cyclic linked list. A circular linked list can traverse the entire linked list from any node.

function CircularLinkedList(){  var Node = function(element){   this.element = element;   this.next = null;  }   var length = 0,   head = null;   this.append = function(element){   var node = new Node(element),    current;    if (!head) {    head = node;    node.next = head;   }else{    current = head;     while(current.next !== head){     current = current.next;    }     current.next = node;    node.next = head;   };    length++;   return true;  };   this.insert = function(position, element){   if(position > -1 && position < length){    var node = new Node(element),     index = 0,     current = head,     previous;      if (position === 0) {      node.next = head;     head = node;     }else{      while(index++ < position){      previous = current;      current = current.next;     }      previous.next = node;     node.next = current;     };     length++;    return true;   }else{    return false;   }  };   this.removeAt = function(position){   if(position > -1 && position < length){    var current = head,     previous,     index = 0;     if (position === 0) {      head = current.next;     }else{      while (index++ < position){      previous = current;      current = current.next;     }      previous.next = current.next;    };     length--;    return current.element;   }else{    return null;   }  };   this.remove = function (element){   var current = head,    previous,    indexCheck = 0;    while(current && indexCheck < length){    if(current.element === element){     if(indexCheck == 0){      head = current.next;      length--;      return true;     }else{      previous.next = current.next;      length--;      return true;     }    }else{     previous = current;     current = current.next;     indexCheck++;    }   }   return false;  };   this.remove = function(){   if(length === 0){    return false;   }    var current = head,    previous,    indexCheck = 0;    if(length === 1){    head = null;    length--;    return current.element;   }    while(indexCheck++ < length){    previous = current;    current = current.next;   }   previous.next = head;   length--;   return current.element;  };   this.indexOf = function(element){   var current = head,    index = 0;    while(current && index < length){    if(current.element === element){     return index;    }else{     index++;     current = current.next;    }   }   return false;  };    this.isEmpty = function(){   return length === 0;  };   this.size = function(){   return length;  };   this.toString = function(){   var current = head,    string = '',    indexCheck = 0;    while(current && indexCheck < length){    string += current.element;    current = current.next;    indexCheck++;   }    return string;  };   } 

Usage:

External Extension Method of the class:

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.