Repository navigation
Expand file tree
/
Copy path23_infixtopostfix.c
More file actions
168 lines (150 loc) · 3.79 KB
/
Copy path23_infixtopostfix.c
File metadata and controls
168 lines (150 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Stack
{
int size;
int top;
char *arr; //@ * because its gona be a string %s instead of a %c
};
char stackTop(struct Stack *ptr)
{
char x = ptr->arr[ptr->top];
return x;
}
int isEmpty(struct Stack *ptr)
{
if (ptr->top == -1)
{
return 1;
}
return 0;
}
int isFull(struct Stack *ptr)
{
if (ptr->top == (ptr->size - 1))
{
return 1;
}
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 (ptr->top == -1)
{
printf("Stack underflow ");
}
else
{
char value = ptr->arr[ptr->top];
ptr->top--;
return value;
}
}
//Function to check Operators(+-*/)
int isOperator(char n)
{
if (n == '+' || n == '-' || n == '*' || n == '/')
{
return 1;
}
return 0;
}
//Function to give precedence to Operators i.e. Precedence of * = / > + = -
int precedence(char ch)
{
if (ch == '*' || ch == '/')
{
return 3;
}
else if (ch == '+' || ch == '-')
{
return 2;
}
else
{
return 0;
}
}
char *infixTOpostfix(char *infix)
{
// struct Stack * store; //ye kaam nahi karraha tha kyuki hamne stack ka instance banaya hi nahi even hamne array ko malloc mein space di,
//we also have to give space to stack in space using malloc, else function wont run!!!!!!!
//This stack "store" is to store the operators while computing Postfix
struct Stack *store = (struct Stack *)malloc(sizeof(struct Stack));
store->size = 100;
store->top = -1;
store->arr = (char *)malloc(sizeof(char)); //it's imporant to allocate space for the array characters too
char *postfix = (char *)malloc((strlen(infix) + 1) * sizeof(char)); //This character string will store the Postfix form of Infix!
int i = 0; //for traversing Infix i.e. for reading the infix
int j = 0; //for tracking Postfix addition
//Using while loop - taught by sir:
// while (infix[i] != '\0')
// {
// if (!isOperator(infix[i])) //is not operator that is not +-*/
// {
// postfix[j]= infix[i];
// i++;
// j++;
// }
// else{
// if ( precedence(infix[i]) > precedence(stackTop(store)) )
// {
// push(store, infix[i]);
// i++; //sirf yaha par i++ karo not in the else part!!!! varna ek operator skip kar diya jayega
// }
// else{
// postfix[j]= pop(store);
// j++;
// }
// }
// }
//using FOR LOOP - done by me completely!!! AND AMAZING ANSWER FASTER! :
for (int i = 0; infix[i] != '\0'; i++)
{
if (!isOperator(infix[i]))
{
postfix[j] = infix[i];
j++;
}
else
{
if (precedence(infix[i]) > precedence(stackTop(store)))
{
push(store, infix[i]);
}
else
{
postfix[j] = pop(store);
j++;
i--; // THIS IS THE PART WHICH IS MAKING ENTIRE DIFFERENCE IN FOR AND WHILE LOOP!!!
}
}
}
while (!isEmpty(store))
{
postfix[j] = pop(store);
j++;
}
postfix[j] = '\0'; //it's important to end the string hence \0
return postfix;
}
int main()
{
char *infixform = "x+y/z-k*d";
printf("Postfix form of %s is %s\n", infixform, infixTOpostfix(infixform));
return 0;
}
//THIS WAS AMAZING!!! LOVED TO CODE IT! ALSO REFER NOTES FOR CLEAR PICTURE!