Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

APPLICATIONS OF STACKS

A Stack is a data structure which serves as a collection of elements, and


performs two principal operations push (insertion) and pop (deletion).
It is basically a LIFO (“LAST IN FIRST OUT”) structure where elements are
inserted and deleted from the same end. It is a simple memory mechanism
where elements are implemented as an array or linked list.
Stacks are present in everyday life which shows real life applications. For
example - stack of books, cafeteria trays, coins, bangles, shunting of trains in
railway yards etc.

1 Evaluation Expression
Conversion of infix to postfix expression.
Computation to postfix (reverse polish) expression.
In this, the arithmetic operation which is a high-level programming language is
converted into machine language. This is because machine can understand only
binary language (0 and 1). It assumes that an arithmetic operation can take place
in two operands only like A+B, C*D, F/E etc. But the arithmetic expression
may contain more than one operator and many operands like A+(B+C-
(D/E^F)*G)*H). These complex expressions can be converted and can be
executed in two operands and an operator form.
2. Backtracking
Backtracking is an algorithm technique that tries to find each and every possible
combination of the technique and find the solution to a problem.
Suppose we are coding a chess-playing algorithm and at a certain point, the
algorithm finds that a set of steps fails to win. In this situation, the algorithm
will reverse back to the safe state and try another possible set of steps.
This "reverse back" (undo) is powered by the stack data structure which keeps
track of the steps for rollback if anything goes wrong.
3. Parenthesis Balance Checking
It is a common question during interviews to write a program to check if the
parenthesis is balanced or not.
We use the stack data structure to check unbalanced parenthesis by pushing
open brackets into the stack and pop out after a closing bracket. If nothing lasts
at the end then all parenthesis are matched.
4. Track nested function calls
Nested function calls are common things but complicated ones. The stack data
structure is used to track the order in which the functions were invoked and
return data in the same manner.
5. Undo Button
As explained in backtracking example, the stack data structure has many
applications in the Undo functionality of many applications. It keeps track on
the work and recovers the previous stage as saved in the stack.

6. Recursion
Recursion is a beautiful but dangerous (infinity) part of programming. Stack
data structure is used to keep track of recursive calls and return data in a
particular order.

7. Reverse String or number


You can use stack when you want to get them in the reverse order as you put
them.
Example to reverse a string, we want to reverse "coder" then we will push "c",
"o", "d", "e", "r" into the stack and pop operation will take them out as "r", "e",
"d", "o", "c". Now we can combine them and we get the reverse of the string.

You might also like