arrays - Using linked lists in javascript -


are there adventage of using linked lists in javascript? main adventage on arrays (for example) can insert element @ random index without moving every element , not limited size arrays.

however, arrays in js dynamically expanded, shrink, , arrays faster access data. can use array.prototype.splice() method (indeed linked lists still faster one) insert data.

are there advantages (speed , on) of using linked lists on arrays in javascript then?

code of basic linked lists using js.

function list() {    this.head = null;   this.tail = null;    this.createnode=function(data) {     return {data: data, next: null }   };    this.addnode=function(data) {     if (this.head == null) {       this.tail = this.createnode(data);       this.head = this.tail;     } else {       this.tail.next = this.createnode(data);       this.tail = this.tail.next;     }   };    this.printnode=function() {     var x = this.head;     while (x != null) {       console.log(x.data);       x = x.next;     }   } }  var list = new list(); list.addnode("one"); list.addnode("two"); list.printnode(); 

i don't know performance differences. say, linked lists have advantages on arrays in other languages in terms of memory allocation, garbage collection, sparseness, javascript arrays handle of problems. nevertheless still may have reason use linked lists if use case calls kind of data structure: is, need reach items starting front (or either end doubly-linked lists) , proceeding from item next item, without need random access array index.

some colorful metaphors linked lists here: what practical, real world example of linked list?


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -