I’ve finally worked up the courage to dive into data structures, interestingly I find data structures to be slightly less terrifying than algorithms. I’ve been doing some LeetCode and I am absolutely starting with all easy questions but recently I got a question that required the solution to be solved using a data structure, I was not ready. If you’ve been reading my blogs you know that I attended the Flatiron School’s immersive software engineering program. In bootcamps you’re only taught how to build applications on the fly, not solve complex algorithms and solve it using the best solution that not only saves time but space. So before even attempting the LeetCode problem, I had to take a step back and ask myself what is a data structure and how can I explain it to someone who knows absolutely nothing about tech?
A particular way of organizing data in a computer so that it can be used effectively. Think of everyday items that you have stored in your home and things that store them. For instance I am OCD about the way I store my clothes, I fold my shirts a certain way so that I not only maximize the drawer space but also so I can easily grab a particular shirt that I’m looking for.
Data structures are similar, depending on the type of data you have and what you need to do with it determines the data structure that you’ll use to access what you need. Things to consider are how much longer will the algorithm take as the input grows? How much space in memory does the algorithm need? This is how Big O is calculated. For my LeetCode problem I was able to solve it by using the merge method but if you recall the solution expected nodes.
The most common data structure, it is linear and has a fixed size/length. An array data structure is used to store elements at a contiguous location also known as indexes. The pro of using an array is you can easily access an element if you know it’s index but usually you don’t know an elements index in programming. This is where arrays aren’t the greatest, they’re very slow. If you needed find a specific element you would have to start at index 0 & search every element until you get to the index you need. Imagine is your array had 200k elements and the one you need is at index 124,576! That would take a long time to execute! Arrays may be slow and have linear time but they have a special place in my heart.
Linked List have nodes and these nodes contain data, they also have pointers that are linked to other nodes that have data. The pointers also represent the location in memory of the connected nodes.
Insertion and deletion in a linked list happens very quickly and it happens in constant time. If you needed to add a node into an existing list all you’d have to do is change the pointer. Another plus is that you can add/delete as many nodes as you please, unlike arrays there’s no fixed size/length. Although insertion and deletion is faster with linked list, searching for information functions similarly to an array. You’d have to start from the head and search through all nodes to find what it is your looking for.
When it comes to searching for information, I wouldn’t use an array or a linked list. Linked list seems like the better option over an array for a lot of situations. It uses less memory & it’s efficient if data needs to be added or removed. I am working on mastering or understanding to the best of ability of linked list this week, next week I will dive into double linked list!! Happy Coding !!