DSA Week 5 and 6
DSA Week 5 and 6
Stacks
A stack is a linear data structure where elements are stored in the LIFO (Last In First Out)
principle where the last element inserted would be the first element to be deleted.
Read more on:
https://www.javatpoint.com/data-structure-stack,
https://www.tutorialspoint.com/data_structures_algorithms/stack_algorithm.htm
Stack Operations
push(): When we insert an element in a stack then the operation is known as a push. If the stack
is full then the overflow condition occurs.
pop(): When we delete an element from the stack, the operation is known as a pop. If the stack
is empty means that no element exists in the stack, this state is known as an underflow state.
isEmpty(): It determines whether the stack is empty or not.
isFull(): It determines whether the stack is full or not.'
peek(): It returns the element at the given position.
count(): It returns the total number of elements available in a stack.
display(): It prints all the elements available in the stack.
Search(): it searches a value in the stack.
Implementing Stacks Using C++ (there are some logical and syntax errors in the code,
simple copy pasting won’t work, you have to analyze the code)
1 – Initializing the stack array
Question related to stacks
Balanced Parentheses: Given a string of parentheses, brackets, and braces, determine whether they
are balanced. This is a classic problem that can be solved using a stack.
Minimum Element in Stack: Design a stack that supports push, pop, and retrieving the minimum
element in constant time. We need two stacks, one stack for storing the values, the second stack
only stores the minimum values.
It can be achieved using a variable to track the minimum value during push and pop operations is a
valid and efficient approach, and it provides constant time complexity for finding the minimum.
Another approach can be to use additional stack and allows you to maintain the minimum value
with each operation in constant time
Yes, when a recursive function is called, each invocation of the function creates a new frame on the
call stack. These frames store information about the function's local variables, parameters, and the
point in the code where the function was called. Each recursive call adds a new frame to the stack.
Week 6
Infix, Prefix and Postfix notations
Source: https://www.codingninjas.com/studio/library/expression-evaluation-using-stack
The way to write arithmetic expression is known as a notation. An arithmetic expression can be
written in three different but equivalent notations, i.e., without changing the essence or output of an
expression. These notations are:
Infix
Prefix
Postfix
Infix notations are normal notations, that are used by us while write different mathematical
expressions. For example, (a + b – c)
Prefix Notation
In this notation, operator is prefixed to operands, i.e. operator is written ahead of operands. For
example, +ab. This is equivalent to its infix notation a + b. Prefix notation is also known as Polish
Notation.
Postfix Notation
This notation style is known as Reversed Polish Notation. In this notation style, the operator is
postfixed to the operands i.e., the operator is written after the operands. For example, ab+. This is
equivalent to its infix notation a + b.
2 5 3 6 + * * 15 / 2 –
2 5 9 * * 15 / 2 –
2 45 * 15 / 2 –
90 15 / 2 –
62–
4
Solving the postfix notation using stacks (left to right)
Prefix expression: - / * 2 * 5 + 3 6 5 2
-/*2*5+3652
-/*2*5952
- / * 2 45 5 2
- / 90 5 2
- 18 2
16
Initializing the stack
Step 2: Scan +, which is an operator. As the stack is empty, push + onto the stack.
1. Input: B * C / E - F
2. Stack: +
3. Postfix: A
Step 4: Scan *, which is an operator. The operator on the top of the stack is +, and * has higher
precedence than +. So, push * onto the stack.
1. Input: C / E - F
2. Stack: + *
3. Postfix: A B
Step 6: Scan /, which is an operator. The operator on the top of the stack is *, and / has the same
precedence as *. So, pop * from the stack and add it to the postfix expression. Then, push / onto the
stack.
1. Input: E - F
2. Stack: + /
3. Postfix: A B C *
Step 8: Scan -, which is an operator. The operator on the top of the stack is /, and - has lower
precedence than /. So, pop / and + from the stack and add them to the postfix expression. Then,
push - onto the stack.
1. Input: F
2. Stack: -
3. Postfix: A B C * E / +
Step 9: Scan F, which is an operand. Add it to the postfix expression.
1. Input: Empty
2. Stack: -
3. Postfix: A B C * E / + F
Step 10: The input is empty. Pop all the remaining operators from the stack and add them to the
postfix expression.
1. Input: Empty
2. Stack: Empty
3. Postfix: A B C * E / + F –
Step 1: Scan (, which is an operator. As the stack is empty, push ( onto the stack.
1. Input: a + b) * c
2. Stack: (
3. Postfix: Empty
Step 3: Scan +, which is an operator. As the operator on the top of the stack is (, push + onto the
stack.
1. Input: b) * c
2. Stack: ( +
3. Postfix: a
Step 5: Scan ), which is a closing parenthesis. Pop operators from the stack and add them to the
postfix expression until a matching opening parenthesis is found. Pop and discard the opening
parenthesis.
1. Input: * c
2. Stack: Empty
3. Postfix: a b +
Step 6: Scan *, which is an operator. As the stack is empty, push * onto the stack.
1. Input: c
2. Stack: *
3. Postfix: a b +
Step 8: The input is empty. Pop all the remaining operators from the stack and add them to the
postfix expression.
1. Input: Empty
2. Stack: Empty
3. Postfix: a b + c *
Step 2: Scan /, which is an operator. As the stack is empty, push / onto the stack.
1. Input: b + c / d
2. Stack: /
3. Postfix: a
Step 4: Scan +, which is an operator. The operator on the top of the stack is /, and + has lower
precedence than /. So, pop / from the stack and add it to the postfix expression. Then, push + onto
the stack.
1. Input: c / d
2. Stack: +
3. Postfix: a b /
Step 6: Scan /, which is an operator. The operator on the top of the stack is +, and / has higher
precedence than +. So, push / onto the stack.
1. Input: d
2. Stack: + /
3. Postfix: a b / c
Step 8: The input is empty. Pop all the remaining operators from the stack and add them to the
postfix expression.
1. Input: Empty
2. Stack: Empty
3. Postfix: a b / c d / +
Step 2: Scan -, which is an operator. As the stack is empty, push - onto the stack.
1. Input: ((a + b) * c) - d
2. Stack: -
3. Prefix: d
Step 5: Scan *, which is an operator. As the operator on the top of the stack is ), push * onto the
stack.
1. Input: ((a + b) * c) - d
2. Stack: - ) *
3. Prefix: d c
Step 8: Scan +, which is an operator. As the operator on the top of the stack is ), push + onto the
stack.
1. Input: ((a + b) * c) - d
2. Stack: - ) * ) +
3. Prefix: d c b
Step 10: Scan (, which is an opening parenthesis. Pop operators from the stack and add them to the
prefix expression until a matching closing parenthesis is found. Pop and discard the closing
parenthesis.
1. Input: ((a + b) * c) - d
2. tack: - ) *
3. Prefix: d c b a +
Step 11: Scan (, which is an opening parenthesis. Pop operators from the stack and add them to the
prefix expression until a matching closing parenthesis is found. Pop and discard the closing
parenthesis.
1. Input: ((a + b) * c) - d
2. Stack: -
3. Prefix: d c b a + *
Step 12: The input is empty. Pop all the remaining operators from the stack and add them to the
prefix expression.
1. Input: Empty
2. Stack: Empty
3. Prefix: d c b a + * -
Step 2: Scan *, which is an operator. As the stack is empty, push * onto the stack.
Input: (a + b) * c
Stack: *
Prefix: c
Step 5: Scan +, which is an operator. As the operator on the top of the stack is ), push + onto the
stack.
Input: (a + b) * c
Stack: * ) +
Prefix: c b
Step 7: Scan (, which is an opening parenthesis. Pop operators from the stack and add them to the
prefix expression until a matching closing parenthesis is found. Pop and discard the closing
parenthesis.
Input: (a + b) * c
Stack: *
Prefix: c b a +
Step 8: The input is empty. Pop all the remaining operators from the stack and add them to the
prefix expression.
Input: Empty
Stack: Empty
Prefix: c b a + *