Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 29

Applications of Stack

 String reversal

 Infix to postfix conversion

 Postfix evaluation

 Constructing an expression tree using stack

 Balanced Paranthesis
void reverse(char *str)
{
if(*str)
{
reverse(str+1);
printf("%c", *str);
}
}

/* Driver program to test above function */


int main()
{
char a[] = “VIT Chennai";
reverse(a);
getch();
return 0;
}
Infix to postfix conversion
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 open 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.
Infix to postfix conversion

infixVect
(a+b-c)*d–(e+f)

postfixVect
Infix to postfix conversion
stackVect

infixVect

postfixVect
ab+c–d*ef+-
Example-1

Convert the infix notation to its equivalent


postfix and Evaluate:

3 – 9 / 3 + 77 * 3 -1

Postfix  3 9 3 / - 77 3 * + 1 -

Evaluated Solution  230

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

Step 1. Reverse the infix expression.

               5^E+D*)C^B+A(

Step 2. Make Every '(' as ')' and every ')' as '('

                5^E+D*(C^B+A)

Step 3: Apply infix to postfix algorithm - 5E^DCB^A+*+

Step 4: Reverse the output of Step3 - +*+A^BCD^E5


(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)

= +* +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( );

printf("Enter the value of n:");


if(n==1)
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);
}
}

28
 int main() Example
{

   char a[] = "Geeks for Geeks";


   reverse(a);
   getchar();

   return 0;
} /* Function to print reverse of
the passed string */

void reverse(char *str)


{
   if(*str)
   {
       reverse(str+1);
       printf("%c", *str);
   }
}

You might also like