Thursday, 19 September 2013

Program for converting infix string to postfix using Stack implementation

Aim: Infix to Postfix
Steps:
1. Input the infix string
2. The program use the stack implementation to convert this infix to postfix
3. There are 5 function are used
     Pop() is used to pop the elements
     Push() is used to push the element in the stack
     createstack() is used to create head node
     check() is used to test whether the element has to push or print
     priority() is used to check the priority of the operators


output







Program


#include<stdio.h>
int priority(char a);
int check(char a);
typedef struct node
{
        void *data;
        struct node *link;
}stacknode;
typedef struct
{
        int count;
        stacknode *top;
}stack;
stack *createstack()
{
      stack *sp;
      sp=(stack*)malloc(sizeof(stack));
      sp->top=NULL;
      sp->count=0;
}
void push(stack *sp,void *data)
{
     stacknode *node;
     node=(stacknode*)malloc(sizeof(stacknode));
     node->data=data;
     node->link=sp->top;
     sp->top=node;
     sp->count++;
}
void *pop(stack *sp)
{
     stacknode *temp;
     void *data;
     data=sp->top->data;
     temp=sp->top;
     sp->top=sp->top->link;
     free(temp);
     sp->count--;
     return(data);
}
void *top(stack *sp)
{
       return(sp->top->data);    
}
int main()
{
    stack *sp;
    char s[100];
    int i;
    int j;
    char *a;
    sp=createstack();
    printf("\nenter the string\n");
    scanf("%s",s);
    for(i=0;i<=strlen(s);i++)
    {
       if(check(s[i]))
       {
          printf("%c",s[i]);
       }
       else if(s[i]==')')
       {
            while(*(char*)top(sp)!='(')
            {
                a=(char*)pop(sp);
                printf("%c",*a);
                free(a);
            }
            a=(char*)pop(sp);
            free(a);
       }
       else
       {
           if(s[i]=='(')
           {
               a=(char*)malloc(sizeof(char*));
               *a=s[i];
               push(sp,a);
           }
           else
           {
               if(s[i]=='('||priority(*(char*)top(sp))<priority(s[i]))
               {
                  a=(char*)malloc(sizeof(char*));
                  *a=s[i];
                  push(sp,a);
               }
               else
               {
                         a=(char*)pop(sp);
                         printf("%c",*a);
                         free(a);
                         while(priority(*(char*)top(sp))>=priority(s[i])&&*(char*)pop(sp)!='(')
                         {
                            a=(char*)pop(sp);
                            printf("%c",*a);
                            free(a);   
                         }
                         if(!check(s[i]))
                         {
                           a=(char*)malloc(sizeof(char*));
                           *a=s[i];
                           push(sp,a);
                         }          
               }
           }
       }
    }            
    free(s);
    free(sp);
    getch();
    return(0);
}
int priority(char a)
{
    switch(a)
    {
        case '(': return(-1);
        break;     
        case '/': return(1);
        break;
        case '*': return(1);
        break;
        case '-': return(0);
        break;
        case '+': return(0);
    }
    return(0);
}
int check(char a)
{
    if(a=='*'||a=='/'||a=='-'||a=='+'||a=='('||a==')')
    {
        return(0);
    }
    else    
    {
       return(1);
    }
}            








No comments:

Post a Comment