2. Contents
1. Introduction to Data Structures
2. Types and Concepts of Arrays
3. Stack
3.1 Concepts and Representation
3.2 Stack operations
3.3 Stack implementation using array
3.4 Applications of Stack.
2
3. 3
Stack
Definitions
1. A stack is a collection of objects that are inserted and removed
according to the last-in, first-out (LIFO) principle. [T1]
2. A stack is an ordered list in which insertion and deletion are done
at one end, called top. The last element inserted is the first one to be
deleted. Hence, it is called the Last in First out(LIFO) or First in Last
out (FILO) list. [T2]
4. 4
Applications of Stack.
• Balancing of symbols
• Infix-to-postfix Conversion- Lab
• Evaluation of postfix expression - Lab
• Implementing function calls (including recursion)
• Page-visited history in a Web browser [Back Buttons]
• Undo sequence in a text editor
• Matching Tags in HTML and XML
5. Algebraic Expressions
• Infix expressions
• An operator appears between its operands
• Example: a + b
• Prefix expressions
• An operator appears before its operands
• Example: + a b
• Postfix expressions
• An operator appears after its operands
• Example: a b +
6. A complicated example
• Infix expressions:
a + b * c + ( d * e + f ) * g
• Postfix expressions:
a b c * + d e * f + g * +
• Prefix expressions:
+ + a * b c * + * d e f g
7. 7
Table : the operator precedence rules for common
mathematical operators
8. 8
Infix-to-postfix Conversion
Infix Expression
Definition: An infix expression is a mathematical notation where the
operators are placed between the operands.
This is the most common and familiar notation, especially in
arithmetic expressions.
Example:
A + B
(A + B) * C
9. 9
Infix-to-postfix Conversion
Postfix Expression (Reverse Polish Notation)
Definition: A postfix expression is a mathematical notation where the
operators are placed after their operands.
There are no parentheses needed, and the order of operations is
explicitly defined by the operators’ positions.
Example:
A B +
A B C * +
10. 10
Infix-to-postfix Conversion
Prefix Expression (Polish Notation)
Definition: A prefix expression is a mathematical notation where the
operators are placed before their operands.
Like postfix expressions, there are no parentheses needed, and the
position of the operators dictates the order of operations.
Example:
+ A B
* + A B C
11. 11
Algorithm: Infix to Postfix Conversion
1. Initialize an empty stack for operators and an empty list for the output (postfix expression).
2. Iterate over each character in the infix expression:
•Operand (e.g., numbers or variables): Directly append it to the postfix list.
•Left parenthesis ( Push it onto the stack. Right parenthesis ): Pop from the stack to the postfix
list until a left parenthesis ( is encountered on the stack. Discard the left parenthesis.
•Operator (e.g., +, -, *, /):
While the stack is not empty and the operator at the top of the stack has greater or equal
precedence than the current operator, pop the operator from the stack to the postfix list.
Push the current operator onto the stack.
• After iterating over the expression, pop all remaining operators from the stack to the postfix
list.
• The postfix list now contains the postfix expression.
18. 18
Example 1: Infix to Postfix Conversion
Input Current
Symbol
Type Operation Stack Output
A + B * C + D A Operand Print Empty A
A + B * C + D + Operator Push(+) + A
A + B * C + D B Operand Print + AB
A + B * C + D * Operator P(*)>P(stack[TOP]) so Push(*) +* AB
A + B * C + D C Operand Print +* ABC
A + B * C + D + Operator P(+)<=P(stack[TOP]) so Pop() and Print
P(+)<=P(stack[TOP]) so Pop() and Print
+
+
ABC*
ABC*+
A + B * C + D D Operand Print + ABC*+D
Completed 0 - POP() all the symbols from stack and
print
Empty ABC*+D+
19. 19
Example 2: Infix to Postfix Conversion
Input Current
Symbol
Type Operation Stack Output
((A + B) – C * (D / E)) + F ( LP Push( ‘(‘ ) (
((A + B) – C * (D / E)) + F ( LP Push( ‘(‘ ) ((
((A + B) – C * (D / E)) + F A Operand Print A (( A
((A + B) – C * (D / E)) + F + Operator Push (+) ((+ A
((A + B) – C * (D / E)) + F B Operand Print B ((+ AB
((A + B) – C * (D / E)) + F ) RP Pop all symbols and print until we get ‘(‘
and discard ‘(‘
Pop(+)
( AB+
((A + B) – C * (D / E)) + F - Operator P(-)>P(stack[TOP]) so push(-) (- AB+
((A + B) – C * (D / E)) + F C Operand Print C (- AB+C
20. 20
Example 2: Infix to Postfix Conversion
Input Current
Symbol
Type Operation Stack Output
((A + B) – C * (D / E)) + F C Operand Print C (- AB+C
((A + B) – C * (D / E)) + F * Operator P(*)>P(stack[TOP]) so push(*) (-* AB+C
((A + B) – C * (D / E)) + F ( LP Push (‘(‘) (-*( AB+C
((A + B) – C * (D / E)) + F D Operand Print D (-*( AB+CD
((A + B) – C * (D / E)) + F / Operator P(/)>P(stack[TOP]) so push(/) (-*(/ AB+CD
((A + B) – C * (D / E)) + F E Operand Print E (-*(/ AB+CDE
((A + B) – C * (D / E)) + F ) RP Pop all symbols and print until we get ‘(‘
and discard ‘(‘ - Pop(/)
(-* AB+CDE/
21. 21
Example 2: Infix to Postfix Conversion
Input Current
Symbol
Type Operation Stack Output
((A + B) – C * (D / E)) + F ) RP Pop all symbols and print until we get ‘(‘
and discard ‘(‘
Pop(*) and Print
Pop(-) and Print
Discard ‘(‘
(-*
(-
(
Empty
AB+CDE/
AB+CDE/*
AB+CDE/*-
AB+CDE/*-
((A + B) – C * (D / E)) + F + Operator Push(+) + AB+CDE/*-
((A + B) – C * (D / E)) + F F Operand Print F + AB+CDE/*-F
Completed /0 - Pop(+) and Print Empty AB+CDE/*-F+
24. 24
Evaluation of Postfix Expression
• Postfix expression: The expression of the form “a b operator” (ab+)
i.e., when a pair of operands is followed by an operator.
• Input: str = “2 3 1 * + 9 -“
• To evaluate a postfix expression we can use a stack.
• Iterate the expression from left to right and keep on storing the
operands into a stack. Once an operator is received, pop the two
topmost elements and evaluate them and push the result in the stack
again.
25. 25
Follow the steps mentioned below to evaluate
postfix expression using stack:
1. Create a stack to store operands (or values).
2. Scan the given expression from left to right and do the following for
every scanned element.
3. If the element is a number, push it into the stack.
4. If the element is an operator, pop operands for the operator from
the stack. Evaluate the operator and push the result back to the
stack.
5. When the expression is ended, the number in the stack is the final
answer.