Task 1:
Reversing an array of numbers.
1) Initialize start indexes as top and a num_array
2) Now we will use for loop for num array starting from top
until this condition num_array >=0.
3) As we are reversing the array by using so we will use push
and pop operation.
4) Push operation is to add integer into the stack.
5) And pop operation is to remove an integer or element from
stack.
6) First we will ask the user to enter numbers, then we will
print them in reverse order.
Task 2:
Testing if a mathematical expression is balanced or
not.
1. Take a expression as input and store it in the array.
2. Check for the “(” and “)” in the expression.
3. If “(” encounters, then push it to the separate array. If “)”
encounters, then pop the element of the array.
4. If the number of “(” and “)” are equal, then the expression is
correctly parenthesized. Otherwise it is not.
5. In this way this program will check expression for “[“ and
“{“.
6. If the expression is balanced it will return 1 otherwise 0.
Post lab:
Infix to postfix conversion
Infix expression is which <operator> is preceded and succeeded by an
<operand>. E.g., A+B
Postfix expression is which <operator> is succeeded by both the
<operand>. E.g., AB+
1. Let, src is an arithmetic expression written in infix notation. This
algorithm finds the equivalent postfix expression dst.
2. Push “(“onto Stack, and add “)” to the end of src.
3. Scan src from left to right and repeat Step 3 to 6 for each element of
src until the Stack is empty.
4. If an operand is encountered, add it to dst.
5. If a left parenthesis is encountered, push it onto Stack.
6. If an operator is encountered ,then:Repeatedly pop from Stack and
add to dst each operator (on the top of Stack) which has the same
precedence as or higher precedence than operator.
7. Add operator to Stack.
[End of If]
8. If a right parenthesis is encountered ,then: Repeatedly pop from
Stack and add to dst each operator (on the top of Stack) until a left
parenthesis is encountered.
9. Remove the left Parenthesis.