In our previous article on relationship between data, data structures and algorithms, we concluded that there can be a number of data-structures based on the algorithm we choose to implement to solve our problem.
Please refer the mentioned article if you need to refresh your memory.
In this article, without being too much technical, I would like to make you aware of various types of data structures and more importantly, the need to have many types of data structures.
Revisiting our chai recipe
Please refer this note on a tea recipe. There can be several such recipes or procedures and the note contains just one of them.
The order of ingredients as described in that recipe is mentioned below.
- tea leaves
Let's build a container for our ingredients
Let us keep all the ingredients in the same order something like the picture below.
Let us give this container a name, say, tea-container. Now our container will look like.
To refer a particular ingredient by its position, let us number the ingredients of the container from left to right starting from 1. Quite natural, isn't it?
Now, we can say that 3rd item in Tea-Container contains Tea Leaves and 5th item is Milk.
Let's use our container
We know that Tea-Container contains ingredients in the order they are required. So, let us keep one of our hand on first item as shown below using -> sign.
When there will be a need of the item our hand is pointing to, we will use it and then put our hand on the next item like the shown below.
Similarly, we will complete our recipe if all of the ingredients are exhausted and our hand is pointing to 6th item which is not present.
Now, after a few minutes, our tea will be ready to serve.
This container served us very well and was an excellent choice because we knew that there are 5 items which we will require and we also had prior information about the order in which they will be used.
Here comes a special request from a special friend
One fine evening, a very close friend of yours visited you and asked you prepare a special chai for her with added ginger and cardamom (adrak and ilaaichi).
Believe me, you can't say no to her, can you? ;)
The task has now changed a bit and so, will our recipe and container.
Changes in Recipe and Container
There will be a small change in the recipe. I will add cardamon and ginger after the last step in our previous recipe and boil for about 2 minutes.
The real problem now is, where and how do I keep the two ingredients in the container? The tea-container we used last time has slots for 5 items only and no more items can be added to it.
So, the type of container we used last time was not good for cases when an extra ingredient can be added. Each time an item is added, we will have to replace the entire container with the new one with one more slot than the previous one. The same container may also be replaced when another ingredient is added. This will result in wastage of a number of containers and is costly at the same time.
For example, when ginger was added in the last step, we could replace our container with another container which looks something like the below.
And to fit in cardamom, we will replace the previous container as well.
The choice of container or data-structure depends a lot on the recipe, procedure or algorithm followed to perform the task. There can not be one type of container which fits all types algorithm.
Our problem is not solved yet. We will extend our discussion in another post. Till then, you can think about solution(s) or containers which can be helpful to solve the problem.