DS with JS — Linked Lists

Data Structures with JavaScript — Chapter One — Linked Lists

Gaurav Mehla
Apr 4, 2018 · 5 min read

Introduction

A Linked List is a linear collection of data elements, called nodes, pointing to the neighbouring node by means of pointer.

Under the simplest form, each node is composed of data and a reference (in other words, a link) to the next node or previous node in the sequence.

⚠️ If you wan’t to learn more about linked-lists because in this post I will not cover the theoretical concepts about linked-list. I will just go straight into coding.

Core

The most important part of a linked list is a . It consist of two things —


  1. In Single Linked Lists there is only one pointer (next ) but in Double Linked lists there are two pointers (prev & next ). There can be more than two pointers also. It totally depends on your purpose of creating them.
this while loop is the one on which 75% of linkedLists depends…

Whenever we create an instance of our LinkedList class, the constructor executes and it create a head node with both data and next as null. Then we use its append method to insert items into this list. In this post, we will go through the following methods/operations on Linked Lists —

⚜️ The list

  1. Append
  2. AppendAt
  3. Remove
  4. RemoveAt
  5. Reverse
  6. Swap
  7. IsEmpty & Length
  8. Traverse
  9. Display
  10. Search

and a lot more… So, without further ado…

Lets’ begin!!

Initial List

❗️All the code snippets you will see below are just Pseudo code. If you want to see working code — click here

🔅 Append

(  ) 
current .head;
newNode Node(item)
( current.next null ) {
current current.next
}
current.next newNode;
;
Append Operation

🔅 AppendAt

( ,  ) 
counter 0;
current .head;
newNode Node(item);
( current.next null ) {
( counter pos ){
newNode.next current.next
current.next newNode
;
}
current current.next
counter;
}
false;
AppendAt Operation

🔅 Remove

(  ) 
current .head;
( current null ) {
previous current;
current current.next;
( current.data item ) {
previous.next current.next;
;
}
}
;
Remove Operation

🔅 RemoveAt

( pos ) 
counter 0;
current .head;
( current null ) {
previous current;
current current.next
if( counter pos ){
previous.next current.next;
;
}
counter;
}
;
RemoveAt Operation

🔅 Reverse

() 
current .head.next;
prev null;
next;
(current null) {
next current.next
current.next prev
prev current
current next
}
this.head.next = prev

Reverse Operation

It is easy to understand what we see instead of what we read. So, If the mechanism of this reverse function is still unclear to you, I have made a gif in order to show you how the execution goes.

pffff….finally I made it after spending 3 hours in just dragging and dropping.. but hope so it will help you :)

🔅 Swap

( ,  ) 
current .head;
counter 0;
firstNode;
( current null ) {
current current.next;
( counter nodeOne ){
firstNode current;
} ( counter nodeTwo ) {
temp current.data;
current.data firstNode.data;
firstNode.data temp;
}
counter;
}

Swap Operation

🔅 isEmpty & Length

() 
current .head;
counter 0;
( current.next null ) {
current current.next
counter;
}
counter;
()
.length() < 1
IsEmpty & Length Operation

🔅 Traverse

If you want to execute a function over each node, then you can use traverse. Just pass the function as shown in the example —

(  ) 
current = .head;
( current.next null ) {
(current)
current current.next;
}
;
Traverse Operation

🔅 Display

() 
current .head;
elements [];
( current null ) {
elements.( current.data );
current current.next
}
elements.join(" ");
Display Operation

🔅 Search

(  ) 
current .head.next;
counter 0;
( current ) {
( current.data item ) {
counter
}
current current.next
counter
}
;
Search Operation

There are still a lot of things that we can do with linked-lists. Never stop doing practice. Explore yourself and think of problems by yourself. The more you think of problems — the more you brain will create links with Data Structures. Just keep checking Github repository for more.

Practice

One more important thing…

About this post

This post is the first of its series “”. There will be more in this series. Next will be on .

That’s It

Happy Coding !!

🎧 Listening to “” . Great music for those who are motivated and dreaming about how their life is going to be change soon — Like me :)

If you like this article, please give it some claps 👏 and share it! If you do not like this post or have any type of questions regarding anything that i mentioned in this post. Feel free to ask me. Just post an issue in my “by clicking here.

For more like this, follow me on Medium or Twitter. To ask a Question visit this link. More about me on my website.

_devblogs

Stories for Full-Stack Web developers which help them in…