Professional Documents
Culture Documents
Module2 - InfixToPostfixConversion Edit
Module2 - InfixToPostfixConversion Edit
String reversal
Postfix evaluation
Balanced Paranthesis
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
postfixVect
ab+c–d*ef+-
Example-1
3 – 9 / 3 + 77 * 3 -1
Postfix 3 9 3 / - 77 3 * + 1 -
19
Example-2
Convert the infix notation to its equivalent
postfix and Evaluate:
(8/2) – 6/3 + 7-2 * (4%3)+ 3 -1
= 1 – 3 + ) 3%4( * 2 – 7 + 3/6 - ) 2/8(
= 1-3+(3%4) *2-7+3/6-(2/8)
=1334%2*736/28/-+-+-
Post-fix 8 2 / 6 3 / - 7 + 2 4 3 % * - 3 + 1 -
Pre-fix -+-+-/82/637*2%4331
Evaluated Solution 9 20
Infix to prefix conversion
Expression = (A+B^C)*D+E^5
= A+(B^C) *D+E^5
= (A+(^BC)) *D+E^5
= (+A^BC) *D+(E^5)
= (* +A^BC D) + (^E5)
= +* +A^BC D^E5
Stack Appln–Balancing Symbols
The common mistake made frequently by
programmers is incorrect closing or missing symbols
such as parenthesis. This causes the compiler to
report lines of error. A program that checks whether
the symbols are balanced would be much useful. For
example the sequence [{ }, [ ], ( ) ] is legal, but [ ( ] ) is
incorrect. The correct representation of arithmetic
expressions includes
1.There must be equal number of opening and closing
symbols
2.The left opening symbol must be balanced with the
right closing symbol
Stack Appln–Balancing Symbols
A stack can be used to find whether the expression
contains balanced symbols as follows:
1.Make an empty stack
2.Read characters until end of string
3.If the character is an opening symbol, push it onto
the stack
4.If the character is a closed symbol, then pop the top
most symbol from stack and verify if it is matching
symbol then proceed reading next character else
report an error.
5.At end of file, if the stack is not an empty report an
error.
Stack–Balancing Symbols Example
Consider the correct expression: {p + (q – [m + n] ) }#
Push ( ‘{’ ) Push ( ‘(’ ) Push ( ‘[’ ) Pop ( ‘]’ ) Pop ( ‘)’ ) Pop ( ‘}’ )
[
( ( (
{ { { { {
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( );
} }
27
//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( );
getch(); {
} x=n*fact(n-1
return(x);
}
}
28
int main() Example
{
return 0;
} /* Function to print reverse of
the passed string */