Module2 02 0.1
Module2 02 0.1
1.Read the characters one by one from left to right from the i/p
infix notation.
2.If the character is an operand append it on to output postfix
notation.
3.If the character is an operator then
(i) if(priority(top of stack) >= priority of operator read) then pop
out the top character and append it on to postfix notation.
(ii) if(priority(top of stack) < priority of operator read) then push
the operator on to stack.
4.If the character is an open bracket then push it on to stack
5.If the character is a closed bracket then repeatedly perform pop
until a corresponding closed bracket is found and then delete
the close bracket.
6.Finally, if we read the end of input, we pop the stack until it is
empty, writing symbols onto the output.
void reverse(char *str)
{
if(*str)
{
reverse(str+1);
printf("%c", *str);
}
}
infixVect
(a+b-c)*d–(e+f)
postfixVect
Infix to postfix conversion
stackVect
infixVect
a+b-c)*d–(e+f)
postfixVect
(
Infix to postfix conversion
stackVect
infixVect
+b-c)*d–(e+f)
postfixVect
a
(
Infix to postfix conversion
stackVect
infixVect
b-c)*d–(e+f)
postfixVect
a
+
(
Infix to postfix conversion
stackVect
infixVect
-c)*d–(e+f)
postfixVect
ab
+
(
Infix to postfix conversion
stackVect
infixVect
-c)*d–(e+f)
postfixVect
ab+
(
Infix to postfix conversion
stackVect
infixVect
c)*d–(e+f)
postfixVect
ab+
-
(
Infix to postfix conversion
stackVect
infixVect
)*d–(e+f)
postfixVect
ab+c
-
(
Infix to postfix conversion
stackVect
infixVect
*d–(e+f)
postfixVect
ab+c-
Infix to postfix conversion
stackVect
infixVect
d–(e+f)
postfixVect
ab+c-
*
Infix to postfix conversion
stackVect
infixVect
–(e+f)
postfixVect
ab+c-d
*
Infix to postfix conversion
stackVect
infixVect
–(e+f)
postfixVect
ab+c–d*
Infix to postfix conversion
stackVect
infixVect
(e+f)
postfixVect
ab+c–d*
-
Infix to postfix conversion
stackVect
infixVect
e+f)
postfixVect
ab+c–d*
(
-
Infix to postfix conversion
stackVect
infixVect
+f)
postfixVect
ab+c–d*e
(
-
Infix to postfix conversion
stackVect
infixVect
f)
postfixVect
+ ab+c–d*e
(
-
Infix to postfix conversion
stackVect
infixVect
)
postfixVect
+ ab+c–d*ef
(
-
Infix to postfix conversion
stackVect
infixVect
postfixVect
ab+c–d*ef+
-
Infix to postfix conversion
stackVect
infixVect
postfixVect
ab+c–d*ef+-
Example-1
3 – 9 / 3 + 77 * 3 -1
Postfix 3 9 3 / - 77 3 * + 1 -
23
Example-2
Postfix ????
24
Example-2
Postfix 8 2 / 6 3 / - 7 + 2 4 3 % * - 3 + 1 -
Evaluated Solution 9
25
Infix to prefix conversion
Expression = (A+B^C)*D+E^5
5^E+D*)C^B+A(
5^E+D*(C^B+A)
[
( ( (
{ { { { {
At the end of the expression, the stack becomes empty, therefore balanced.
Consider the following incorrect expression: ( (x + y) #
Push ( ‘(’ ) Push ( ‘(’ ) Pop ( ‘)’ )
(
( ( (
At the end of the expression, the stack is not empty, therefore unbalanced.
Recursive Function or Recursion
A Function which calls the same function
itself again and again until some condition is
satisfied is said to be Recursive function
Eg:
main( ) f1( )
{ {
f1( ); f1( );
} }
31
//Factorial using recursion
#include<stdio.h> Example
#include<conio.h>
int fact(int);
int fact(int n)
void main()
{
{
int n,z; int x;
clrscr( );
if(n==1)
printf("Enter the value of n:");
scanf("%d",&n);
return(1);
z=fact(n); else
printf("\nFactorial of %d is %d",n,z);
{
getch();
x=n*fact(n-1
}
return(x);
}
}
32
int main() Example
{
return 0;
} /* Function to print reverse of
the passed string */