Detailed knowledge about JavaScript Data Structure linked list

Source: Internet
Author: User
I recently read the book "JavaScript data structures and algorithms" to explain the knowledge of the javascript Data Structure linked list. I think this is a short board.

Linked List: stores ordered element sets, but unlike arrays, elements in the linked list are not placed continuously in the memory. Each element consists of a node that stores the element itself and a reference (also called a pointer or link) pointing to the next element.

Benefit: You can add or remove any item. It will expand as needed without moving other elements.

Differences from Arrays:

Array: You can directly access any element at any location;

Linked List: to access an element in the linked list, You Need To iterate the list from the starting point (header) until you find the required element.

Take notes.

Function compute list () {var Node = function (element) {this. element = elementthis. next = null} var length = 0var head = nullthis. append = function (element) {var node = new Node (element) var currentif (head = null) {// The linked list is empty head = node} else {// The linked list is not empty current = head // The linked list loop until the last while (current. next) {current = current. next} current. next = node} length ++ // update the length of the linked list} this. insert = function (position, element) {var node = new Node (element) var current = headvar previusvar index = 0if (position> = 1 & position <= length) {// determine whether to cross-border if (position = 0) {// Insert the first node. next = currenthead = node} else {while (index ++ <position) {previous = currentcurrent = current. next} node. next = currentprevious. next = node} length ++ // update the linked list length return true} else {return false} this. indexOf = function (element) {var current = headvar index =-1 while (current) {if (element = current. element) {return index} index ++ current = current. next} return-1} this. removeAt = function (position) {if (position>-1 & position
 
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.