Not C and CPP can also learn data structure--javascript implementation of doubly linked list

Source: Internet
Author: User
Tags prev

This article copyright belongs to the blog park and the author Wu Di himself common all, reproduced and crawler Please indicate the original link http://www.cnblogs.com/tdws/

In the afternoon, we shared JavaScript to implement the one-way list, and at night we added the two-way list. The implementation of the linked list is not very well understood can be: http://www.cnblogs.com/tdws/p/6033209.html

The difference between a doubly linked list and a linked list is mainly in his two-way search. Because in this structure we have designed a reference or pointer to each node's prev (look up) and a reference or pointer to next (look down). Thanks to this structure you can do both forward and reverse lookups. You can also find the element at an index position, based on its length, whether you are using forward or reverse, depending on yourself.

Directly on the code, the explanation in the comments:

First look at the overall structure of the code:

Here are the specific implementations:

function Doublylinkedlist () {varNode =function (Element) { This. Element =element;  This. Next =NULL;//who's next?             This. prev =NULL;//who was the last one?        }; varHead =NULL; varLength =0; varTail =0;  This. Insert =function (position, element) {if(Position >=0&& position <=length) {                varnode =NewNode (Element); varCurrent =Head; varindex =0; varprevious; if(Position = =0) {                                                if(Head = =NULL) {//Empty linked listHead =node; Tail=node; } Else{Head= node;//new element as headHead.next = current;//the next node in the head is the old headCurrent.prev = node;//the previous node of the old header is the new element                    }                } Else if(Position = = length) {//TailCurrent =tail; Current.next= node;//the next node of the old trailer is the new nodeNode.prev = current;//the previous node of the new node is the old tailtail = node;//Update tail node as new element}Else {                     while(Index <position) {Previous=Current ; Current=Current.next; Index++; }                                                   //the node after the Traverse current is currently positionNode.next = current;//The new node next is currentPrevious.next = node;//next to the upper node is the new elementNode.prev= Previous;//the last node of the new element is previouscurrent.previous = node;//the last node of current is a new element} length++; return true; } Else {                return false;        }        };  This. removeAt =function (position) {if(Position >-1&& Position <length) {                varCurrent =Head; varindex =0; varprevious; if(Position = =0) {Head= Current.next;//assigns the head to the next node and does not care if it is null                    if(Length = =1) {//if the length is 1 head is already null, the tail is set to nullTail =NULL; } Else{//Head has been assigned to the next nodeHead.prev =NULL;//prev of head is null                    }                } Else if(Position = = Length-1) {//last elementCurrent =tail; Tail=Current.prev; Tail.next=NULL; } Else {                     while(index++ < position) {//Common Intermediate ElementsPrevious =Current.prev; Current=Current.next; }                                               //gets the current position element after traversalPrevious.next = Current.next;//the prev of the current osition element points to the next element of the current postion elementCurrent.next.prev = previous;//In short, over a} length--; returncurrent.element; } Else {                return NULL;        }        };  This. GetLength =function () {returnlength;        };  This. toString =function () {varCurrent =Head; var string="';  while(current) {string+=','+current.element; Current=Current.next; }            return string;    }; }

Don't say much nonsense, about the two-way linked list of articles on the Internet a lot of search.

By the way, the list type in the Redis five data types, we know that we can look for the elements in the Redis list, or we can find the elements in reverse. This is also a doubly linked list in the actual use of it.

Redis related articles link http://www.cnblogs.com/tdws/tag/NoSql/

If I share a bit for you to help, welcome to click the red button below, I will continue to output share.

Not C and CPP can also learn data structure--javascript implementation of doubly 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.