You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/********** Function to return precedence of the operators ********/
46
+
intprecedence(char x)
47
+
{
48
+
if (x == '+' || x == '-') //if character (ie operator) is + or - return precedence as 1
49
+
return1;
50
+
elseif (x == '*' || x == '/') //if character is * or / return precedence as 2
51
+
return2;
52
+
else//else return precedence as 0
53
+
return0;
54
+
}
55
+
56
+
/********** Function to check if a character in infix notation is an operand or not **********/
57
+
intisOperand(char x)
58
+
{
59
+
if (x == '+' || x == '-' || x == '*' || x == '/') //if character is not an operand return false as 0
60
+
return0;
61
+
else//if character is an operand return true as 1
62
+
return1;
63
+
}
64
+
65
+
/****** Infix to Postfix Function*******/
66
+
char* InfixToPostfix(char* infix) //function which takes pointer to a character array as parameter and returns pointer to a character array
67
+
{
68
+
int i = 0, j = 0; //i and j initialised with 0 which will help in accessing and scanning the character array
69
+
char* postfix; //pointer to a character array //it will hold the postfix notation created by the function
70
+
int len = strlen(infix); //len is assigned with the string length of the infix array that has been passed as an argument //it is done to allocate same size of memory for postfix array in the HEAP
71
+
postfix = (char*)malloc((len + 1) * sizeof(char)); //memory is allocated for the postfix array inside the HEAP of size equal to (len+1) where len is string length of the infix array // +1 has been added to len to store '\0' to make it a string
72
+
73
+
while (infix[i] != '\0') //Using while loop instead of for loop as i is not incrementing continuosly after every loop
74
+
{
75
+
if (isOperand(infix[i])) //if the character in the infix array is an operand it must be copied directly into the postfix array
76
+
postfix[j++] = infix[i++]; //postfix copies character of infix //i and j both are incremented
77
+
else
78
+
{
79
+
if (precedence(infix[i]) > precedence(top->data)) //if precedence of the character of infix array is greater than the precedence of the character in the top of the stack //CAUTION: as there is a chance of the top of stack getting NULL and to avoid accessing NULL->data (error), '#' is pushed in the stack before hand to avoid this problem
80
+
push(infix[i++]); //push the character of the infix array inside the stack //i is incremented
81
+
else//if precedence of the character of infix array is less or equal than the precedence of the character in the top of the stack
82
+
{
83
+
postfix[j++] = pop(); //pop the character from the stack and copy the popped character in the postfix array //j is incremented
84
+
}
85
+
}
86
+
}
87
+
88
+
//If any operators are left inside the stack, all must pe popped out and copied into the postfix array
89
+
while (top->data != '#') //Terminating condition for popping //'#' has been used to avoid the error of NULL->data (error)
90
+
postfix[j++] = pop(); //characters are popped out of the stack one by one and copied in the postfix array
91
+
92
+
postfix[j] = '\0'; //last index of postfix array is assigned with '\0' to make it a complete string
93
+
94
+
return postfix; //postfix array is returned as char *
95
+
}
96
+
97
+
intmain()
98
+
{
99
+
char infix [] = "a+b*c-d/e"; //infix expression
100
+
push('#'); //'#' is pushed inside stack before hand to avoid the error of accessing NULL->data (error)
101
+
102
+
char* postfix = InfixToPostfix(infix); //calling the function for conversion
0 commit comments