Page 71 - DCAP605_ADVANCED_DATA_STRUCTURE_AND_ALGORITHMS
P. 71

Advanced Data Structure and Algorithms




                    Notes               char top_ch;

                                        stk stack;
                                        initStack(&stack);             /* initialise stack */
                                        get_infix(infix);              /* get infix expression from user */
                                        length = strlen(infix);
                                        if ( length )          /* if strlen if infix is more than zero */
                                        {
                                             push(&stack, ‘(‘);
                                             strcat(infix, “)”);
                                             length++;
                                             for ( i=0; i<length; i++ )
                                             {
                                              if ( isdigit(infix[i]) ) /* if current operator in infix is digit */
                                                  {
                                                       postfix[j++] = infix[i];
                                                  }
                                                  else if ( infix[i] == ‘(‘ )   /* if current operator in infix is
                                   left  parenthesis */
                                               {
                                                     push(&stack, ‘(‘);
                                               }
                                               else if ( isOperator(infix[i]) ) /* if current operator is operator */
                                                  {
                                                       while(1)
                                                       {
                                                            top_ch = stackTop(&stack);      /* get tos */
                                                 if ( top_ch == ‘\0’ )               /* no stack left */
                                                            {
                                                                 printf(“\nInvalid infix expression\n”);
                                                                 exit(1);
                                                            }
                                                            else
                                                            {
                                                               if ( isOperator(top_ch) )
                                                               {
                                                                 if ( pred_level(top_ch) >= pred_level(infix[i]) )
                                                                           postfix[j++] = pop(&stack);
                                                                      else
                                                                           break;
                                                                 }
                                                                 else



          66                               LOVELY PROFESSIONAL UNIVERSITY
   66   67   68   69   70   71   72   73   74   75   76