Precedence and Associativity Table
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);
}
void push(char oper)
{
if(isFull())
printf("Stack Full!!!!");
else{
top++;
stk[top] = oper;
}
}
int checkIfOperand(char ch)
{
return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z');
}
int precedence(char ch)
{
switch (ch)
{
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
}
return -1;
}
int covertInfixToPostfix(char* expression)
{
int i, j;
for (i = 0, j = -1; expression[i]; ++i)
{
if (checkIfOperand(expression[i]))
expression[++j] = expression[i];
else if (expression[i] == '(')
push(expression[i]);
else if (expression[i] == ')')
{
while (!isEmpty() && peek() != '(')
expression[++j] = pop();
if (!isEmpty() && peek() != '(')
return -1;
else
pop();
}
else
{
while (!isEmpty() && precedence(expression[i]) <=
precedence(peek()))
expression[++j] = pop();
push(expression[i]);
}
}
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;
};
struct Stack* create(int max)
{
struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));
stack -> maxSize = max;
stack -> top = -1;
stack -> array = (int*)malloc(stack -> maxSize * sizeof(int));
return stack;
}
int isFull(struct Stack* stack)
{
if(stack -> top == stack -> maxSize - 1)
{
printf("Will not be able to push maxSize reached\n");
}
return stack -> top == stack -> maxSize - 1;
}
int isEmpty(struct Stack* stack)
{
return stack -> top == -1;
}
void push(struct Stack* stack, int item)
{
if (isFull(stack))
return;
stack -> array[++stack -> top] = item;
}
int pop(struct Stack* stack)
{
if (isEmpty(stack))
return INT_MIN;
return stack -> array[stack -> top--];
}
int peek(struct Stack* stack)
{
if (isEmpty(stack))
return INT_MIN;
return stack->array[stack->top];
}
int checkIfOperand(char ch)
{
return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z');
}
int precedence(char ch)
{
switch (ch)
{
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
}
return -1;
}
int covertInfixToPostfix(char* expression)
{
int i, j;
struct Stack* stack = create(strlen(expression));
if(!stack)
return -1 ;
for (i = 0, j = -1; expression[i]; ++i)
{
if (checkIfOperand(expression[i]))
expression[++j] = expression[i];
else if (expression[i] == '(')
push(stack, expression[i]);
else if (expression[i] == ')')
{
while (!isEmpty(stack) && peek(stack) != '(')
expression[++j] = pop(stack);
if (!isEmpty(stack) && peek(stack) != '(')
return -1;
else
pop(stack);
}
else
{
while (!isEmpty(stack) && precedence(expression[i]) <=
precedence(peek(stack)))
expression[++j] = pop(stack);
push(stack, expression[i]);
}
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();
}