Notations Conversion
Notations Conversion
Notes Amit@CodesforStructures
Conversion of Infix to Postfix Using an Array-
Based Stack
#include<limits.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX 20
char stk[20];
int top = -1;
int isEmpty()
{
return top == -1;
}
int isFull()
{
return top == MAX - 1;
}
char peek()
{
return stk[top];
}
char pop()
{
if(isEmpty())
return -1;
char ch = stk[top];
top--;
return(ch);
}
else{
top++;
stk[top] = oper;
}
}
case '*':
case '/':
return 2;
case '^':
return 3;
}
return -1;
}
while (!isEmpty())
expression[++j] = pop();
expression[++j] = '\0';
printf( "%s", expression);
}
int main()
{
char expression[] = "(a*b(x+(y*z))-w)";
covertInfixToPostfix(expression);
return 0;
}
Using
Struct;;;;;;;
#include<stdio.h>
#include <string.h>
#include <limits.h>
#include <stdlib.h>
struct Stack {
int top;
int maxSize;
int* array;
};
case '*':
case '/':
return 2;
case '^':
return 3;
}
return -1;
}
while (!isEmpty(stack))
expression[++j] = pop(stack);
expression[++j] = '\0';
printf( "%s", expression);
}
int main()
{
char expression[] = "((x+(y*z))-w)";
covertInfixToPostfix(expression);
return 0;
}
Prefix to Postfix
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
# define MAX 20
char str[MAX],stack[MAX];
int top=-1;
void push(char c)
{
stack[++top]=c;
}
char pop()
{
return stack[top--];
}
void pre_post()
{
int n,i,j=0; char c[20];
char a,b,op;
printf("Enter the prefix expression\n");
gets(str);
n=strlen(str);
for(i=0;i<MAX;i++)
stack[i]='\0';
printf("Postfix expression is:\t");
for(i=0;i<n;i++)
{
if(str[i]=='+'||str[i]=='-'||str[i]=='*'||str[i]=='/')
{
push(str[i]);
}
else
{ c[j++]=str[i];
while((top!=-1)&&(stack[top]=='@'))
{
a=pop(); c[j++]=pop();
}
push('@');
}
}
c[j]='\0';
printf("%s",c);
}
main()
{
pre_post();
}