The BEST way to LEARN DATA STRUCTURES AND ALGORITHMS  

The BEST way to learn Data Structures and Algorithms  

Shares

For anyone learning code, the subjects of data structures and algorithms are straight up daunting! Even if you have been coding a while, these can still be difficult subjects. But this post aims to help you conquer these challenging concepts with resources, guides, and practical advice.

When it came to learning code, this topic had me thoroughly confused. I can remember going back and forth, trying to understand the in’s and out’s of data structures. What are they? How do they work? And how are they used? Questions that I need answering!

If I’m honest, I still have a lot of learning to do. But I now have a system in place which helps me get my head around this intimating subject.

However, before we get into that, I feel a quick intro is necessary to explain the basics of what data structures and algorithms actually are, AND, why they seem to come as a pair.

WHAT is a data structure? WHAT is an algorithm? And WHY are they together?cartoon of a businessman with a question mark

On the surface, they are not that confusing to understand. A data structure is just a collection of data organized in a particular way so that we can perform computational operations on its data effectively.

Think of a bookshelf full of books. You can consider the bookshelf as the data structure, and the books as the data. Not so scary now right?…But where do algorithms come in?

Suppose you wanted to find a certain book on the bookshelf, or sort the books in a certain order. Well, that’s where we use an algorithm.

An algorithm is simply a set of logical instructions for carrying out a predefined task, like finding a particular book for example.

In this case, you would scan through the books on the shelf, attempting to match the book on the self to the book you are looking for. When you have a match, you know you’ve found the your book!

By the way, algorithms are not just reserved for data structures, but they are normally associated because we use algorithms to interact and perform operations on the data structure.

Whenever you are creating a set of instructions to achieve a task, you are creating an algorithm.

And that it in a nutshell. Of course, there is a hell of a lot more to it than that. Just for a start, you have different types of data structure such as Lists, Trees, Stacks, Queue’s, plus more!

But I just wanted to provide a simple overview for now.

Next, I have another question. One you may be asking yourself, and one I DEFINITELY asked myself!

Do you REALLY need to know the ins and outs of data structures and algorithms? 

So here’s the question. Do you NEED to know the background details of how data structures work? Including how to build one and implement commonly used algorithms like merge sort, binary search, bubble sort?

For a new programmer, the question becomes more understandable when you consider that most languages have inbuilt libraries to do the heavy lifting for you. Take Java for example. You can simply write:

And BOOM! There’s your data structure, and you can now use inbuilt methods to add, remove, sort and search data as you please.

So the question remains…Do you? The short answer is YES!

3 BIG reasons why you NEED to learn data structures and algorithmsleader standing on big winner golden cup  

1/ It will IMPROVE your SKILLS and make you a better PROGRAMMER

In my eyes, programmers are problem solvers. It’s a fine skill, I would even say an art. However, this skill doesn’t appear out of thin air. It comes with practice, practice, practice!

By their very nature, data structures provide the perfect playground to practice. They give you a canvas to work with, in the form of interestingly arranged data. Data which you can manipulate till your heart’s content.

Practicing your problem-solving skills is the only way to improve. 

Even just creating your own data structure helps you to think more abstractly. In the end, It all helps you THINK more like a programmer. Which in my opinion is the hardest think about learning to code.

In addition to helping you think a certain way, it’s another skill you can add to your programmers’ toolbox. And the more tools you have in your toolbox the more complex problems you can solve.

2/ NOT EVERY LANGUAGE is the SAME

Although near enough all programming languages encompass the same concepts, they do still differ. Each language will have its own set of API’s and inbuilt methods, separate from the rest.

While you may notice similarities, it’s NOT a case of one to rule them all I am afraid. Learning the API’s and libraries of one language does not necessarily grant you the understanding for the rest. Being totally dependent on the inbuilt functionalities of a language may somewhat constrain you to that language when using data structures.

Whereas having a fundamental understanding of how data structures work and are built, would give you greater versatility.

For the record, I am not saying it’s a bad thing to know or use language API’s when it comes to data structures. Only to be aware that things won’t be EXACTLY the same for every language.

3/ CODING INTERVIEWS…need I say more?

If you’re looking to land a job as a developer, knowing this stuff may be essential!

I have recently been reading Cracking The Coding Interview by Gayle Laakmann McDowell. And after just a few pages it becomes clear that knowing this stuff is kind of important when it comes to getting a job.

You can bet your bottom dollar that some of the top tech companies such as Google, Facebook, Amazon, Microsoft, and Apple will expect you to know your data structures and algorithms. But not just know them, be able to code them by hand.

If you haven’t already heard of whiteboard interviews let me just fill you in. You are presented with a problem, and you have to code up the solution on a whiteboard. And all to often that problem will require data structure and algorithm knowledge.

Sounds scary right? And it’s not just the tech giants who employee these types of interviews. Other, smaller companies follow suit.

This is NOT to say that all software related interviews operate the same way; something even Gayle points out in her book. That said, it’s always better to be prepared!

Did that persuade you?

If not, ask yourself, do you want to MASTER programming? If your answer is yes? Then you know what you need to do. Learn this stuff. You will be better for it.

Data structures and algorithms are fundamental to programming, and fundamentals are very important. Fundamentals build the foundations and allow us to have a full understanding of a subject.

The BEST way to learn??? BUILD, TEAR DOWN, RE-BUILD, REPEAT!learning concept

When it comes to learning how to code the universal rule is to BUILD. That is not to say this is the ONLY way to learn.

You CAN learn by reading textbook after textbook, watching video after video; soaking up all the raw information you can. However, I find it really helpful to see the implementation in action, AND attempt to replicate it myself.

Personally, my advice is to make the whole process of learning to code an ACTIVE one. This means asking questions, research answers and most importantly figure things out. More than anything, I believe this will help the information stick.

Why? Because your brain is engaged, and you offer up 100% of your concentration in order to solve a problem.

This compared to something passive like watching a video. I don’t know about you, but sometimes I have a tenancy to drift off halfway through a video. I don’t feel 100% engaged as I do when I am actively coding and problem-solving.

So, if you’re looking to really understand this stuff I recommend building your own data structure and coding up some algorithms.

Building your own will force you to understand what is going on under the hood. By default, you will learn all you need to know about data structure.

But don’t just follow a tutorial, build it once, then move on. Instead, tear it down and repeat the process until you feel comfortable. I guarantee, each time you do this, things will start to make a little more sense. Till eventually it clicks!

Side note: It personally helps my understanding if I talk out loud through the process. So don’t be afraid to try this as well.

GUIDE to BUILDING a LINKED LIST 

Okay, so I said “build your own data structure”…but where do you even start? Which data structure should you build first?!

Well assuming you are still fairly new to the whole data structure thing, I would recommend a Linked List.

In my personal onion, a Linked List is a fairly beginner friendly data structures to implement. However, rather than just telling you to go away and build one. I wanted to provide some further guidance to help you get started.

Below is a quick guide, written in Java, showing you how to create your own Linked List. Again, with the consideration of a beginner, I chose to go with the simplicity of a Singly Linked List.

Although it’s written in Java, hopefully, it’s simplicity will make it easy to replicate in your preferred language.

But first, WHAT is a LINKED LIST?

Node diagram

A Linked List is known as a linear data structure, which basically means it’s a sequence!

It’s created using a series of  “objects”, which are linked together. Each object can be thought of as a “link”, and each link points to the next one in the list. Hence the name, Linked List.

Side note: If you don’t know about “objects” or object-oriented programming, check out this post: The ULTIMATE NOOB GUIDE to understanding Object Oriented Programming.

These link objects are often referred to as “nodes”, and the “LinkedList” object itself will hold the starting node, referred to as the “head”. The head kicks off the list by pointing to the next node, which will point to the next node, and so on and so forth.

To implement a Linked List we only need two classes, the “Node” class and the “LinkedList” class. But to avoid confusion with the inbuilt Java “LinkedList” class I recommend name it something slightly different. You will see I called mine “MyLinkedList”, not very original I know!

So…What’s a NODE?

A node isn’t too complicated, it simply an “object” with instance variables to hold a “value” and the “next” node (refer to the above diagram). The next node acts as the pointer to the next link in the list.

Because this is a Singly Linked List we only have the next node in order to move forward. If it were a Doubly Linked List we would also have a “previous” node. The previous node in a Doubly Linked List just allows the list to move backwards as well as forwards.

Creating the Node

Before I explained the code, I just want to say that I have used public instance variables rather than “getters” and “setters” to get and set instance values. This is NOT the ideal way to code as it doesn’t provide any protection to the integrity of the object. But if I’m honest, I’m cutting corners to keep things simple!

Just know that you SHOULD be using getters and setters.

Anyway, to the code. As you can see below, we just create a new class called “Node”, and add two instance variables, an integer called “value” and a Node called “nextLink”. It does not have to be an integer it can be any data type, it just has to sort a value. Be that a number, word or object.

Then we set the node object to “null”. By the way, I didn’t have to assign a value to the node, by default an empty variable is null. But it’s just to illustrate that the value should be set to null.

Finally, we create the constructor so when the object is initialized we can pass in the value we want to assign and store. Then the object is good to go!

 

Creating the List

Now, for the final class, the Linked List class itself.

Just create a new class and add an instance variable to hold the “head” node. Then initialize the head as “null” in the constructor.

As I said before the “head” node will be our reference to the head of the list, and point to the next item in the list, which will point to the next, and so on.

After that, we can start adding some methods to the Linked List class, which will enable us to add items, delete items, traverse (move) through the list, and find a particular value. You can see these methods below.

 

Adding to the List

First, we create a new node from the value we want to add.

Then we do a null test to see if the “head” of the list contains anything. If it doesn’t, then we are at the start of the list, and we simply assign the new node to the head of the list.

Otherwise, we add in the new node behind the current head node, then change the new node to the head of the list.

We achieve this by adding the current head into the new node’s next field, at which point we can reassign the head to equal the new node. So now the new node is the head of the list.

The thing to remember when you’re adding nodes this way is that you are not adding it on to the end of the list, but rather the start. This pushes all the other items along, making every new node the head of the list.

Here’s what that looks like…

 

Removing an item from the List

Lets start by create some holders for a current and previous position. The current node will be set as the head, and the previous node will be set as null.

Why do we need a current and previous node? Because later we are going to have to reassign the nodes in order to overwrite (delete) the specified node, while making sure the sequence of links are maintained.

Once the set up is done, we test the current value to see if it matches the value passed. If it does, all we need to do is reassign the head to the next node in the list. This overwrites the node with the next item, thus deleting it from the list.

Of course, if that statement returns false we are going to be looping through the nodes in the list to find the one we ARE looking for. This we do with a simple While loop; testing the current value to see if it matches the value passed.

If the current value does not match, it may be because it’s in the next node, which means we need to continue looping…OR it’s because we are at the end of the list, meaning the value does not EXIST!

So let see if it exist’s first, then we will continue looping if necessary.

To do this we test if the next node in the list is null. If it is, we are at the end of the list, meaning the item doesn’t exist! In which case, we print a statement and jump out of the method with a “return”.

Otherwise, we need to continue moving through the list and testing the next node in the sequence. To move through the list we simply reassign the previous to the current and the current to the next. Then, test all over again!

Once the While loop finishes we can be sure we have found the node to be deleted. So it’s another case of swapping values in order to delete the node.

Here’s what that looks like…

 
 
Traversing the List

This is a fairly straightforward method. We begin by creating a temporary node to work with and passing the value of the head node in.

Next, we test to see if the node i.e. head is null, which would indicate that the list is empty. If that’s true, we just want to print a statement to say “List is empty”.

If the test comes back false, we While loop through the nodes in the list, testing if they are NOT empty. As long as they are NOT, we can print out the value, then move to the next by reassigning the temporary node to the next node.

Simple! Here’s what that looks like…

 

Finding an item in the List

Again, let’s start by creating a temporary node to hold the head.

Next, we test! The first test we do is to see if the node, aka the head, is null. If it is, then there is nothing in the list and we jump down to the end and print a statement to say “List is empty”.

The next test is a While loop. We loop through and test the value passed against the value in the node. As long as they DON’T match, we continue looping.

Within this loop we do yet another test to see if the next node is null. This would indicate we are at the end of the list and didn’t find the item. In which event, we print out a statement, then jump out of the method with a “return” as we have done before.

As long as there is still another valid item we continue moving through the list the same way we have done in previous methods (by reassigning the node to the next node in the list).

Finally, when the condition of the loop is meet, we can safely assume the values match, meaning we found the item. So if you want you can print a statement and shout it from the rooftops!

Here’s what that looks like…

Other learning resources 

I created the above guide to serve as a beginners introduction. However, I have compiled a few further resources below, should you wish to learn more. As we speak I am compiling a larger list to put into a separate blog post. So stay tuned!

 Feature image Designed by Freepik

Sharing is caring!

About the Author Owen

Creator of Code Dad, father and self-taught programmer. My mission is to balance life, work, family, all while learning to code, and hopefully help others who want to do the same.

follow me on: