Intro to Data Structures + Algo [Part 3]
Tags: challenge, beginners, javascript, algorithms
Published: 2020-08-17 03:56:59

---
title: Intro to Data Structures + Algo [Part 3]
published: true
description: 3th part of the data algo series
tags: challenge, beginners, javascript, algorithms
cover_image: https://images.unsplash.com/photo-1531265550384-4f789f9fc8f6?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80
---

Hello everyone, it's been another week and is time for the next part of our series. Today we are going to be talking about Linked list. We are covering two type of linked list, singly and doubly. So what problems did we had with static arrays? 

We were able to allocate a certain amount of data or memory next to each other but then both dynamic and static arrays can increase their memory once they hit a certain limit and double the memory in another location. However, this operation of doubling up memory has a performance implication and it cost **O(n)**. Additionally, arrays have bad performance for operations such as delete and / or insertion that have to shift indexes over. Specially when you do these operations when is not at the end of the array.

Then, as to our rescue came the hash tables which allows for you to store whatever you want in memory. Hash tables will know where to store it for us and take care of it. Unfortunately, hash tables by default aren't ordered but don't sweat it that where Linked list will make our life easier. Does this mean that Link lists are the go to alternative now? No, there is never an absolute answer in data structures. Remember, there will always be some trade offs.

**Note**: Hashes will be covered in a future post. 🙇

A singly linked list contains a set of nodes. These nodes contains two elements, the value of the data that you want to store and a pointer to the next node in line. The first node is called the 'Head' and the last one is 'Tail' the after the last node it points to 'Null'.

![example1](https://media.geeksforgeeks.org/wp-content/cdn-uploads/gq/2013/03/Linkedlist.png)
<figcaption>  a linked list consists of nodes where each node contains a data field and a reference link to the next node in the list.</figcaption>


### **Example 1** - Pseudo Code

```javascript
/* 
head
eggs 
4   --> ham     tail
        6  --> cheese
               12    --> null
*/
```

For more visual representation of algorithms use [this](https://visualgo.net/en) website.

Are linked list good or bad? First, on arrays you are able to start from whatever index of your choosing but on linked list you can only iterate from the head until you hit null.

Let's talk about what do we mean about a pointer? refers to the next place (object or node) in memory. Simplest example for pointers in javascript is the following:  ```js 
const objectOne = {stock1: 1.33}; const objectTwo = objectOne;
```

Now let's jump to a more concrete example. This upcoming piece of code will be the base for our first exercise. The challenge is to complete this linked list. **Tip:** Try completing an append() method that allows you to add the value to the list.[**SOLUTION**](https://repl.it/join/fnmljwgs-mius00)

```javascript
// 23 --> 51 --> 12
class LinkedList {
  constructor(val) {
    this.head = {
      value: val,
      next: null
    };
    this.tail = this.head;
    this.length = 1;
  }
  append(val) {
    // complete it for the linkedList pt 2
  }
}

let myLinkedList = new LinkedList(23);
myLinkedList.append(51);
myLinkedList.append(12);
```

Now let's try creating a prepend() method for our list under the append method. Check out the Linkedlist2 script for the [**Solution**](https://repl.it/join/fnmljwgs-mius00)


![tacoAll](https://media.giphy.com/media/WNs0uptipSG40/giphy.gif)
<figcaption>Until next time everyone eat your tacos!</figcaption>

### Resources

- [VisualAlgo.net](https://visualgo.net/en) by Dr Steven Halim
- [GeekforGeeks](https://www.geeksforgeeks.org/data-structures/linked-list/)

## **Conclusion**

Hey y'all, thanks for sticking around in this mini series. We have covered the basics about linked list and Hopefully we have all learned a thing or two from today examples. If you guys have any suggestions or better examples please share them in the comment section below. P.S: I'm going to start updating all blogs now. Apologies for any inconvenience.