0% found this document useful (0 votes)
11 views7 pages

Stack Application 1

Uploaded by

mahantasuman8908
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views7 pages

Stack Application 1

Uploaded by

mahantasuman8908
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Applications of Stacks:

Function calls: Stacks are used to keep track of the return addresses of function calls, allowing
the program to return to the correct location after a function has finished executing.
Recursion: Stacks are used to store the local variables and return addresses of recursive function
calls, allowing the program to keep track of the current state of the recursion.
Expression evaluation: Stacks are used to evaluate expressions in postfix notation (Reverse
Polish Notation).
Syntax parsing: Stacks are used to check the validity of syntax in programming languages and
other formal languages.
Memory management: Stacks are used to allocate and manage memory in some operating
systems and programming languages.
Advantages of Stacks:
Simplicity: Stacks are a simple and easy-to-understand data structure, making them suitable for a wide range
of applications.
Efficiency: Push and pop operations on a stack can be performed in constant time (O(1)), providing efficient
access to data.
Last-in, First-out (LIFO): Stacks follow the LIFO principle, ensuring that the last element added to the stack is
the first one removed. This behavior is useful in many scenarios, such as function calls and expression
evaluation.
Limited memory usage: Stacks only need to store the elements that have been pushed onto them, making
them memory-efficient compared to other data structures.
Disadvantages of Stacks:
Limited access: Elements in a stack can only be accessed from the top, making it difficult to retrieve or
modify elements in the middle of the stack.
Potential for overflow: If more elements are pushed onto a stack than it can hold, an overflow error will
occur, resulting in a loss of data.
Not suitable for random access: Stacks do not allow for random access to elements, making them
unsuitable for applications where elements need to be accessed in a specific order.
Limited capacity: Stacks have a fixed capacity, which can be a limitation if the number of elements that
need to be stored is unknown or highly variable.
Convert Infix expression to Postfix expression

Write a program to convert an Infix expression to Postfix form.

Infix expression: The expression of the form “a operator b” (a + b) i.e., when an operator is in-between
every pair of operands.
Postfix expression: The expression of the form “a b operator” (ab+) i.e., When every pair of operands is
followed by an operator.

Examples:

Input: A + B * C + D
Output: ABC*+D+

Input: ((A + B) – C * (D / E)) + F


Output: AB+CDE/*-F+
Why postfix representation of the expression?
The compiler scans the expression either from left to right or from right to left.
Consider the expression: a + b * c + d

The compiler first scans the expression to evaluate the expression b * c, then again scans the expression to
add a to it.
The result is then added to d after another scan.
The repeated scanning makes it very inefficient. Infix expressions are easily readable and solvable by
humans whereas the computer cannot differentiate the operators and parenthesis easily so, it is better to
convert the expression to postfix(or prefix) form before evaluation.

The corresponding expression in postfix form is abc*+d+. The postfix expressions can be evaluated easily
using a stack.
How to convert an Infix expression to a Postfix expression?
Below are the steps to implement the above idea:

1. Scan the infix expression from left to right.


2. If the scanned character is an operand, put it in the postfix expression.
3. Otherwise, do the following
• If the precedence and associativity of the scanned operator are greater than the precedence and
associativity of the operator in the stack [or the stack is empty or the stack contains a ‘(‘ ], then
push it in the stack. [‘^‘ operator is right associative and other operators like ‘+‘,’–‘,’*‘ and ‘/‘ are left-
associative].
• Check especially for a condition when the operator at the top of the stack and the
scanned operator both are ‘^‘. In this condition, the precedence of the scanned operator
is higher due to its right associativity. So it will be pushed into the operator stack.
• In all the other cases when the top of the operator stack is the same as the scanned
operator, then pop the operator from the stack because of left associativity due to which
the scanned operator has less precedence.
• Else, Pop all the operators from the stack which are greater than or equal to in precedence than that
of the scanned operator.
• After doing that Push the scanned operator to the stack. (If you encounter parenthesis
while popping then stop there and push the scanned operator in the stack.)
4. If the scanned character is a ‘(‘, push it to the stack.
5. If the scanned character is a ‘)’, pop the stack and output it until a ‘(‘ is
encountered, and discard both the parenthesis.
6. Repeat steps 2-5 until the infix expression is scanned.
7. Once the scanning is over, Pop the stack and add the operators in the postfix
expression until it is not empty.
8. Finally, print the postfix expression.

You might also like