Implementation of two-way linked list and two-way circular linked list in JavaScript Data Structure

Source: Internet
Author: User

Implementation of two-way linked list and two-way circular linked list in JavaScript Data Structure

The difference between a two-way linked list and a common linked list is that in a linked list, a node only links to the next node. In a two-way linked list, a link is bidirectional: one link goes to the next element, the other chain is a forward element.

A two-way linked list provides two methods to iterate the list: From start to end, or vice versa. You can also access the next or previous element of a specific node. In a one-way linked list, if you miss the element to be found during the iteration list, you need to return to the starting point of the list and start iteration again. This is an advantage of a two-way linked list.

Two-way linked list: A one-way linked list can only traverse a table node in one direction, while a two-way linked list with a forward pointer can be added in the node pointer field to traverse the node in two directions. This allows the two-way linked list to traverse the entire linked list on any node.

function DoublyLinkedList() {   var Node = function(element) {     this.element = element;     this.next = null;     this.prev = null;   };    var length = 0,     head = null,     tail = null;    this.append = function(element){     var node = Node(element),       current,       previous;          if(!head){       head = node;       tail = node;     }else{       current = head;       while(current){         previous = current;         current = current.next;       }        node.next = current;       current.prev = node;       previous.next = node;       node.prev = previous;     }      length++;     return true;   }    this.insert = function(position,element){     if(position > -1 && position < length){       var node = new Node(element),         current = head,         previous,         index = 0;        if(position === 0){          if(!head){           head = node;           tail = node;         }else{           node.next = current;           current.prev = node;           head = node;         }        }else if (position === length -1){         current = tail;         current.next = node;         node.prev = current;       }else {         while(index++ < position){           previous = current;           current = current.next;         }         node.next = current;         previous.next = node;         current.prev = node;         node.prev = previous;       }        length++;       return true;     }else{       return false;     }   };    this.removeAt = function(position){     if(position > -1 && position < length){       var current = head,         index = 0,         previous;        if (position === 0) {         head = current.next;          if(length === 1){           tail = null;         }else{           head.prev = null;         }       }else if(position === length - 1){         current = tail;         tail = current.prev;         tail.next = null;       } else{         while(index++ < position){           previous = current;           current = current.next;         }          previous.next = current.next;         current.next.prev = previous;       };       length-- ;        return current.element;     }else{       return false;     }   };    this.remove = function(element){     var current = head,       previous;      if(current.element === element){       head = current.next;     }     previous = current;     current = current.next;      while(current){       if (current.element = element) {         previous.next = current.next;         current.next.prev = previous;       }else{         previous = current;         current = current.next;       }     }     return false;   };    this.remove = function(){     if (length === 0) {       return false;     };      var current = head,       previous;      if(length === 1){       head = null;       tail = null;       length--;       return current.element;     }      while(current){       previous = current;       current = current.next;     }      previous.next = null;     length--;     return current.element;   };    this.indexOf = function(element){     var current = head,       index = 0;      while(current && index++ < length){       if (current.element === element) {         return 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;   };    this.getTail = function(){     return tail;   }; } 

Two-way cyclic linked list: connects the head and tail pointers of the two-way linked list to form a two-way cyclic linked list. This kind of linked list can traverse nodes from any node in two directions at the same time, and the query speed is also the fastest.

/* Bidirectional cyclic linked list */function DoublyCircularLinkedList () {var Node = function (element) {this. element = element; this. next = null; this. prev = null;}; var length = 0, head = null, tail = null; this. append = function (element) {var node = new Node (element), current, previous; if (! Head) {head = node; tail = node; head. prev = tail; tail. next = head;} else {current = head; while (current. next! = Head) {previous = current; current = current. next;} current. next = node; node. next = head; node. prev = current;}; length ++; return true;}; this. insert = function (position, element) {if (position> = 0 & position <= length) {var node = new Node (element), index = 0, current = head, previous; if (position = 0) {if (! Head) {node. next = node; node. tail = node; head = node; tail = node;} else {current. prev = node; node. next = current; head = node; node. prev = tail ;}} else if (position === length) {current = tail; current. next = node; node. prev = current; tail = node; node. next = head;} else {while (index ++ <position) {previous = current; current = current. next;} current. prev = node; node. next = current; previous. next = node; node. prev = previous;} length ++; return true;} else {return false ;}}; this. removeAt = function (position) {if (position>-1 & position <length) {var current = head, index = 0, previous; if (position = 0) {current. next. previous = tail; head = current. next;} else if (position = length-1) {current = tail; current. prev. next = head; head. prev = current. prev; tail = current. prev;} else {while (index ++ <position) {previous = current; current = current. next;} previous. next = current. next; current. next. prev = previous;} length --; return true;} else {return false ;}}; this. remove = function (element) {var current = head, previous, indexCheck = 0; while (current & indexCheck <length) {if (current. element = element) {if (indexCheck = 0) {current. next. prev = tail; head = current. next;} else {current. next. prev = previous; previous. next = current. next;} length --; return true;} 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; tail = null; length --; return current. element;} while (indexCheck ++ <length) {previous = current; current = current. next;} previous. next = head; tail = previous. next; length --; return current. element;}; this. indexOf = function (element) {var current = head, index = 0; while (current & index ++ <length) {if (current. element = element) {return index;} current = current. next;} return false;}; this. toString = function () {var current = head, indexCheck = 0, string = ''; while (current & indexCheck <length) {string + = current. element; indexCheck ++; current = current. next;} return string;}; this. isEmpty = function () {return length = 0 ;}; this. getHead = function () {return head;}; this. getTail = function () {return tail;}; this. size = function () {return length ;};}

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.