Page 74 - DCAP605_ADVANCED_DATA_STRUCTURE_AND_ALGORITHMS
P. 74

Unit 3: Stacks





          }                                                                                     Notes
          char stackTop(stk *stack)  /* return the top value of the stack without popping
          the stack */
          {
               if ( !(isEmpty(stack)) )
                    return stack->data[stack->top];
               else
                    return ‘\0’;
          }
          int isEmpty(stk *stack)      /* determine if stack is empty */
          {
               if ( stack->top == -1 )        /* empty */
                    return 1;
               else             /* not empty */
                    return 0;
          }
          int isFull(stk *stack)    /* determine if stack is full */
          {
               if ( stack->top == 19 )        /* full */
                    return 1;
               else             /* not full */
                    return 0;
          }
          void printResult(char infix[], char postfix[]) /* display the result postfix
          expression */
          {
               printf(“\n\n”);                /*system(“cls”);*/
               printf(“Infix notation  : %s\n”, infix);
               printf(“Postfix notation: %s\n\n”, postfix);
          }
          Here are some results of the test run.

                     Infi x                        Postfi x
                     (1+2)-(3*4/5)+6              12+34*5/-6+
                     1-2*3/(4-5)                  123*45-/-
                     (1*2^3)-(4%3-5)^6            123^*43%5-6^-
          Evaluating Postfi x Expression

          An expression may be evaluated by making a left to right scan, stacking operands and evaluating

          operators using the correct number of operands from the stack and finally placing back the result
          on to the stack.







                                           LOVELY PROFESSIONAL UNIVERSITY                                    69
   69   70   71   72   73   74   75   76   77   78   79