CSE 2151 Lecture 4
CSE 2151 Lecture 4
Course No.: 0714 09 CSE 2151, Course Title: Data Structures and Algorithms
Electronics and Communication Engineering Discipline, Khulna University, Khulna
Md. Farhan Sadique
Email: [email protected]
This lecture is not a study material. Use this lecture as an outline. Follow the outline to study from the mentioned sources
after each section header. The sections of this lecture have been prepared from the mentioned sources.
By convention, when the stack is empty it will have t equal to −1 (and thus has size t + 1, which is 0).
3. Applications of Stack
Arithmetic expressions that may contain various pairs of grouping symbols, such as
• Parentheses: “(” and “)”
• Braces: “{” and “}”
• Brackets: “[” and “]”
Each opening symbol must match its corresponding closing symbol.
3
Data Structures and Algorithms – Lecture 4 Md. Farhan Sadique
• Correct: ()(()){([()])}
• Correct: ((()(()){([()])}))
• Incorrect: )(()){([()])}
• Incorrect: ({[])}
• Incorrect: (
The steps of checking the validity of an arithmetic expression using stack are
Example:
4
Data Structures and Algorithms – Lecture 4 Md. Farhan Sadique
Implementation:
An arithmetic expression can be expressed in various forms such as prefix, infix or postfix.
Prefix: + A B
Infix: A + B
Postfix: A B +
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.
5
Data Structures and Algorithms – Lecture 4 Md. Farhan Sadique
Below are the steps to implement the above idea:
Scan the infix expression from left to right.
3.4. Evaluation of Postfix Expression (Md. Rafiqul Islam and M. A. Mottalib: 5.4.3, https://www.javatpoint.com/convert-
infix-to-postfix-notation)
6
Data Structures and Algorithms – Lecture 4 Md. Farhan Sadique
Example: Evaluating 5 6 2 + * 12 4 / -
Review Questions
1. What values are returned during the following series of stack operations, if executed upon an initially
empty stack? push(5), push(3), pop(), push(2), push(8), pop(), pop(), push(9), push(1), pop(), push(7),
push(6), pop(), pop(), push(4), pop(), pop().
2. Implement a method with signature transfer(S, T) that transfers all elements from stack S onto stack T , so
that the element that starts at the top of S is the first to be inserted onto T , and the element at the bottom
of S ends up at the top of T .
Bibliography
• Book: Data Structures and Algorithms in Java, Sixth Edition - Michael T. Goodrich, Roberto Tamassia and
Michael H. Goldwasser
• Book: Data Structures Fundamentals, Second Edition – Md. Rafiqul Islam and M. A. Mottalib
• Website: https://www.geeksforgeeks.org/convert-infix-expression-to-postfix-expression/
• Website: https://www.javatpoint.com/convert-infix-to-postfix-notation