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