Ok so you got a whole stack of cups. Different sizes, colors, some plastic, some glass, some skinny, some fat. All in one big stack.
You want one in particular. How do you describe it? Well say you want a cup that’s large, blue, plastic, and skinny.
How do you find it? You could go through each one and ask if it’s the cup you want. Or you could start by eliminating all the non-large, non-blue, non-plastic, non-skinny. Then you’d be left with what you can choose from.
Easy to do with 50 cups, now imagine an insanely large amount of cups. Like 10 million. And you want one.
How can you do that efficiently? Well you need to sort them by their attributes. You’d make a table where you’d say each cup has a number (called a key) and in that row you’d have each attribute type. Each type can have a subset of attributes like plastic shiny vs plastic matte and that would be a matrix in the attribute.
That’s data structure. Logically defining your attributes.
At its most basic, there are tradeoffs you can make when you’re storing data, and those tradeoffs impact how you can later process the data.
For example, imagine you need to store a bunch of names so that you can assign seats to them at an event. You could have everyone line up and give their name to a person who writes them down in order. This is slow, but it’s highly organized, so you won’t take down more names than there are seats and it preserves the order. Alternatively, you could have everyone write down their own name and throw them in a bucket and have one or more people pull them out to assign seats. This is comparatively disorganized but can be done really fast, by dividing the work among multiple people.
Data structures in computers, are the set of ways you can organize storing, reading and modifying data and the associated rules and trade-offs that go with them.
Ultimately, computer data is just patterns of high and low electrical charges. Each high/low charge is called “a bit”.
We can technically call a number a “data structure”. It means we pick a number of bits and say that certain patterns mean certain numbers.
A more complicated data structure can refer to multiple things. Say we want to represent a list of 10 numbers. If each number is 8 bits, we could just point at 80 bits and say “that’s 10 numbers”.
But what if we want different sizes of lists? Well, then we might say “The first 8 bits represent how many numbers there are”. So the list of 10 numbers now takes 88 bits: the first 8 bits will be in the pattern for “10”, then we know the next 80 bits represent 10 numbers.
We can get even more complicated. Maybe we want a list of numbers, but the numbers aren’t all in a neat little line in memory. Well, we can store a *memory address* in a list. That way we still have a list that represents a number of things, but now those things can be scattered throughout memory.
So a data structure is really just a way for us to organize memory and dictate rules about what the values represent. But it’s not very useful to describe them to people who aren’t familiar with programs, because we don’t tend to use complex data structures unless we’re solving relatively complex problems.
Have they used a spreadsheet? A data structure is like a spreadsheet, where the columns in the spreadsheet are the properties in a data structure, and each row is an individual record.
If they don’t understand spreadsheets, I would probably say that a data structure is simply a way to combine different types of information under one name. An Employee structure, for example, might have a name, birthdate, and hourly wage. Each employee is represented by a single instance of an Employee structure.
A computer does many simple tasks very fast. So in order to do complicated things, the concepts and ideas need to be organised and broken down in many simpler things.
It also needs to manage large quantities of small bits of information.
Data structures are reusable concepts that help do that, like a ‘stack’. A stack is a simple idea, like a stack of plates, or books. It has a definite behavior, like you can only take and remove the top plate or book. These limitations seem counterproductive but are extremely fast and efficient in some specific situations.
That’s why we have a bunch of such structures used in different scenarios. Like ‘queues’. Stacks are ‘last-in-first-out’ (or LIFO), like mentioned, only the first is accessible. Queues are ‘first-in-first-out’ (or FIFO).
There are several more, and each cab have their variations. Used to togheter, they can form very complex systems (like a computer operating system).
A data structure is a structure thats holds data (numbers, letters) in an efficient way for the task you want to do. There are commonly known data structures and patterns used which have been discovered to be very efficient and fast for certain tasks. But there is also mostly a tradeoff involved; e.g finding a specific number in a list. Depending on how long this list can be, if you only need to find but never change (adding or removing) or how you can access the information, you decide which tradeoffs you can accept and thus, which data structure will help solve your problem best. These tradeoffs are typically described as BigO notation
One of the more basic ways to illustrate data structures would be to use everyday things.
For example a diary. It comes in a book with pages separated by dates. And entries written for that day on the page. That is a simple data structure – an agreed upon method to record, store and retrieve data.
If someone creates a list of contacts. A typical way is to manage the list is by last name, first name, phone number, email address. Perhaps in a table. That is a data structure.
For a more complex example, say you want to prepare a meal and use a particular recipe book for the different dishes. So you write a list of recipe names followed by the page number. So this is now a list that instructs you where to find the data in another location – the recipe book – rather than making a copy of the recipe itself. This is also a data structure.
Take this a step further. Say you want to plan out meals for a month. One way would be to take a monthly calendar and on each daily entry you write the name of the recipe book and the page. Now you have an ordered list (by date) pointing you to a variety of lists (different recipe books) and how to locate the data (the page in a specific recipe book). That calendar with the list of entries plus the recipe books constitute a data structure.
Once you strip away all the programming etc, that is basically what data structures are. The formal study of the methods of organizing, storing, retrieving, and locating data.
a data structure is a box that holds stuff.
you can choose to just dump everything into the box or you can get all fancy.
how can you make that box fancy? Maybe after you put everything in, it will always give them back to you in the same order you put them in.
Or, maybe it give them back to you in reverse.
what if you wanted to find something in the box fast? well, you’ll need some way to sort and store it better.
Choose a box based on how you want to use/find your items that are stored in the box.
The best non-technical analog would be a map.
Computers can access any various parts of their memory, but they need a “map” to know where everything is. There are many different types of these maps and organizing the data in different ways can change how quickly you’re able to access, add, remove, and edit the data.
Latest Answers