Page 97 - DCAP605_ADVANCED_DATA_STRUCTURE_AND_ALGORITHMS
P. 97

Advanced Data Structure and Algorithms




                    Notes
                                              }
                                              printf( “\tS pushing %c...\n”, op );
                                              sPush( s, op );
                                              break;
                                           }
                                        }
                                     }
                                     bool isop( char op ) {
                                         /*
                                          * is op an operator?
                                          */
                                         return (getIndex(op) != -1);
                                     }
                                     char *in2pre( char *str ) { /*
                                          * returns valid infix expr in str to prefix.
                                          */
                                         char *sptr;
                                         queue q = {NULL};
                                         stack s = NULL;
                                         char *res = (char *)malloc( N*sizeof(char) );
                                         char *resptr = res;
                                         tptr = t;
                                         for( sptr=str+strlen(str)-1; sptr!=str-1; -sptr ) {
                                            printf( “processing %c tptr-t=%d...\n”, *sptr, tptr-t );
                                            if( isalpha(*sptr) ) // if operand.
                                                   qPush( &q, *sptr );
                                            else if( isop(*sptr) )      // if valid operator.
                                                   processOp( *sptr, &q, &s );
                                            else if( isspace(*sptr) )    // if whitespace.
                                                   ;
                                            else {
                                                   fprintf( stderr, “ERROR:invalid char %c.\n”, *sptr );
                                                   return “”;
                                            }
                                        }
                                        while( !qEmpty(&q) ) {
                                            *tptr++ = qPop(&q);
                                            printf( “\tQ popping %c...\n”, *(tptr-1) );
                                        }
                                        while( !sEmpty(&s) ) {
                                            *tptr++ = sPop(&s);
                                            printf( “\tS popping %c...\n”, *(tptr-1) );
                                                                                                         Contd...
                                        }



          92                               LOVELY PROFESSIONAL UNIVERSITY
   92   93   94   95   96   97   98   99   100   101   102