-
Re: Data Structure
Hello. I'm trying to learn data structures but it is not clear for me why to use them. Why to use a different structure for example a stack instead of an array. I would appreciate it if someone could give me a simple example to understand. For example, i have a student: name, age, id... why to store these data in a differnet structure and not in an array? Thank you...
-
Re: Data Structure
Reason Numero Uno: Arrays have a fixed size. Data structures can increase and decrease their size.
Another reason: Arrays do not have any methods (other than those inherited from Object). Data structures have plenty of methods that can help you do what you want. Alternatively you can write your own data structure and provide whatever functionality you need. Or write a data structure that extends an existing one to enhance its capabilities.
-
Re: Data Structure
Using data structures effectively is all about thinking about how a program needs to operate. Yes, arrays do have a fixed size, but you can always create another array.
Consider this example:
You take your set of school books and Stack them on top of your desk. This type of data structure is called first in, last out because the first book put down is the last one to get used. In order to get to the books at the bottom of the pile, you need to remove the other books from the top of the stack.
Why is this useful?
As a programmer you may want to limit the field of view that your data structures give it's users. The same thing goes for a Queue, first in first out. This data structure is useful when processing a list a commands that need to be executed. You would almost never do this with a Stack because there is a chance that the first command into the stack would never get executed.
In either case, unless you are the programmer, you do not know whether the particular data structure you are using is implemented with Arrays or Linked Lists. You can always create a special class that will dynamically expand the size of its array when it gets too full by creating a new array with more space and copying the elements. Then you can simply assign the reference of the old array to the new one and voila.
Data structures are extremely important to programmers because they provide us with a different way to think about things and allow us to solve problems differently.
Wait until you get to Hash Tables and the use of that particular Data Structure will make itself extremely apparent.
-
Re: Data Structure
Thank you very much, you helped me a lot. Thank you.