Cracking the code: Intro guide to data structures.
Tags: algorithms, beginners, react, programming
Published: 2021-12-22 22:07:33


<h1>Intro.</h1>
<p>Why do we need data structures? Well without data structures, we cannot solve algorithms. In that case, if we were only coding for joy, we wouldn't necessarily need algorithms either. But for those who need to see the green, including myself, algorithms are a powerful necessity to prosper in coding interviews. Also, you'll run into data structures more often than not during your programming journey. Data structures have a particular way of organizing data on your computer to be used effectively. If your goal is to build better computer programs, understanding the concept of data structures will definitely help coding abilities become more efficient. At first glance, learning data structures from books, tutorials, etc can be really intimidating, solely based on the heavy presence of math and numbers. If you understand the the reasoning, they become less complicated. </p>

<h1>Need To Know Data Structures!</h1>
<p>So lets think of a way to break this down and get a better understanding! Imagine you were going to bake a cake, and the only ingredient you had was flour. Well you would't be able to bake that cake, unless you had all the rest of your ingredients, like sugar, vanilla extract, eggs, etc. Now if we combine all the necessary ingredients, we're in business and we can get baking! The same analogy works with data structures, it renders useless with a single data item, but when grouped together then we can consider those data items as useful <strong>compound data.</strong> As a result, that data gets stored in a particular data structure, and choosing the right one is important. There isn't a data structure that would be considered best to use, each one has its pros and cons. The way we can get a better understanding of choosing a data structure to manipulate our data is to see how it adds, retrieves, sorts, or searches those items.  </p>
```html
.add()
.get()
.sort()
.search()
```
<p>This is known as <strong>The Big O Notation.</strong> It describes how fast a function is growing. So, if we had a group of data items, and we added a significant amount more to our existing function, then it calculates how much longer each operation would take. </p>

<h3>Data Structures.</h3>
<ul>
<li><strong>Linked Lists</strong></li>
<p>A linked list is a linear data structure that consists of nodes. A node consists of a value, which can simply be a number, and a reference link that points to the next node in that list. Hence, it being a linear structure that keeps going on and on. The start of a linked list is known as the head, where as the last node, is known as the tail or null.</p>
<h4>Pros.</h4>
<li>It's good at <u>adding nodes</u></li>
<li>It's also does well at <u>deleting nodes</u> because we can simply change where our pointer points to.</li>
<h4>Cons.</h4>
<li>It doesn't do so well at <u>retrieving or searching nodes</u> because it is only aware of the node that is next to it.</li>
</ul>

![Linked List diagram](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ydt37rfv3flmaa3qy02.png)

<ul>
<li><strong>Arrays</strong></li>
<p>Arrays have a familiarity across almost all programming languages, so you should probably be familiar with them. An array is a collection of data items stored in adjacent memory locations. Arrays keep memory of all the locations of the data items.</p>
<h4>Pros.</h4>
<li>It's good at <u>retrieving and searching</u> because items are stored in specific memory locations. Which makes them easy to retrieve.</li>
<h4>Cons.</h4>
<li><u>Adding items</u> can sometimes cause issues; as your array grows in size, it can crash into other items stored in memory.</li>
</ul>

![Array diagram](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1ec06i5lbe2ndkm466oj.jpeg)

<ul>
<li><strong>Hash Table</strong></li>
<p>A hash table stores a collection of keys and values. It's an important type of data structure, because after you give a hash table a key, it's able to return its value. Even though it's similar to an array, what makes hash tables special is once you provide a hashing function, it automatically retrieves the data and it does not have to be stored next to one another like an array. </p>
<h4>Pros.</h4>
<li>It's good at <u>adding, retrieving, and removing</u> because items don't crash based on memory location, like arrays.</li>
<h4>Cons.</h4>
<li>Sometimes two keys can hash to the same value, which is known as <strong>collision</strong>. This can be fixed with collision resistant hash functions, such as cryptography. Cryptography uses an algorithm to transform values, so they don't return mimicked values.</li>
</ul>

![Hash table diagram](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s7wkrkh9t78ytjz8a268.jpeg)

<ul>
<li><strong>Stack & Queue</strong></li>
<p>Stack and queues are very similar to each other, and they are built from the same structure as arrays. With stack, the last item you put in is the first item to go out. Two methods used are <strong>.push()</strong> and <strong>.pop()</strong> to carry out this behavior. On the other-hand, queue operates as first item in, is the first item to go out. Adding an item to the end is known as <strong>.enqueue()</strong> and removing an item from the front is <strong>.dequeue()</strong></p>
<h4>Pros.</h4>
<li>Efficient in <u>adding and removing.</u></li>
<h4>Cons.</h4>
<li>Depending on use, usage could be limited, depending on your application.</li>
</ul>

<p>These are some important data structures to keep in mind, next I'd like to cover graph and trees in its own right, since there's a ton of information to go over. Till next time coders...
Below are some resources to study algorithms in preparation for your coding interviews!</p>

![Stack and queue diagram](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dsn43tpjtq6aq9xligxa.png)

<h1>Resources</h1>
<ul>
<li><a href="https://leetcode.com">Leet Code</a></li>
<li><a href="https://www.hackerrank.com">Hacker Rank</a></li>
<li><a href="https://www.codewars.com">Code Wars</a></li>
</ul>