r/AskComputerScience • u/alexmoline02 • Aug 31 '20
What exactly is a data structure? Can someone put into like layman’s terms please?
25
Aug 31 '20
I like how Sedgewick said it best - it's a way to store information.
An algorithm is a method to solve a problem. You typically use data structures as a means to store data when solving a problem (creating an algorithm).
Examples being:
Linked list - a way to store data in "nodes" which are really just objects in memory that point to other objects in memory.
Array - a way to store lists of data and access it at particular indexes.
Hashmap - a way to store some data and retrieve it based off of a particular key (that gets converted into a somewhat unique key in the background)
10
u/gluedtothefloor Aug 31 '20
Someone mentioned that my response wasn't great for a layman, so I'm going to try this again.
You know how you have a cabinet where you store your clothes? And each drawer is usually reserved for one type of clothing? Like you have one drawer for socks, one for shirts, another for pants, etc. That's a basically a data structure. We could theoretical store all our clothes in a big unorganized chest, but it would be messy and difficult to find anything in it. So instead we use a cabinet with separate drawers to organize our clothes in, and we can find everything fairly easily and in a small amount of time.
And we can think of another data structure for clothes- a closet, which has a different use case. Like we store different types of clothes in a closet than we do in a cabinet. Like you keep your button up shirts and suits or dresses and maybe even your shoes, in a closet instead of a cabinet. So we have another data structure for clothes of a certain type and use case.
Hope this example is elucidating.
5
u/maq0r Aug 31 '20
This is a great example and to further add if I may, those drawers are shaped differently depending on how you need use them, because you wouldn't put socks in the drawer with compartments for jewelry/belts for example.
Maybe all your socks look the same and you don't care in which order you pick them from the drawer as long as you pick two socks. You can use an array for this.
Maybe you want to pull socks in order from when you last washed them. You'd use a LinkedList for this (newly washed is added last). The problem here is, if you want to wear a pair of socks you washed in a particular day, you would have to go through the whole linkedlist to find it. This mean search operations on linkedlists are expensive.
Then we have the jewelry drawer, where you have named compartments(keys) for each type: earrings, rings, belt, watch, etc; these are called Hashes / Maps. It doesn't matter how you write them to the data structure or read them as you always use the key, but once again, makes search difficult as you have to iterate over everything as they're usually unordered.
So when you build an application, deciding which drawer type to use has a lot to do with the order you need to read, write and search through the data structure.
1
4
u/Kootle Aug 31 '20
A dictionary is easy to look things up in, but hard to insert new things into.
A deck of flash cards is hard to look things up in, but easy to insert new things into.
The dictionary and flash cards are different data structures. They store the same data (words + definitions), but in different ways, and are suited for different purposes.
2
u/Elkripper Aug 31 '20 edited Sep 22 '20
Data structures are abstractions for organizing and accessing data.
Imagine a giant library with shelves and shelves of books. That's like your computer's memory. All your data is in there, but it is pretty hard to find just by looking around. It'd take you forever to stumble across the piece of data you wanted, and you might not even recognize it when you saw it.
The card catalog is the data structure (do libraries still have those? If not, they were something like this: https://en.wikipedia.org/wiki/Library_catalog)
If the card catalog analogy doesn't make sense to you, think about the computer in the library lobby that lets you look up books. It doesn't have the actual text of the books on it. It just lets you find the book you care about.
In similar fashion (and glossing over some details for sake of explanation), the data structure isn't your actual data. It is how you find your data.
Think of an array as all the Harry Potter books on a single shelf. They're all together, so to find one all you really need to know is where the shelf is. Then if you want Chamber of Secrets, you grab the second book off the shelf. This is convenient because all the books are right there together and you can jump straight to whichever one you like. But suppose J.K Rowling wrote another book that fit between Chamber of Secrets and Prisoner of Azkaban. The library people would have to scoot down all the other books to fit it on the shelf. For a few books, that's no big deal. But if there were thousands, it'd be a pain.
Think of an array a linked list as a note in each book that says where the next one is. If you want Chamber of Secrets, you first go to Philosopher's Stone (or Sorcerer's Stone, depending on which country you live in) and look at the note. It tells you where to find Chamber of Secrets. So then you go there. This is a bit of a pain if you really only want Goblet of Fire. But if you're reading the books in order, it isn't that inconvenient. And if that new book has to be added in the middle, it is no big deal for the library staff. They just put the new book wherever there is space, change the note in Chamber of Secrets to point to it, and put a note in the new book saying where Prisoner of Azkaban is.
So in closing, the data structure isn't your data. It is information about your data that helps you find what you want.
That's my "layman's terms" explanation. In practice, memory storage is more complicated, it probably isn't technically correct to say an array "isn't really your data" because the array is likely allocated as a block of memory that really does contain your data, etc. But you don't need all the details to have a useful mental picture that gets you started.
More here: https://en.wikipedia.org/wiki/Data_structure
Hope that helps.
Edit: fixed stupid copy-paste error in a month-old comment that nobody will ever read again, because it looked like I don't know the difference between an array and a linked list.
2
u/gluedtothefloor Aug 31 '20
Put basically, it's the way data is organized. The way this data is organized allows us to do certain operations on that data. For example, you could have a very simple unordered list, like '4 2 1 5 7 3 9'. This structure supports things like insertion, deletion, and search. But search and delete in this instance would take linear time...If I wanted to find '9' in the list, I would have to count through the whole thing to find it, and if I wanted to, remove it. What if we wanted to find or delete a member of the list faster? We could have a sorted list, i.e '1 2 3 4 5 7 9'. This data structure now supports faster search operation that only takes O(logn) time. If we wanted to support an 'instant' search in constant time, we could implement a basic hash table, another very useful and common data structure.
1
u/codemasonry Aug 31 '20
I appreciate the effort but I don't feel like O-notation and hash tables are layman's terms.
1
u/gluedtothefloor Aug 31 '20
You're right, I decided to write another response which might be better.
1
u/Derangedteddy Aug 31 '20
Data structures are methods of storing data in a computer system to support a specific use case. Each data structure has unique advantages and disadvantages when it comes to accessing, manipulating, and sorting data, so it's important to know which data structure is best suited for the task at hand.
1
u/claytonkb Aug 31 '20 edited Aug 31 '20
Think of a tax form. It has a standard definition. Every form is identical, that is, has exactly the same defined fields. And each field is required to hold a specific kind of information (name, date-of-birth, and so on). In CS jargon, this is called a record data-type (or sometimes a "struct" or just "structure").
One of the features of records is that they cannot expand or shrink. You cannot add or remove fields from records, they are fixed data-types, just like a government form. You cannot alter what can be entered into the fields of a record, either.
Abstractly, we can envision more general data-structures that can expand or shrink. So, for example, we can imagine an array (or "vector") of items (an ordered list of some fixed length) all having the same type. An ordered list of items having different types is sometimes called a "tuple". There are many such definitions that you will find in a data-structures and algorithms textbook.
But concretely, most computer systems implement these expandable/shrinkable data-structures using records. So, for example, there will be some record called a "Pair" and it will have two fields: "Payload" and "Next". The "Next" field can contain the index to another "Pair" or it can be empty (meaning there are no more records). And "Payload" must contain the index of some data record you want to store. Using this very generic record, you can implement a data-structure called a "Linked List", which is a list that can contain 0, 1, or any number of pairs. Each pair points to some data record which is a member of the list. This is the basic structure used in the programming language Lisp, which is very important in the history of CS.
tl;dr: Mathematically, we can define any kind of data-structure we like, so long as it follows well-defined rules. There are certain kinds of data-structures that occur very frequently in the CS literature and are well-documented in any textbook on algorithms and data-structures. Concretely, most real computer systems implement data-structures using the "record" primitive which is very easy to understand and reason about.
1
u/Kambz22 Aug 31 '20
I can give you a bottle of water, a six pack of water or a case of water.
If a bottle of water is an object..
A six pack of water would be a linked data structure since they are all connected by the plastic.
A case of water is also a data structure of water but they are not really related and you can take out a bottle at any time.
Replace water with data. Its how data is stored and reference, essentially.
1
u/JustAnotherBotbibboo Aug 31 '20
Very loosely defined it's how you organize your memory.
Is a collection of data stored right next to each other? That's an array.
Do each object point to the next object in a sequence? That's a linked list.
1
u/g105b Aug 31 '20
A data structure is a way of describing how computers organise data. Why do we need them? Because computers are designed to work with data in particular ways, and people want computers to do different things with different types of data.
There are definitions for different types of lists, or different types of relationships, etc. because computers need to know how to store the information in a way that will help the operator achieve what they need.
Real world example: in a library, books need to be organised so that customers can find them easily. They are grouped on shelves, separated by category, listed in alphabetical order, and there is an index of where to find each book at the front desk. This is the library's data structure.
1
u/emasculine Sep 01 '20
it's like a sandwich. you have various flavor elements that are trying to different things to make a cohesive whole thing to munch on.
1
1
u/andrew_rdt Sep 01 '20
To start it helps to know what "primitive data" is. Most programming languages have primitive data types which are some form of a number, text, bool (true/false). A data structure is a means to store something more complex than that, usually of the same type. To store a single number you just use a primitive type, to store a list of numbers you need a data structure. Its an interesting topic because of how simplistic a computer is, there are multiple ways to implement something as simple as a list, all have their pros/cons depending on use case. There are other types but a list is the most basic and everyone understands it.
1
u/Forsaken_Ad_9870 Sep 24 '20
I am a computer science student. Earlier i was looking for a course that can teach me programming from scratch. I am going to share my experience about pepcoding that has done an exemplary work.Pepcoding is providing its content free of cost. Since I found their content very much effective and it has helped me in learning programming from the very beginning. Therefore, I am sharing their post with you.
“We at pepcoding have recently made our level 1 DATA STRUCTURES course, free for all.
FREE RESOURCES: For Data Structures and Algo
WEbsite Link:-https://www.pepcoding.com/resources/online-java-foundation
Youtube Link:-https://www.youtube.com/channel/UC7rNzgC2fEBVpb-q_acpsmw
I think this is the best resource because it is quite complete and also has an online judge where you can submit questions.Each and every question has a SOLUTION video with it which you can use to understand the subtlety of the question.
Pepcoding has also decided to make all it's current and future content free of cost for the benefit of the community.Pepcoding site is free of cost, aiming at competitive programming and most important interview questions in top product-based companies.”
0
48
u/coffe4u Aug 31 '20
It's a well defined way to organize data.