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

Infix Topostfix

Programming

Uploaded by

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

Infix Topostfix

Programming

Uploaded by

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

#include<stdio.

h>
#include<stdlib.h>
#include<string.h>
struct stack{
int size;
int top;
char* arr;
};
int stackTop(struct stack * S1){
return S1->arr[S1->top];
}

int isEmpty(struct stack* ptr){


if(ptr->top==-1) return 1;
else return 0;
}
int isFull(struct stack* ptr){
if(ptr->top==ptr->size-1) return 1;
else return 0;
}
void Push(struct stack* ptr,char value){
if(isFull(ptr)) printf("Stack Overflow!! Cannot push %c to the stack\
n",value);
else{
ptr->top++;
ptr->arr[ptr->top] = value;
}
}
char pop(struct stack * ptr){
if(isEmpty(ptr)) printf("Stack Underflow!!");
else{
char value = ptr->arr[ptr->top];
ptr->top--;
return value;
}
}
int precedence(char ch){
if(ch=='*'|| ch=='/') return 3;
else if(ch=='+' || ch=='-') return 2;
else return 0;

}
int isOperator(char ch){
if(ch=='+' || ch=='-' || ch=='*' || ch=='/') return 1;
else return 0;
}
char* infixTopostfix(char* infix){
struct stack *S1 = (struct stack*)malloc(sizeof(struct stack));
S1->size = 100;
S1->top = -1;
S1->arr = (char*)malloc(S1->size* sizeof(char));
char * postfix = (char*)malloc((strlen(infix)+1)*sizeof(char));
int i=0,j=0;
while(infix[i]!='\0'){
if(!isOperator(infix[i])){
postfix[j] = infix[i];
i++;
j++;
}
else{
if(precedence(infix[i])>precedence(stackTop(S1))){
Push(S1,infix[i]);
i++;
}
else{
postfix[j] = pop(S1);
j++;
}
}
}
while(!isEmpty(S1)){
postfix[j] = pop(S1);
j++;
}
postfix[j] = '\0';
return postfix;
}

int main(){
char* infix = "a-b";
printf("Postfix is %s",infixTopostfix(infix));

You might also like