This post helps in better understanding of stack data structure. Let us consider an array with the name ‘array’ of size ‘n’. You can access any element of the array by specifying the appropriate index. Visualize the picture mentally and this is how probably it should look like:
Let us now understand how an array becomes the stack. Look at the same array and apply the following conditions:
- Seal the three sides of the array by leaving the top or bottom end open (Say the top end is left open, To be more specific, we can only see what is at the top of the array)
- Imagine a index which will allow us the access of top most element of stack (Say the index is named as ‘top’)
Visualize all the conditions on the figure array. Mentally visualize the picture and it should look as following:
That is how we define stack. With this understanding, let us note down the basic properties of stack:
- We can insert a new item only on top of the stack.
- We can delete an existing item only from top of the stack.
- We can only see what is at the top of the stack.
Stack represents a class of problems where the operations happen at one end. Let us try to identify some real world examples which exhibit the same behavior as stack. Following are a few:
- Parle Poppins
- Sequence of dreams in the movie Inception
- Bangles worn in hand
- Rings put inside the well
- Disks placed one above the other in disk stand
Can you now define a stack?