Peek() Applications!
- Guided By Mrs. Sampada Kulkarni
This time let us continue the journey of exploring more into them and finding some new claim.
1. Stack :
Here Firstly as mentioned in the first blog , Stack is Linear & Static data structure. A Stack uses a basic principle of LAST-IN FIRST OUT (LIFO). It can be implemented using both the Array and Linked-List. There are two types of Stack :
a. Implicit Stack :
An Implicit Stack is used by the system to make the process of compilation, debugging , Storage Management, CPU processing and many more…
One not in these is → use of stack in bash scripting. An very basic need of stack in bash scripting.
Firstly let us see the problem first arise , which lead to this type of usage.
There is one Folder named “test” and another “dsatest” in my device.
NOTE : dsatest is inside the folder
ls which is in DSA folder. (i.e. DSA/ls/dsatest)
Now let us understand the commands I used here :
Commands |
Description |
ls |
Showing the contents of the current directory. |
cd |
Change the current directory to the home directory. |
cd <PATH> |
Change the current directory to the specified path. |
cd - |
Change the current directory to the previous directory. |
Now the problem statement is ; “cd -” command only revert back to the previous but not the history of the commands we used previously.
This problem is solved using stack very easily ( i.e. LIFO principle).
There are two more commands used for
implicit Stack :
Commands |
Description |
pushd |
Adds the path to stack and change the directory to the pushed path. |
popd |
Removes the path from the stack and change the directory to the popped path. |
If there is no path in the stack then
it shows an error : stack is empty. Hence Solved.
One more application is → Arithmetic and Logical Unit (ALU ) using stack for Evaluation and Conversion of the Expression as given below. It uses Implicit stack which we can implement explicitly.
b. Explicit Stack :
Explicit Stack is when the programmer itself create a structure using linked list or an array implementing stack.
This stack can be created either in any type of programming language or explicitly as a structure for the theoretical concepts.
eg. in C++ programming language.
The above example shows that stack implemented using Linked-List having :
Data in the type of character and next field as the pointer to the next element in the linked-list
Let us take and example of Converting infix expression A * ( B * C + D *E ) + F
image courtesy by : https://www.techiedelight.com/wp-content/uploads/Infix-To-Postfix.png
Similarly is done with the Conversion of Infix to Prefix
Now the expression is A + B — C * D + (E^F)
Hence the Conversion is as below :
image courtesy by : https://miro.medium.com/v2/resize:fit:720/format:webp/1*_Intvr_huZXax2C4alO9hg.png
Now reverse it ..
image courtesy by : https://miro.medium.com/v2/resize:fit:640/format:webp/1*WAqUdBXblc0ZAc0Of4iSWg.png
This is how it is done By using stack.