10

Introduction to Data Structures

 1 year ago
source link: https://kirupa.com/data_structures_algorithms/intro_data_structures_why.htm
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Introduction to Data Structures

by kirupa   |   filed under Data Structures and Algorithms

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!

Onwards!

Right Tool for the Right Job

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:

1_collection_tools_200.png

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:

2_storing_in_box_200.png

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:

4_rummaging_100.png

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:

3_toolbox_200.png

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

  • Adding items is very fast. We just chuck it in there. Life is good.
  • Finding items is slow. If what we are looking for happens to be at the top, we can easily access it. If what we are looking for happens to be at the bottom, we’ll have to rummage through almost all of the items.
  • Removing items is slow as well. It has the same challenges as what we outlined with finding items. Things at the top can be removed easily. Things at the bottom may require some extra wiggling and untangling to get out safely.

Storing Tools in a Toolbox

  • Adding items to our box is slow. There are different compartments for different tools, so we need to ensure the right tool goes into the right location.
  • Finding items is fast. We go to the appropriate compartment and pick the tool from there.
  • Removing items is fast as well. Because the tools are organized in a good location, we can retrieve them without any fuss.

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.

Back to Data Structures

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:

5_tetris_tool_200.png

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!

Kirupa's signature!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK