0% found this document useful (0 votes)
15 views2 pages

+DS 08 Infix Postfix

This document contains a C program that converts an infix expression to a postfix expression using a stack data structure. It defines functions for pushing and popping elements from the stack, determining operator precedence, and processing the input expression. The program reads an infix expression, processes it, and outputs the corresponding postfix expression.

Uploaded by

manasi wani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views2 pages

+DS 08 Infix Postfix

This document contains a C program that converts an infix expression to a postfix expression using a stack data structure. It defines functions for pushing and popping elements from the stack, determining operator precedence, and processing the input expression. The program reads an infix expression, processes it, and outputs the corresponding postfix expression.

Uploaded by

manasi wani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

// program for conversion of infix expression to postfix expression

PROGRAM

#include<stdio.h>
#define MAX 100

char stack[MAX];
int top=-1;

void push(char ch)


{
if (top>= MAX -1)
printf("stack Overflow\n");
else
stack [++top]=ch;
}

char pop()
{
if (top<0)
{
printf("Stack underflow\n");
return -1;
}
else
{
return stack[top--];
}
}
int precedence(char operator)
{
switch(operator)
{
case '+':
case '-': return 1;
case '*':
case '/': return 2;
case '^': return 3;
default : return 0;
}
}
int main()
{
char infix[MAX], postfix[MAX], ch ,temp;
int i=0, j=0;

printf("Enter the infix expression: ");


scanf("%s",&infix);
while ((ch = infix[i++]) !='\0')
{
if(isalnum(ch))
postfix[j++]=ch;
else if (ch=='(')
push(ch);
else if (ch ==')')
{
while((temp =pop()) !='(')
postfix[j++]=temp;
}
else
{
while ( top !=-1 && precedence(stack[top]) >=precedence(ch))
postfix[j++]=pop();

push(ch);
}
}
while (top!= -1)
postfix[j++]=pop();

postfix [j] ='\0';

printf("postfix expression :%s\n",postfix);


return 0;
}

OUTPUT

You might also like