Conversion of Infix to Postfix Notation lab program

 #include<stdio.h>

#include<ctype.h>

char stack[20];

int top = -1;

void push(char);

char pop();

int precedence(char);

int main()

{

char infix[20],postfix[20];

int i,j=0;

printf("Enter an Infix expression:-\t");

gets(infix);

for (i = 0 ; infix[i] != '\0' ; i++)

{

if(isalnum(infix[i]))

{

postfix[j] = infix[i];

j++;

}

else if(infix[i] == '(')

push(infix[i]);

else if(infix[i] == ')')

{

while(stack[top] != '(')

{

postfix[j] = pop();

j++;

}

pop();

}

else

{

while((top != -1 && stack[top] != '(') && (precedence(stack[top]) >= precedence(infix[i])))

{

postfix[j] = pop();

j++;

}

push(infix[i]);

}

}

while(top != -1)

{

postfix[j] = pop();

j++;

}

postfix[j] = '\0';

printf("PostFix Expression = %s\n",postfix);

return 0;

}

void push(char c)

{

top++;

stack[top] = c;

}

char pop()

{

char c;

c = stack[top];

top--;

return c;

}

int precedence(char c)

{

switch(c)

{

case '+': 

case '-': return 1;

case '*':

case '/': return 2;

}

}


Comments

Popular posts from this blog

Data Structures UNIT-1 Chapter-1

Unit-2 chapter-1 STACKS AND ITS APPLICTIONS

Single linked List Lab program