DEV Community

Cover image for Elementary Data Structures with JavaScript - Linked Lists - PART 2πŸš€
devlazar
devlazar

Posted on

Elementary Data Structures with JavaScript - Linked Lists - PART 2πŸš€

Table Of Contents
* πŸ€“ INTRODUCTION
* 🟒 CREATING A NODE
* πŸ”—CREATING THE LINKED LIST
* πŸ˜‰ADD ELEMENT TO THE HEAD OF THE LIST
* 🐈ADD ELEMENT TO THE TAIL OF THE LIST
* πŸ”CREATE A LINKED LIST FROM AN ARRAY
* ❌DELETING ELEMENTS FROM A LIST
* ❌DELETE HEAD
* βœ…CHECK IF AN ELEMENT WITH SPECIFIC VALUE IS IN THE LIST
* πŸ‘¨πŸ»β€πŸ’»CODE
* πŸ™ THANK YOU

πŸ€“ INTRODUCTION

Welcome, my dear hackers!πŸš€ Welcome to yet another blog article about elementary data structures.

If you missed the previous article where we describe the Linked Lists and write pseudocode, you can check it out here:

Today we are going to implement the Singly-Linked list using JavaScript programming language.

Please feel free to connect with me via Twitter, Instagram or LinkedIn

🟒 CREATING A NODE

Every node of the singly linked list, consists of the info or the value stored in the node, and the pointer that points to the next node of the list. Let's create a class describing the node.
Alt Text

πŸ”— CREATING THE LINKED LIST

Since we are creating a Singly-Linked list, I am going to name the class "SLList". The class has a couple of class member variables and those are length (or size) of the list, a pointer to the head of the list, a pointer to the tail of the list.
Alt Text

πŸ˜‰ ADD ELEMENT TO THE HEAD OF THE LIST

We need to implement a function that will add a new element to the head of the list.
Alt Text

🐈 ADD ELEMENT TO THE TAIL OF THE LIST

This function will provide logic for adding a new element at the tail of our linked list.
Alt Text

πŸ” CREATE A LINKED LIST FROM AN ARRAY

Let's assume that our user wants to provide an array of elements, but it is required from us to convert that array into the linked list in the respective order. We will do it like this:

  1. Check if the provided value is an array
  2. If it is, we call a member function that will traverse an array and call our addToHead function. I will omit some code so that we can display a nice image. Alt Text

❌ DELETING ELEMENTS FROM A LIST

This is the most complex function that we will implement. This function needs to differentiate if we were to delete the first (head), the last (tail), or any other element with the specified value. But, we will also provide an indicator that will override the function to delete the first element it encounters with the specified value.
Alt Text

❌ DELETING HEAD

Alt Text

❌ DELETING TAIL

Alt Text

βœ… IS IN LIST

This function will check if our list includes an element with a specific value.
Alt Text

πŸ‘¨πŸ»β€πŸ’» CODE

And finally our code!

πŸ™ THANK YOU FOR READING!

References:
School notes...
School books...

Please leave a comment, tell me about you, about your work, comment your thoughts, connect with me!

β˜• SUPPORT ME AND KEEP ME FOCUSED!
Buy Me a Coffee at ko-fi.com

Have a nice time hacking! 😊

Top comments (0)