Data structures are the fundamental building blocks of modern-day programming. Learn more about what they are and why they matter.
Programming is all about taking data and manipulating it in all sorts of interesting ways. Now, depending on what we are doing, our data needs to be represented in a form that makes it easy for us to actually use. This form is better known as a data structure. As we will see shortly, data structures give the data we are dealing with a heavy dose of organization and scaffolding. This makes manipulating our data easier and (often) more efficient. In the following sections, we’ll find out how that is possible!
For a comprehensive and beginner-friendly look at data structures and algorithms, check out Kirupa's best-selling book on this topic.
BUY ON AMAZON
To better understand the importance of data structures, let’s look at an example. Here is the setup. We have a bunch of tools and related gadgets:
What we want to do is store these tools for easy access later. One solution is to throw all of the tools in a giant cardboard box and call it a day:
If we want to find a particular tool, we can rummage through our box to find what we are looking for. If what we are looking for happens to be buried deep in the bottom of our box, that’s cool. With enough rummaging (and possibly shaking the box a few times), we will eventually succeed:
Now, there is a different approach we can take. Instead of throwing things into a box, we could store them in something that allows for better organization. We could store all of these tools in a toolbox:
A toolbox typically has many compartments to help us organize our tools. While it takes a bit of extra effort to store the items initially, all of this organization makes it easier for us to retrieve a tool later on. Instead of rummaging through a pile of things like an animal, we can go directly to the appropriate pocket or compartment for the tool we are looking for.
What we have just seen are two ways to solve our problem of storing our tools. If we had to summarize both approaches, it would look as follows:
Storing Tools in a Cardboard Box
Storing Tools in a Toolbox
What we can see is that both our cardboard box and toolbox are good for some situations and bad for others. There is no universally right answer. If all we care about is storing our tools and never really looking at them again, stashing them in a cardboard box is the right choice. If we will be frequently accessing our tools, storing them in the toolbox is more appropriate.
When it comes to programming and computers, deciding which data structure to use is similar to deciding whether to store our tools in a cardboard box or a toolbox. Every data structure we will encounter is good for some situations and bad for other situations:
Knowing which data structure to use and when is an important part of being an effective developer, and the data structures we need to deeply familiarize ourselves with are:
Over the next many tutorials, we’ll learn more about what each data structure is good at and, more importantly, what types of operations each is not very good at. By the end of it, you and I will have created a mental map connecting the right data structure to the right programming situation we are trying to address.
Just a final word before we wrap up. If you have a question and/or want to be part of a friendly, collaborative community of over 220k other developers like yourself, post on the forums for a quick response!