JavaScript data structure-linked List

Source: Internet
Author: User

Gihtub Blog Address

A linked list is a non-sequential, non-sequential storage structure on a physical storage unit that can either represent a linear structure or represent a nonlinear structure, and the logical order of the data elements is achieved through the order of the pointers in the linked List. A linked list consists of a series of nodes (each element in the list is called a node) that can be dynamically generated at run Time. Each node consists of two Parts: one is the data field that stores the data element, and the other is the pointer field that stores the next node address.

function LinkedList () {function Node (element) {this.element = Element;this.next = null;} var length = 0;var head = Null;this.append = function (element) {var node = new node (element); if (!head) {head = node;} els E {var current = head;while (current.next) {current = current.next;} Current.next = node;} length++;}; This.insert = function (element, position) {if (!position | |!element) {return false;} var current = Head,previous = Null,index = 0;var node = new node (element); if (position >= 0 && position <= le Ngth-1) {if (position = = 0) {node.next = Current;head = node;} Else{while (index++ < Position) {previous = Current;current = current.next;} Node.next = Current;previous.next = node;} length++;}}; This.removeat = function (position) {var current = head,previous = Null,index = 0;if (position >= 0 && Position & lt;= length-1) {if (position = = 0) {head = current.next;} Else{while (index++ < Position) {previous = Current;current = current.next;} Previous.next = current.next;} Length--;retUrn current.element;}}; This.remove = function (element) {var index = this.indexof (element); return this.removeat (index);}; This.indexof = function (element) {var current = Head,index = 0;while (current) {if (element = = Current.element) {return Index ;} Index++;current = current.next;} return-1;}; This.isempty = function () {return length = = 0;}; This.size = function () {return length;}; This.gethead = function () {return head.element;}; this.tostring = function () {var current = Head;var s = ' ", while (current) {s + = (", "+current.element); current = Current.next ;} return S.SUBSTR (1);}; This.print = function () {console.log (this.tostring ())};}

  

JavaScript 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.