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:
Article No Longer Available
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.
π 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.
π 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.
π 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.
π 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:
- Check if the provided value is an array
- 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.
β 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.
β DELETING HEAD
β DELETING TAIL
β IS IN LIST
This function will check if our list includes an element with a specific value.
π¨π»βπ» 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!
Have a nice time hacking! π
Top comments (0)