The definition given in this Textbook says that
A data structure is a way to store and organize data in order to facilitate access and modifications.
Again, highlighting key words:
A data structure is a way to store and organize data in order to facilitate access and modifications.
Now, analogous to real world, if we want to store something, there are multiple ways.
Example : We want to store our Office Uniforms
One way is to pile them up, one on top of other. So, you come, keep your uniform, and at next day, pick the topmost uniform, as it is easy to pick.
What will happen? You essentially end up wearing same clothes again and again.
Another way is to use Hangers, and Closet Rod. Assuming you have 7 clothes, and Office is 7-days working. So, you come, keep your uniform at one end of closet, and next day, pick clothes from another end.
What will happen? You will realize that after 7 days, you will have to wear same clothes again, but at least for tomorrow, you will have new uniform.
So, we essentially have Stored Data, and in fact organized data in proper form. Which is Best? Well, it depends. Piling up is convenient, but Hanger One is Better for Choices. Note that even in pile up, or hanger we might want to access any clothes in between the pile. And may want to modify, maybe wash or iron that clothes.
Thus, when you are storing something, you should be able to access them.
In computer, we store data at different memory locations, if definition was limited to organized data or structured data, we missed out important point that we should be able to access data. In every data structure, you should be able to access each and every data, else storing them would be of not use.
By accessing, we mean that sufficient information and techniques should be there to get each and every data.
- Array : We should know actual length of array, and starting address, we then can traverse linearly
- Linked List : We should know address of head node, we then can traverse linearly
- Tree : We should know address of Root Node, we then can traverse in Pre-Order, Post-Order or In-Order. These techniques guarantee that each and every element will be accessed.
Thus, accessing itself means sufficient techniques and information are there. Moreover, this property of accessing should be preserved even after applying operations.
Example : Linked List Insertion
Here, we have three steps.
- Establish link : 2 to New
- Establish link : New to 3
- Breaking link : 2 to 3
An observation will help us know that Establish link : 2 to New
will automatically do Breaking link : 2 to 3
Thus, we have 2 steps only, this can be done in 2!
ways i. e. 2
ways:
WAY1:
- Establish link : 2 to New
- Establish link : New to 3
Is this correct?
No!
Why?
While establishing link 2 to New, we have broken link 2 to 3. Now, we can't establish link New to 3, as we have lose access to 3. Initially, 2 has access of address of 3. But, now we don't have.
What's the correct way then?
WAY2:
- Establish link : New to 3
- Establish link : 2 to New
We can get address of 3, as it is pointed by 2, and we have broken link later on.
Thus, you can see that accessing is important part of Data Structures Definition. There are Multiple way to do certain operation on Data Structures, this access helps us in putting restrictions on some of these way.
After doing any operation, data structure should remain a data structure. If definition was limited to organized data or structured data, then after doing Way 1, definition of data structure was not violated. But definition get violated in Way 1, if we add access part in definition, and we came to know that this way isn't correct!