forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
infixToPrefix.c
146 lines (130 loc) · 4.22 KB
/
infixToPrefix.c
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
/*
Given an infix expression, the aim is to convert it into prefix expression.
In order to do this, we first reverse the expression, and find it's postfix expression.
(Postfix expression can be found with the help of an operator stack, and precedence of operators)
The reverse of this resulting expression gives us the final infix expression.
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
typedef struct stack {
int capacity, top;
char *arr;
} stack;
stack * createStack(int size) {
struct stack *s = (struct stack *) malloc(sizeof(struct stack));
s -> arr = (char *)malloc (size * sizeof(char));
s -> top = -1;
s -> capacity = size;
return s;
}
int isEmpty(stack *s) {
/* tells whether the stack is empty or not */
return s -> top == -1;
}
int isFull(stack *s) {
/* tells whether a stack is full or not */
return s -> top == s -> capacity - 1;
}
void push(stack *s, int data) {
/* push a given element into the stack */
s -> arr[ ++ s -> top] = data;
}
char pop(struct stack *s) {
/* to pop the top element from stack */
return s -> arr[s -> top --];
}
char peek(struct stack *s) {
/* to get the top element of stack */
return s -> arr[s -> top];
}
int precedence(char ch) {
/* gives the precedence of operators */
switch(ch) {
case '+':
case '-': return 1;
case '/':
case '*': return 2;
case '^': return 3;
case ')': return 4;
}
}
stack * infToPost(char infixExp[100]) {
/* To reverse a given infix expression and convert to postfix expression */
int index = 0;
int contains = 0; /* using a variable to see if the operator stack contains a '(' at any time */
int len = strlen(infixExp);
stack *operatorStack = createStack(len);
stack *postfixExp = createStack(len + 1);
/* converting the reverse of given expression into postfix */
for ( index = len - 2; index >= 0; index --) {
char ch = infixExp[index];
if ( isspace(ch)) {
continue;
}
if ( isalnum(ch) ) { /* if its an operand */
push(postfixExp, ch);
push(postfixExp, ' ');
}
else { /* if its an operator */
if (ch == ')' ) {
push(operatorStack, ch);
contains += 1;
}
else if (isEmpty(operatorStack)) {
push(operatorStack, ch);
}
else if ( ch == '(') { /* if the current character is '(', we empty the stack until we encounter ')' */
while ( peek(operatorStack) != ')'){
push(postfixExp, pop(operatorStack));
push(postfixExp, ' ');
}
char waste = pop(operatorStack);
contains -= 1;
}
else if ( (precedence( peek(operatorStack)) <= precedence(ch)) || contains ) { /* comparing the preceedence */
push(operatorStack, ch);
}
else {
while (precedence( peek(operatorStack)) > precedence(ch)) { /* comparing the preceedence */
push(postfixExp, pop(operatorStack));
push(postfixExp, ' ');
if (isEmpty(operatorStack)) {
break;
}
}
push(operatorStack, ch);
}
}
}
while ( !isEmpty(operatorStack)) {
push(postfixExp, pop(operatorStack));
push(postfixExp, ' ');
}
return postfixExp;
}
void displayInReverse(stack *s) {
/* to display the stack in reverse order */
for (int i = s -> top ; i >= 0; i --) {
printf("%c", s -> arr[i]);
}
printf("\n");
}
void infToPre(char infixExp[100]) {
/* converts a given infix expression to infix expression */
stack *postfixExp = infToPost(infixExp);
displayInReverse(postfixExp); /* reversing finally to get prefix of the given expression */
}
int main() {
char infixExp[100];
printf("Enter the Infix expression:\n");
fgets(infixExp, 100, stdin);
printf("The Prefix expression is:\n");
infToPre(infixExp);
return 0;
}
/*
SampleInput: (A + B) * (C + D)
Sample Output: * + A B + C D
*/