Page 70 - DCAP605_ADVANCED_DATA_STRUCTURE_AND_ALGORITHMS
P. 70

Unit 3: Stacks





          char pop(stk *stack);                                                                 Notes
          char stackTop(stk *stack);
          int isEmpty(stk *stack);
          int isFull(stk *stack);
          void printResult(char infix[], char postfix[]);
          /*void print_msg(void);*/
          /* program entry point */
          int main(void)
          {
               char infix[20], postfix[20]=””;
               /* convert from infix to postfix main function */
               convertToPostfix(infix, postfix);
               /* display the postfix equivalent */
               infix[strlen(infix)-2] = ‘\0’;
               printResult(infix, postfix);
               return(1);
          }
          void initStack(stk *stack) /* initalise the stack */
          {
               stack->top = -1;        /* stack is initially empty */
          }
          void get_infix(char infix[])  /* get infix expression from user */
          {
               int i;
          printf(“Enter infix expression below (max 18 characters excluding spaces) :  \n”);
               fflush(stdin);
               for ( i=0; i<18; )   /* to read in only 18 characters excluding spaces */
               {
                    if ( (infix[i] = getchar()) == ‘\n’ )
                    {
                         i++; break;
                    }
                    else if ( !(isspace(infix[i])) )
                         i++;
               }
               infix[i] = ‘\0’;
          }
          void convertToPostfix(char infix[], char postfix[]) /* convert the infix expression
          to postfix notation */
          {
               int i, length, j=0;



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