0% found this document useful (0 votes)
3 views22 pages

Stack

The document provides an overview of stack data structures, including their properties, operations (push, pop, peek), and applications such as recursion and depth-first search. It also discusses infix, postfix, and prefix notations, along with conversion methods and evaluation examples. The content is aimed at explaining the concepts and operations related to stacks and expression conversions in computer science.

Uploaded by

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

Stack

The document provides an overview of stack data structures, including their properties, operations (push, pop, peek), and applications such as recursion and depth-first search. It also discusses infix, postfix, and prefix notations, along with conversion methods and evaluation examples. The content is aimed at explaining the concepts and operations related to stacks and expression conversions in computer science.

Uploaded by

erkriti.mittal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Dr.

Supriyo Mandal,
Data Structures and Ph.D. (IIT Patna)
Algorithms Postdoc (ZBW, University of Kiel, Germany)
Stack
Stack

• One side open another side closed


• Works on LIFO (Last in First Out) or FILO (First in Last Out)
• Top is a variable which contains the position of the Top Most
element in the stack.
Stack Creation: Using Array
Stack Operations: Traversal
Stack Operation: Push (insertion)
Stack Operation: Pop (Deletion)
Stack Operation: Peek (access top element)
Stack Operation................

[Link]
compiler/02k8ztbhvfYEG
Applications of Stack

• Recursion
• Infix to Postfix
• Prefix to postfix
• Tower of Hanoi
• Fibonacci Series
• Depth First Search (DFS)
Infix, Postfix, Prefix

a+b : infix
ab+ : postfix Symbol Priority Associativity
+ab : prefix +, - 1 L-R
*, / 2 L-R
2+3*4 infix ^ 3 R-L
234*+ postfix () higher L-R
+2*34 prefix

a+b*c infix
abc*+ postfix
Infix, Postfix, Prefix

2+3*4/6-2^1^2*2/2 INFIX
POSTFIX
PREFIX
Symbol Priority Associativity
+, - 1 L-R
*, / 2 L-R
^ 3 R-L
() higher L-R
Infix to Postfix Conversion
Problem 1:
Infix: a+b*c
Postfix:?????

Problem 2:
Infix: a+b-c
Postfix:?????

Problem 3:
Infix: a + b * c / d ^ e ^ f * g – h ^ i
Postfix:?????
Problem 1:
Infix: (a+b)*c
Postfix:?????

Problem 2:
Infix: a+(b-c)
Postfix:?????

Problem 3:
Infix: a + b * (c / (d ^ e)) ^ ((f * g) – h) ^ i
Postfix:?????
Infix to Postfix Conversion

Symbol Priority Associativity Input TOS operation


+, - 1 L-R H L PUSH
*, / 2 L-R L H POP
^ 3 R-L E E PUSH (R-L)
() higher L-R E E POP (L-R)
( opt PUSH
a+b*c opt ( PUSH
a*b+c ( ( PUSH
a+b-c ( ) POP until reaches to (
a^b^c
a*(b+c)
( ( a + b) * c )
Infix to Postfix Conversion
Step 1: Scan the infix expression from left to right.
Step 2: If the scanned character is an operand, put it in the postfix expression.
Step 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.)
§ If the scanned character is a ‘(‘, push it to the stack.
§ If the scanned character is a ‘)’, pop the stack and output it until a ‘(‘ is encountered, and discard both the parenthesis.
§ Repeat steps 2-5 until the infix expression is scanned.
§ Once the scanning is over, Pop the stack and add the operators in the postfix expression until it is not empty.
Infix to Postfix Conversion

Infix Expression:
A+ (B*C-(D/E^F)*G)*H
Postfix: ??
Infix to Postfix Conversion
Infix: a + b * (c / (d ^ e)) ^ ((f * g) – h) ^ i
Postfix: ?????

Infix: A + ( B * C - ( D / E ^ F ) * G ) * H
Postfix : ??????
Infix to Prefix Conversion

input is in infix
make it reverse : assume it is your infix expression
convert it into postfix [follow infix to postfix conversion algo]
make it reverse again and we will get output which is in prefix
Postfix Evaluation

10 5 + 60 6 / * 8 -

2 3 4 *6/+212^^2*2/-
Prefix Evaluation

+ + 2 6 + - 13 2 4 Ans : 23

You might also like