TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to JAVASCRIPT

JavaScript Linked List Example

Use a linked list with elements composed of two-part arrays. Each array has a current value and next pointer.
Linked list. In JavaScript we often build up an array of elements. Sometimes an algorithm may be helped by a linked list—less array resizing is needed.
With a two-element array, we can store a "current" value and "next" pointer. Then to loop over the linked list, we can access all the next pointers.
Example program. Let us begin. This program has a global variable called "list." This is an array of linked lists. Each "head" of a linked list is at an index in this list.

AddToList: This method adds an element at an index in the linked list. The current value (even if undefined) is placed as the "next" value.

Display: This loops over the pairs in the linked list at a certain index. It stops when an undefined "next" pointer is encountered.

JavaScript program that creates linked list var list = []; function addToList(n, value) { // Add to the list as this index. // ... Place existing entry as second element in a new array. // ... Replace current array. list[n] = [value, list[n]]; } function display(n) { // Loop over and display linked list. for (var pair = list[n]; pair; pair = pair[1]) { console.log("ITEM: " + pair[0]); } console.log("::END::"); } addToList(5, "cat"); addToList(5, "bird"); addToList(5, "frog"); addToList(1, "blue"); addToList(1, "green"); display(5); display(1); Output ITEM: frog ITEM: bird ITEM: cat ::END:: ITEM: green ITEM: blue ::END::
Some thoughts, performance. Usually an array is a better solution than a linked list. We can place many elements (thousands) into an array with no trouble.

However: For small lists, sometimes linked lists are a better option. No branches are needed to add an element.

For some programs, code size is most important. A linked list can be represented in only a few bytes of JavaScript. This can be an important advantage.
Avoid resizing arrays. This is not often a performance problem, but with linked lists we can avoid resizing arrays. And if we have existing objects we can reuse them for linking.Array
A summary. In most situations a linked list is not the best solution. But occasionally it can benefit a program: it may use less code and require less array resizing.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf